Skip to content

Commit

Permalink
feat: add allowance loading
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeday committed Mar 5, 2024
1 parent f6d703f commit 390a130
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 13 deletions.
9 changes: 5 additions & 4 deletions features/withdrawals/hooks/contract/useRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ export const useWithdrawalRequest = ({
approve,
needsApprove,
allowance,
loading: loadingUseApprove,
initialLoading: loadingUseApprove,
} = useApprove(
valueBN,
tokenContract.address,
Expand All @@ -331,10 +331,11 @@ export const useWithdrawalRequest = ({
spender: withdrawalQueueAddress,
});

const isApprovalFlow =
const isApprovalFlow = Boolean(
connector?.id === 'walletConnect' ||
isMultisig ||
(allowance.gt(BigNumber.from(0)) && !needsApprove);
isMultisig ||
(allowance && allowance.gt(Zero) && !needsApprove),
);

const isApprovalFlowLoading =
isMultisigLoading || (isApprovalFlow && loadingUseApprove);
Expand Down
1 change: 1 addition & 0 deletions features/wsteth/unwrap/unwrap-form/unwrap-stats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const UnwrapStats = () => {
>
<FormatToken
data-testid="youWillReceive"
fallback="-"
amount={willReceiveStETH}
symbol="stETH"
showAmountTip
Expand Down
1 change: 1 addition & 0 deletions features/wsteth/wrap/wrap-form/wrap-stats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export const WrapFormStats = () => {
<FormatToken
amount={willReceiveWsteth}
data-testid="youWillReceive"
fallback="-"
symbol="wstETH"
showAmountTip
trimEllipsis
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export type AllowanceDataTableRowProps = Omit<
title?: ReactNode;
token: TOKENS.WSTETH | TOKENS.STETH | 'ETH';
allowance?: BigNumber;
loading?: boolean;
isBlank?: boolean;
};

Expand Down
14 changes: 5 additions & 9 deletions shared/hooks/useApprove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import invariant from 'tiny-invariant';
import { useCallback } from 'react';

import { ContractReceipt, ContractTransaction } from '@ethersproject/contracts';
import { Zero } from '@ethersproject/constants';
import { BigNumber } from '@ethersproject/bignumber';
import { getERC20Contract } from '@lido-sdk/contracts';
import { useAllowance, useSDK } from '@lido-sdk/react';
Expand All @@ -26,7 +25,7 @@ export type UseApproveResponse = {
approve: (options?: ApproveOptions) => Promise<void>;
needsApprove: boolean;
initialLoading: boolean;
allowance: BigNumber;
allowance: BigNumber | undefined;
loading: boolean;
error: unknown;
};
Expand All @@ -45,14 +44,11 @@ export const useApprove = (
invariant(spender != null, 'Spender is required');

const result = useAllowance(token, spender, mergedOwner, STRATEGY_LAZY);
const {
data: allowance = Zero,
initialLoading,
update: updateAllowance,
} = result;
const { data: allowance, initialLoading, update: updateAllowance } = result;

const needsApprove =
!initialLoading && !amount.isZero() && amount.gt(allowance);
const needsApprove = Boolean(
!initialLoading && allowance && !amount.isZero() && amount.gt(allowance),
);

const approve = useCallback<UseApproveResponse['approve']>(
async ({ onTxStart, onTxSent, onTxAwaited } = {}) => {
Expand Down

0 comments on commit 390a130

Please sign in to comment.