Skip to content

Commit

Permalink
feat: not show allowance if not need
Browse files Browse the repository at this point in the history
  • Loading branch information
solidovic committed Oct 1, 2024
1 parent 9b0b4ee commit 7e30d40
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 16 deletions.
4 changes: 4 additions & 0 deletions features/wsteth/unwrap/hooks/use-unwrap-tx-on-l2-approve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import type { BigNumber } from 'ethers';

import { runWithTransactionLogger } from 'utils';
import { useLidoSDK } from 'providers/lido-sdk';
import { useDappStatus } from 'shared/hooks/use-dapp-status';

type UseUnwrapTxApproveArgs = {
amount: BigNumber;
};

export const useUnwrapTxOnL2Approve = ({ amount }: UseUnwrapTxApproveArgs) => {
const { isDappActiveOnL2 } = useDappStatus();
const { sdk } = useLidoSDK();
const [allowance, setAllowance] = useState<bigint | null>(null);
const [isApprovalNeededBeforeUnwrap, setIsApprovalNeededBeforeUnwrap] =
Expand Down Expand Up @@ -67,12 +69,14 @@ export const useUnwrapTxOnL2Approve = ({ amount }: UseUnwrapTxApproveArgs) => {
refetchAllowance,
allowance,
isApprovalNeededBeforeUnwrap,
isShowAllowance: isDappActiveOnL2,
}),
[
processApproveTx,
refetchAllowance,
allowance,
isApprovalNeededBeforeUnwrap,
isDappActiveOnL2,
],
);
};
18 changes: 15 additions & 3 deletions features/wsteth/unwrap/unwrap-form/unwrap-stats.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { useWatch } from 'react-hook-form';
import { BigNumber } from 'ethers';
import { DataTableRow, DataTable } from '@lidofinance/lido-ui';
import { TOKENS } from '@lido-sdk/constants';

import { useTxCostInUsd } from 'shared/hooks';
import { FormatToken } from 'shared/formatters/format-token';
import { DataTableRowStethByWsteth } from 'shared/components/data-table-row-steth-by-wsteth';
import { AllowanceDataTableRow } from 'shared/components/allowance-data-table-row';
import { FormatToken } from 'shared/formatters/format-token';
import { FormatPrice } from 'shared/formatters';
import { useTxCostInUsd } from 'shared/hooks';

import { useDebouncedStethByWsteth } from 'features/wsteth/shared/hooks/use-debounced-wsteth-steth';
import { useUnwrapGasLimit } from '../hooks/use-unwrap-gas-limit';
import type { UnwrapFormInputType } from '../unwrap-form-context';
import { useUnwrapFormData, UnwrapFormInputType } from '../unwrap-form-context';

export const UnwrapStats = () => {
const { allowance, isShowAllowance } = useUnwrapFormData();
const amount = useWatch<UnwrapFormInputType, 'amount'>({ name: 'amount' });
const unwrapGasLimit = useUnwrapGasLimit();
const {
Expand Down Expand Up @@ -43,6 +47,14 @@ export const UnwrapStats = () => {
<FormatPrice amount={unwrapTxCostInUsd} />
</DataTableRow>
<DataTableRowStethByWsteth />
{isShowAllowance && (
<AllowanceDataTableRow
data-testid="allowance"
allowance={BigNumber.from(allowance || '0')}
loading={false}
token={TOKENS.WSTETH}
/>
)}
</DataTable>
);
};
4 changes: 3 additions & 1 deletion features/wsteth/wrap/hooks/use-wrap-tx-on-l1-approve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const useWrapTxOnL1Approve = ({
amount,
token,
}: UseWrapTxApproveArgs) => {
const { isDappActive } = useDappStatus();
const { isDappActive, isDappActiveOnL1 } = useDappStatus();
const { address } = useAccount();
const { chainId } = useSDK();

Expand Down Expand Up @@ -54,6 +54,7 @@ export const useWrapTxOnL1Approve = ({
isApprovalLoading,
isApprovalNeededBeforeWrap,
refetchAllowance,
isShowAllowance: isDappActiveOnL1,
}),
[
allowance,
Expand All @@ -62,6 +63,7 @@ export const useWrapTxOnL1Approve = ({
isApprovalLoading,
processApproveTx,
refetchAllowance,
isDappActiveOnL1,
],
);
};
19 changes: 11 additions & 8 deletions features/wsteth/wrap/wrap-form/wrap-stats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ const oneSteth = parseEther('1');

export const WrapFormStats = () => {
const { isDappActive } = useDappStatus();
const { allowance, wrapGasLimit, isApprovalLoading } = useWrapFormData();
const { allowance, isShowAllowance, wrapGasLimit, isApprovalLoading } =
useWrapFormData();

const { watch } = useFormContext<WrapFormInputType>();
const [token, amount] = watch(['token', 'amount']);
Expand Down Expand Up @@ -91,13 +92,15 @@ export const WrapFormStats = () => {
DATA_UNAVAILABLE
)}
</DataTableRow>
<AllowanceDataTableRow
data-testid="allowance"
allowance={allowance}
isBlank={!(isSteth && isDappActive)}
loading={isApprovalLoading}
token={TOKENS.STETH}
/>
{isShowAllowance && (
<AllowanceDataTableRow
data-testid="allowance"
allowance={allowance}
isBlank={!(isSteth && isDappActive)}
loading={isApprovalLoading}
token={TOKENS.STETH}
/>
)}
</DataTable>
);
};
13 changes: 9 additions & 4 deletions shared/hooks/use-dapp-status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,30 @@ export const useDappStatus = () => {
[chainId, multiChainBanner],
);

// TODO: rename isDappActive to isDappActiveOnL1
const isDappActive = useMemo(() => {
if (!chainId) return false;

return isWalletConnected && isSupportedChain;
}, [chainId, isWalletConnected, isSupportedChain]);

const isDappActiveOnL1 = useMemo(() => {
if (!chainId) return false;

return isDappActive && !isSDKSupportedL2Chain(chainId);
}, [chainId, isDappActive]);

const isDappActiveOnL2 = useMemo(() => {
if (!chainId) return false;

// TODO: check connected
return isSDKSupportedL2Chain(chainId);
}, [chainId]);
return isDappActive && isSDKSupportedL2Chain(chainId);
}, [chainId, isDappActive]);

return {
isWalletConnected,
isSupportedChain,
isLidoMultichainChain,
isDappActive,
isDappActiveOnL1,
isDappActiveOnL2,
};
};

0 comments on commit 7e30d40

Please sign in to comment.