Skip to content

Commit

Permalink
fix: swap errors (Uniswap#431)
Browse files Browse the repository at this point in the history
  • Loading branch information
zzmp authored Jan 20, 2023
1 parent 8a4e572 commit 40a45f5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
22 changes: 15 additions & 7 deletions src/hooks/useUniversalRouter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BigNumber } from '@ethersproject/bignumber'
import { TransactionResponse } from '@ethersproject/providers'
import { TransactionRequest, TransactionResponse } from '@ethersproject/providers'
import { t } from '@lingui/macro'
import { Percent } from '@uniswap/sdk-core'
import { SwapRouter, UNIVERSAL_ROUTER_ADDRESS } from '@uniswap/universal-router-sdk'
import { FeeOptions, toHex } from '@uniswap/v3-sdk'
Expand All @@ -23,6 +24,8 @@ export function useUniversalRouterSwapCallback(trade: InterfaceTrade | undefined
const { account, chainId, provider } = useWeb3React()

return useCallback(async (): Promise<TransactionResponse> => {
let tx: TransactionRequest
let response: TransactionResponse
try {
if (!account) throw new Error('missing account')
if (!chainId) throw new Error('missing chainId')
Expand All @@ -35,7 +38,7 @@ export function useUniversalRouterSwapCallback(trade: InterfaceTrade | undefined
inputTokenPermit: options.permit,
fee: options.feeOptions,
})
const tx = {
tx = {
from: account,
to: UNIVERSAL_ROUTER_ADDRESS(chainId),
data,
Expand All @@ -47,16 +50,21 @@ export function useUniversalRouterSwapCallback(trade: InterfaceTrade | undefined
try {
gasEstimate = await provider.estimateGas(tx)
} catch (gasError) {
await provider.call(tx) // this should throw the actual error
throw new Error('unexpected issue with gas estimation; please try again')
console.warn(gasError)
throw new Error('Your swap is expected to fail')
}
const gasLimit = calculateGasMargin(gasEstimate)
const response = await provider.getSigner().sendTransaction({ ...tx, gasLimit })
return response
response = await provider.getSigner().sendTransaction({ ...tx, gasLimit })
} catch (swapError: unknown) {
const message = swapErrorToUserReadableMessage(swapError)
throw new Error(`Trade failed: ${message}`)
throw new Error(message)
}
if (tx.data !== response.data) {
throw new Error(
t`Your swap was modified through your wallet. If this was a mistake, please cancel immediately or risk losing your funds.`
)
}
return response
}, [
account,
chainId,
Expand Down
7 changes: 3 additions & 4 deletions src/utils/swapErrorToUserReadableMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function swapErrorToUserReadableMessage(error: any): string {

switch (reason) {
case 'UniswapV2Router: EXPIRED':
return t`The transaction could not be sent because the deadline has passed. Please check that your transaction deadline is not too low.`
return t`This transaction could not be sent because the deadline has passed. Please check that your transaction deadline is not too low.`
case 'UniswapV2Router: INSUFFICIENT_OUTPUT_AMOUNT':
case 'UniswapV2Router: EXCESSIVE_INPUT_AMOUNT':
return t`This transaction will not succeed either due to price movement or fee on transfer. Try increasing your slippage tolerance.`
Expand All @@ -43,8 +43,7 @@ export function swapErrorToUserReadableMessage(error: any): string {
console.error(error, reason)
return t`An error occurred when trying to execute this swap. You may need to increase your slippage tolerance. If that does not work, there may be an incompatibility with the token you are trading. Note: fee on transfer and rebase tokens are incompatible with Uniswap V3.`
}
return t`Unknown error${
reason ? `: "${reason}"` : ''
}. Try increasing your slippage tolerance. Note: fee on transfer and rebase tokens are incompatible with Uniswap V3.`
return t`${reason ? reason : 'Unknown error'}. Try increasing your slippage tolerance.
Note: fee-on-transfer and rebase tokens are incompatible with Uniswap V3.`
}
}

0 comments on commit 40a45f5

Please sign in to comment.