Skip to content

Commit

Permalink
refactor: return swap errors as string (#318)
Browse files Browse the repository at this point in the history
  • Loading branch information
zzmp authored Dec 5, 2022
1 parent 8be7f02 commit b3b9dcd
Showing 1 changed file with 20 additions and 48 deletions.
68 changes: 20 additions & 48 deletions src/utils/swapErrorToUserReadableMessage.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { Trans } from '@lingui/macro'
import { ReactNode } from 'react'
import { t } from '@lingui/macro'
import { ErrorCode } from 'constants/eip1193'
/**
* This is hacking out the revert reason from the ethers provider thrown error however it can.
* This object seems to be undocumented by ethers.
* @param error an error from the ethers provider
*/
export function swapErrorToUserReadableMessage(error: any): ReactNode {
export function swapErrorToUserReadableMessage(error: any): string {
if (error.code) {
if (error.code === ErrorCode.USER_REJECTED_REQUEST) {
return t`transaction rejected.`
}
}

let reason: string | undefined
while (Boolean(error)) {
reason = error.reason ?? error.message ?? reason
Expand All @@ -16,63 +22,29 @@ export function swapErrorToUserReadableMessage(error: any): ReactNode {

switch (reason) {
case 'UniswapV2Router: EXPIRED':
return (
<Trans>
The transaction could not be sent because the deadline has passed. Please check that your transaction deadline
is not too low.
</Trans>
)
return t`The 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 (
<Trans>
This transaction will not succeed either due to price movement or fee on transfer. Try increasing your
slippage tolerance.
</Trans>
)
return t`This transaction will not succeed either due to price movement or fee on transfer. Try increasing your slippage tolerance.`
case 'TransferHelper: TRANSFER_FROM_FAILED':
return <Trans>The input token cannot be transferred. There may be an issue with the input token.</Trans>
return t`The input token cannot be transferred. There may be an issue with the input token.`
case 'UniswapV2: TRANSFER_FAILED':
return <Trans>The output token cannot be transferred. There may be an issue with the output token.</Trans>
return t`The output token cannot be transferred. There may be an issue with the output token.`
case 'UniswapV2: K':
return (
<Trans>
The Uniswap invariant x*y=k was not satisfied by the swap. This usually means one of the tokens you are
swapping incorporates custom behavior on transfer.
</Trans>
)
return t`The Uniswap invariant x*y=k was not satisfied by the swap. This usually means one of the tokens you are swapping incorporates custom behavior on transfer.`
case 'Too little received':
case 'Too much requested':
case 'STF':
return (
<Trans>
This transaction will not succeed due to price movement. Try increasing your slippage tolerance. Note: fee on
transfer and rebase tokens are incompatible with Uniswap V3.
</Trans>
)
return t`This transaction will not succeed due to price movement. Try increasing your slippage tolerance. Note: fee on transfer and rebase tokens are incompatible with Uniswap V3.`
case 'TF':
return (
<Trans>
The output token cannot be transferred. There may be an issue with the output token. Note: fee on transfer and
rebase tokens are incompatible with Uniswap V3.
</Trans>
)
return t`The output token cannot be transferred. There may be an issue with the output token. Note: fee on transfer and rebase tokens are incompatible with Uniswap V3.`
default:
if (reason?.indexOf('undefined is not an object') !== -1) {
console.error(error, reason)
return (
<Trans>
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.
</Trans>
)
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 (
<Trans>
Unknown error{reason ? `: "${reason}"` : ''}. Try increasing your slippage tolerance. Note: fee on transfer
and rebase tokens are incompatible with Uniswap V3.
</Trans>
)
return t`Unknown error${
reason ? `: "${reason}"` : ''
}. Try increasing your slippage tolerance. Note: fee on transfer and rebase tokens are incompatible with Uniswap V3.`
}
}

1 comment on commit b3b9dcd

@vercel
Copy link

@vercel vercel bot commented on b3b9dcd Dec 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

widgets – ./

widgets-uniswap.vercel.app
widgets-seven-tau.vercel.app
widgets-git-main-uniswap.vercel.app

Please sign in to comment.