Skip to content

Commit

Permalink
fix: bugfix for zrx (DimensionDev#5891)
Browse files Browse the repository at this point in the history
* fix: replace gas source from zrx api to web3 api

* chore: reply review
  • Loading branch information
albert-0229 authored Mar 18, 2022
1 parent 439e9dc commit 973f999
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 24 deletions.
34 changes: 16 additions & 18 deletions packages/mask/src/plugins/Trader/trader/0x/useTradeCallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ export function useTradeCallback(tradeComputed: TradeComputed<SwapQuoteResponse>
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,
})
Expand All @@ -40,27 +40,25 @@ export function useTradeCallback(tradeComputed: TradeComputed<SwapQuoteResponse>
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<string>((resolve, reject) => {
web3.eth.sendTransaction(config, (error, hash) => {
web3.eth.sendTransaction(config_, (error, hash) => {
if (error) {
setTradeState({
type: TransactionStateType.FAILED,
Expand Down
22 changes: 17 additions & 5 deletions packages/mask/src/plugins/Trader/trader/0x/useTradeGasLimit.ts
Original file line number Diff line number Diff line change
@@ -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<SwapQuoteResponse> | 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])
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down

0 comments on commit 973f999

Please sign in to comment.