Commit 3acbcbc6 authored by Zach Pomerantz's avatar Zach Pomerantz Committed by GitHub

fix: err and warn if user modifies tx (#5210)

* fix: err and warn if user modifies tx

* fix: update message

* fix: clarify swap error name
parent 7b086848
...@@ -29,6 +29,8 @@ interface FailedCall extends SwapCallEstimate { ...@@ -29,6 +29,8 @@ interface FailedCall extends SwapCallEstimate {
error: Error error: Error
} }
class InvalidSwapError extends Error {}
// returns a function that will execute a swap, if the parameters are all valid // returns a function that will execute a swap, if the parameters are all valid
export default function useSendSwapTransaction( export default function useSendSwapTransaction(
account: string | null | undefined, account: string | null | undefined,
...@@ -114,6 +116,11 @@ export default function useSendSwapTransaction( ...@@ -114,6 +116,11 @@ export default function useSendSwapTransaction(
...(value && !isZero(value) ? { value } : {}), ...(value && !isZero(value) ? { value } : {}),
}) })
.then((response) => { .then((response) => {
if (calldata !== response.data) {
throw new InvalidSwapError(
t`Your swap was modified through your wallet. If this was a mistake, please cancel immediately or risk losing your funds.`
)
}
return response return response
}) })
.catch((error) => { .catch((error) => {
...@@ -124,7 +131,11 @@ export default function useSendSwapTransaction( ...@@ -124,7 +131,11 @@ export default function useSendSwapTransaction(
// otherwise, the error was unexpected and we need to convey that // otherwise, the error was unexpected and we need to convey that
console.error(`Swap failed`, error, address, calldata, value) console.error(`Swap failed`, error, address, calldata, value)
throw new Error(t`Swap failed: ${swapErrorToUserReadableMessage(error)}`) if (error instanceof InvalidSwapError) {
throw error
} else {
throw new Error(t`Swap failed: ${swapErrorToUserReadableMessage(error)}`)
}
} }
}) })
}, },
......
...@@ -59,7 +59,6 @@ export function swapErrorToUserReadableMessage(error: any): ReactNode { ...@@ -59,7 +59,6 @@ export function swapErrorToUserReadableMessage(error: any): ReactNode {
) )
default: default:
if (reason?.indexOf('undefined is not an object') !== -1) { if (reason?.indexOf('undefined is not an object') !== -1) {
console.error(error, reason)
return ( return (
<Trans> <Trans>
An error occurred when trying to execute this swap. You may need to increase your slippage tolerance. If An error occurred when trying to execute this swap. You may need to increase your slippage tolerance. If
......
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