Commit b2f88965 authored by Moody Salem's avatar Moody Salem

handle errors better

parent 95db44e0
......@@ -22,12 +22,6 @@ To access the Uniswap Interface, use an IPFS gateway link from the
[latest release](https://github.com/Uniswap/uniswap-interface/releases/latest),
or visit [app.uniswap.org](https://app.uniswap.org).
## Listing a token
Please see the
[@uniswap/default-token-list](https://github.com/uniswap/default-token-list)
repository.
## Development
### Install Dependencies
......
......@@ -133,6 +133,26 @@ function useSwapCallArguments(
}, [account, allowedSlippage, chainId, deadline, library, recipient, routerContract, signatureData, trade])
}
export function swapErrorToUserReadableMessage(error: { reason: string }): string {
switch (error.reason) {
case 'UniswapV2Router: EXPIRED':
return '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 'This transaction will not succeed either due to price movement or fee on transfer. Try increasing your slippage tolerance.'
case 'UniswapV2: TRANSFER_FAILED':
return 'The token could not be transferred. There may be an issue with the token.'
case 'UniswapV2: K':
return '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 'This transaction will not succeed due to price movement. Try increasing your slippage tolerance.'
default:
return 'Unknown error. Please join the Discord to get help.'
}
}
// returns a function that will execute a swap, if the parameters are all valid
// and the user has approved the slippage adjusted input amount for the trade
export function useSwapCallback(
......@@ -198,28 +218,7 @@ export function useSwapCallback(
})
.catch((callError) => {
console.debug('Call threw error', call, callError)
let errorMessage: string
switch (callError.reason) {
case 'UniswapV2Router: EXPIRED':
errorMessage =
'The transaction could not be sent because the deadline has passed. Please check that your transaction deadline is not too low.'
break
case 'UniswapV2Router: INSUFFICIENT_OUTPUT_AMOUNT':
case 'UniswapV2Router: EXCESSIVE_INPUT_AMOUNT':
errorMessage =
'This transaction will not succeed either due to price movement or fee on transfer. Try increasing your slippage tolerance.'
break
case 'UniswapV2: TRANSFER_FAILED':
errorMessage = 'The token could not be transferred. There may be an issue with the token.'
break
case 'UniswapV2: K':
errorMessage =
'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.'
break
default:
return { call }
}
return { call, error: new Error(errorMessage) }
return { call, error: new Error(swapErrorToUserReadableMessage(callError)) }
})
})
})
......@@ -289,7 +288,10 @@ export function useSwapCallback(
} else {
// otherwise, the error was unexpected and we need to convey that
console.error(`Swap failed`, error, address, calldata, value)
throw new Error(`Swap failed: ${error.message}`)
throw new Error(
`Swap failed: ${'reason' in error ? swapErrorToUserReadableMessage(error) : error.message}`
)
}
})
},
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment