From 7f1f68399a56758b16fff5172f1b6bc1dd3c078d Mon Sep 17 00:00:00 2001 From: Dan J Miller Date: Fri, 22 Jan 2021 17:44:57 -0330 Subject: [PATCH] Do not publish swaps transaction if the estimateGas call made when adding the tx fails. (#9947) --- ui/app/ducks/swaps/swaps.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/ui/app/ducks/swaps/swaps.js b/ui/app/ducks/swaps/swaps.js index 0278df774..bd029d4ef 100644 --- a/ui/app/ducks/swaps/swaps.js +++ b/ui/app/ducks/swaps/swaps.js @@ -694,6 +694,21 @@ export const signAndSendTransactions = (history, metaMetricsEvent) => { addUnapprovedTransaction(usedTradeTxParams, 'metamask'), ) dispatch(setTradeTxId(tradeTxMeta.id)) + + // The simulationFails property is added during the transaction controllers + // addUnapprovedTransaction call if the estimateGas call fails. In cases + // when no approval is required, this indicates that the swap will likely + // fail. There was an earlier estimateGas call made by the swaps controller, + // but it is possible that external conditions have change since then, and + // a previously succeeding estimate gas call could now fail. By checking for + // the `simulationFails` property here, we can reduce the number of swap + // transactions that get published to the blockchain only to fail and thereby + // waste the user's funds on gas. + if (!approveTxParams && tradeTxMeta.simulationFails) { + await dispatch(setSwapsErrorKey(SWAP_FAILED_ERROR)) + history.push(SWAPS_ERROR_ROUTE) + return + } const finalTradeTxMeta = await dispatch( updateTransaction( {