forked from Uniswap/interface
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswapErrorToUserReadableMessage.tsx
More file actions
78 lines (72 loc) · 3.36 KB
/
Copy pathswapErrorToUserReadableMessage.tsx
File metadata and controls
78 lines (72 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { t } from '@lingui/macro'
import { UserRejectedRequestError } from './errors'
function getReason(error: any): string | undefined {
let reason: string | undefined
while (error) {
reason = error.reason ?? error.message ?? reason
error = error.error ?? error.data?.originalError
}
return reason
}
export function didUserReject(error: any): boolean {
const reason = getReason(error)
if (
error?.code === 4001 ||
// ethers v5.7.0 wrapped error
error?.code === 'ACTION_REJECTED' ||
// For Rainbow :
(reason?.match(/request/i) && reason?.match(/reject/i)) ||
// For Frame:
reason?.match(/declined/i) ||
// For SafePal:
reason?.match(/cancell?ed by user/i) ||
// For Trust:
reason?.match(/user cancell?ed/i) ||
// For Coinbase:
reason?.match(/user denied/i) ||
// For Fireblocks
reason?.match(/user rejected/i) ||
error instanceof UserRejectedRequestError
) {
return true
}
return false
}
/**
* 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): string {
if (didUserReject(error)) {
return t`Transaction rejected`
}
let reason = getReason(error)
if (reason?.indexOf('execution reverted: ') === 0) reason = reason.substr('execution reverted: '.length)
switch (reason) {
case 'UniswapV2Router: EXPIRED':
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.`
case 'TransferHelper: TRANSFER_FROM_FAILED':
return t`The input token cannot be transferred. There may be an issue with the input token.`
case 'UniswapV2: TRANSFER_FAILED':
return t`The output token cannot be transferred. There may be an issue with the output token.`
case 'UniswapV2: K':
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 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 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 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`${reason ? reason : 'Unknown error.'} Try increasing your slippage tolerance.
Note: fee-on-transfer and rebase tokens are incompatible with Uniswap V3.`
}
}