diff --git a/packages/mask/src/plugins/Trader/trader/0x/useTradeCallback.ts b/packages/mask/src/plugins/Trader/trader/0x/useTradeCallback.ts index a87fd4822a04..50dffd19ff2b 100644 --- a/packages/mask/src/plugins/Trader/trader/0x/useTradeCallback.ts +++ b/packages/mask/src/plugins/Trader/trader/0x/useTradeCallback.ts @@ -21,14 +21,14 @@ export function useTradeCallback(tradeComputed: TradeComputed if (!account || !tradeComputed?.trade_ || !SUPPORTED_CHAIN_ID_LIST.includes(chainId)) return null return { from: account, - ...pick(tradeComputed.trade_, ['to', 'data', 'value', 'gas', 'gasPrice']), + ...pick(tradeComputed.trade_, ['to', 'data', 'value']), ...gasConfig, } as TransactionConfig }, [account, tradeComputed]) const tradeCallback = useCallback(async () => { // validate config - if (!account || !config) { + if (!account || !config || !tradeComputed) { setTradeState({ type: TransactionStateType.UNKNOWN, }) @@ -40,27 +40,25 @@ export function useTradeCallback(tradeComputed: TradeComputed type: TransactionStateType.WAIT_FOR_CONFIRMING, }) - // estimate transaction - try { - await web3.eth.call(config) - } catch { - // for some transactions will always fail if we do estimation before a kick to the chain - if ( - !confirm( - 'Failed to estimated the transaction, which means it may be reverted on the chain, and your transaction fee will not return. Sure to continue?', - ) - ) { - setTradeState({ - type: TransactionStateType.FAILED, - error: new Error('User denied the transaction.'), + const config_ = { + ...config, + gas: await web3.eth + .estimateGas({ + from: account, + ...pick(tradeComputed.trade_, ['to', 'data', 'value']), }) - return - } + .catch((error) => { + setTradeState({ + type: TransactionStateType.FAILED, + error, + }) + return 0 + }), } // send transaction and wait for hash return new Promise((resolve, reject) => { - web3.eth.sendTransaction(config, (error, hash) => { + web3.eth.sendTransaction(config_, (error, hash) => { if (error) { setTradeState({ type: TransactionStateType.FAILED, diff --git a/packages/mask/src/plugins/Trader/trader/0x/useTradeGasLimit.ts b/packages/mask/src/plugins/Trader/trader/0x/useTradeGasLimit.ts index 880c90c47e9e..0808a70150a0 100644 --- a/packages/mask/src/plugins/Trader/trader/0x/useTradeGasLimit.ts +++ b/packages/mask/src/plugins/Trader/trader/0x/useTradeGasLimit.ts @@ -1,14 +1,26 @@ import type { TradeComputed, SwapQuoteResponse } from '../../types' import { useMemo } from 'react' import { TargetChainIdContext } from '../useTargetChainIdContext' -import BigNumber from 'bignumber.js' import { SUPPORTED_CHAIN_ID_LIST } from './constants' +import { useAccount, useWeb3 } from '@masknet/web3-shared-evm' +import { pick } from 'lodash-unified' +import { useAsync } from 'react-use' export function useTradeGasLimit(tradeComputed: TradeComputed | null) { const { targetChainId } = TargetChainIdContext.useContainer() - return useMemo(() => { - if (!tradeComputed?.trade_ || !SUPPORTED_CHAIN_ID_LIST.includes(targetChainId)) return 0 - return new BigNumber(tradeComputed.trade_.gas).toNumber() - }, [targetChainId, tradeComputed]) + const web3 = useWeb3({ chainId: targetChainId }) + const account = useAccount() + const config = useMemo(() => { + if (!account || !tradeComputed?.trade_) return null + return { + from: account, + ...pick(tradeComputed.trade_, ['to', 'data', 'value']), + } + }, [account, tradeComputed]) + + return useAsync(async () => { + if (!tradeComputed?.trade_ || !SUPPORTED_CHAIN_ID_LIST.includes(targetChainId) || !config) return 0 + return web3.eth.estimateGas(config) + }, [targetChainId, tradeComputed, config, web3]) } diff --git a/packages/mask/src/plugins/Trader/trader/useAllTradeComputed.ts b/packages/mask/src/plugins/Trader/trader/useAllTradeComputed.ts index c8144face12a..95d3c4d7ea1f 100644 --- a/packages/mask/src/plugins/Trader/trader/useAllTradeComputed.ts +++ b/packages/mask/src/plugins/Trader/trader/useAllTradeComputed.ts @@ -205,7 +205,7 @@ export function useAllTradeComputed( { provider: TradeProvider.QUICKSWAP, ...quickSwap_, value: quickSwap, gas: quickSwapEstimateGas }, { provider: TradeProvider.PANCAKESWAP, ...pancakeSwap_, value: pancakeSwap, gas: pancakeSwapEstimateGas }, { provider: TradeProvider.UNISWAP_V3, ...uniswapV3_, value: uniswapV3, gas: uniswapV3SwapEstimateGas }, - { provider: TradeProvider.ZRX, ...zrx_, value: zrx, gas: { value: zrxSwapEstimateGas, loading: false } }, + { provider: TradeProvider.ZRX, ...zrx_, value: zrx, gas: zrxSwapEstimateGas }, { provider: TradeProvider.BALANCER, ...balancer_, value: balancer, gas: balancerSwapEstimateGas }, { provider: TradeProvider.DODO, ...dodo_, value: dodo, gas: dodoSwapEstimateGas }, { provider: TradeProvider.BANCOR, ...bancor_, value: bancor, gas: bancorSwapEstimateGas },