|
1 | 1 | import { skipToken, useQuery } from '@tanstack/react-query'; |
2 | 2 | import { erc20Abi, encodeFunctionData } from 'viem'; |
3 | | -import { useEstimateGas, useGasPrice, usePublicClient } from 'wagmi'; |
| 3 | +import { useEstimateFeesPerGas, useEstimateGas, usePublicClient } from 'wagmi'; |
4 | 4 |
|
5 | 5 | import { useWallet } from 'src/domains/chains/components/WalletProvider.tsx'; |
6 | 6 | import { Token } from 'src/domains/chains/types/misc'; |
7 | 7 | import useChain from 'src/domains/chains/utils/useChain'; |
| 8 | +import getQueryKey from 'src/domains/misc/utils/getQueryKey'; |
8 | 9 |
|
9 | 10 | type Props = { |
10 | 11 | token: Token, |
11 | 12 | amount: bigint, |
12 | | - enabled?: boolean, |
| 13 | + disabled?: boolean, |
13 | 14 | }; |
14 | 15 |
|
15 | | -const useEstimateAllowanceFee = ({ token, amount, enabled = true }: Props) => { |
| 16 | +const useEstimateAllowanceFee = ({ token, amount, disabled }: Props) => { |
16 | 17 | const chainConfig = useChain(); |
17 | 18 | const publicClient = usePublicClient(); |
18 | 19 | const { address: walletAddress } = useWallet(); |
19 | 20 |
|
20 | | - const shouldCheckAllowance = !token.isNative && amount > 0n && enabled; |
| 21 | + const shouldCheckAllowance = !token.isNative && amount > 0n && !disabled; |
21 | 22 |
|
22 | 23 | const { data: needsApproval } = useQuery({ |
23 | 24 | queryKey: chainConfig && walletAddress && shouldCheckAllowance ? |
24 | | - ['allowance-check', token.address, chainConfig.id.toString(), walletAddress, amount.toString()] : [], |
| 25 | + getQueryKey.allowanceCheck(token.address, chainConfig.id.toString(), walletAddress, amount.toString()) : [], |
25 | 26 | queryFn: !publicClient || !chainConfig?.shielderConfig || !walletAddress || !shouldCheckAllowance ? |
26 | 27 | skipToken : |
27 | 28 | async (): Promise<boolean> => { |
@@ -50,20 +51,35 @@ const useEstimateAllowanceFee = ({ token, amount, enabled = true }: Props) => { |
50 | 51 | abi: erc20Abi, |
51 | 52 | functionName: 'approve', |
52 | 53 | args: [chainConfig.shielderConfig.shielderContractAddress, amount], |
53 | | - }) : undefined, |
| 54 | + }) : |
| 55 | + undefined, |
54 | 56 | account: walletAddress, |
55 | 57 | query: { |
56 | | - enabled: shouldCheckAllowance && !!needsApproval && !!chainConfig?.shielderConfig && !!walletAddress, |
| 58 | + enabled: |
| 59 | + shouldCheckAllowance && |
| 60 | + !!needsApproval && |
| 61 | + !!chainConfig?.shielderConfig && |
| 62 | + !!walletAddress, |
57 | 63 | }, |
58 | 64 | }); |
59 | 65 |
|
60 | | - const { data: gasPrice } = useGasPrice({ |
| 66 | + const { data: feeEstimate } = useEstimateFeesPerGas({ |
| 67 | + chainId: chainConfig?.id, |
61 | 68 | query: { |
62 | | - enabled: shouldCheckAllowance && !!needsApproval && !!gasEstimate, |
| 69 | + enabled: |
| 70 | + shouldCheckAllowance && |
| 71 | + !!needsApproval && |
| 72 | + !!chainConfig?.shielderConfig && |
| 73 | + !!walletAddress, |
63 | 74 | }, |
64 | 75 | }); |
65 | 76 |
|
66 | | - const allowanceFee = gasEstimate && gasPrice ? gasEstimate * gasPrice : null; |
| 77 | + const gasPrice = feeEstimate?.maxFeePerGas; |
| 78 | + |
| 79 | + const allowanceFee = |
| 80 | + gasEstimate && feeEstimate?.maxFeePerGas ? |
| 81 | + gasEstimate * feeEstimate.maxFeePerGas : |
| 82 | + null; |
67 | 83 |
|
68 | 84 | if (!shouldCheckAllowance) { |
69 | 85 | return { |
|
0 commit comments