Skip to content

Commit

Permalink
fix: usdt token approval
Browse files Browse the repository at this point in the history
  • Loading branch information
0xMasayoshi committed Jan 30, 2025
1 parent 1b01e48 commit e083815
Showing 1 changed file with 52 additions and 9 deletions.
61 changes: 52 additions & 9 deletions apps/web/src/lib/wagmi/hooks/approvals/hooks/useTokenApproval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { erc20Abi_approve } from 'sushi/abi'
import { Amount, Type } from 'sushi/currency'
import {
Address,
ContractFunctionZeroDataError,
SendTransactionReturnType,
UserRejectedRequestError,
maxUint256,
Expand All @@ -21,6 +22,19 @@ import {
import { ERC20ApproveABI, ERC20ApproveArgs } from './types'
import { useTokenAllowance } from './useTokenAllowance'

const old_erc20Abi_approve = [
{
type: 'function',
name: 'approve',
stateMutability: 'nonpayable',
inputs: [
{ name: '_spender', type: 'address' },
{ name: '_value', type: 'uint256' },
],
outputs: [],
},
] as const

export enum ApprovalState {
LOADING = 'LOADING',
UNKNOWN = 'UNKNOWN',
Expand Down Expand Up @@ -57,7 +71,11 @@ export const useTokenApproval = ({
enabled: Boolean(amount?.currency?.isToken && enabled),
})

const { data: simulation } = useSimulateContract<
const simulationEnabled = Boolean(
amount && spender && address && allowance && enabled && !isAllowanceLoading,
)

const standardSimulation = useSimulateContract<
ERC20ApproveABI,
'approve',
ERC20ApproveArgs
Expand All @@ -71,17 +89,42 @@ export const useTokenApproval = ({
approveMax ? maxUint256 : amount ? amount.quotient : 0n,
],
query: {
enabled: Boolean(
amount &&
spender &&
address &&
allowance &&
enabled &&
!isAllowanceLoading,
),
enabled: simulationEnabled,
retry: (failureCount, error) => {
if (error instanceof ContractFunctionZeroDataError) return false
return failureCount < 2
},
},
})

const fallbackSimulationEnabled = Boolean(
standardSimulation.isError &&
standardSimulation.error instanceof ContractFunctionZeroDataError &&
simulationEnabled,
)

const fallbackSimulation = useSimulateContract<
typeof old_erc20Abi_approve,
'approve',
ERC20ApproveArgs
>({
chainId: amount?.currency.chainId,
abi: old_erc20Abi_approve,
address: amount?.currency?.wrapped?.address as Address,
functionName: 'approve',
args: [
spender as Address,
approveMax ? maxUint256 : amount ? amount.quotient : 0n,
],
query: {
enabled: fallbackSimulationEnabled,
},
})

const { data: simulation } = fallbackSimulationEnabled
? fallbackSimulation
: standardSimulation

const onSuccess = useCallback(
async (data: SendTransactionReturnType) => {
if (!amount) return
Expand Down

0 comments on commit e083815

Please sign in to comment.