From 983536d300a17e84dff691eb55d645b46ad1437b Mon Sep 17 00:00:00 2001 From: Leandro Date: Fri, 20 Dec 2024 15:40:18 +0000 Subject: [PATCH] fix(eth-flow): set chainId explicitly when sending ethFlow tx (#5244) --- .../ethFlow/steps/signEthFlowOrderStep.ts | 34 ++++++++++++------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/apps/cowswap-frontend/src/modules/swap/services/ethFlow/steps/signEthFlowOrderStep.ts b/apps/cowswap-frontend/src/modules/swap/services/ethFlow/steps/signEthFlowOrderStep.ts index b8a431e5af..1c688a320d 100644 --- a/apps/cowswap-frontend/src/modules/swap/services/ethFlow/steps/signEthFlowOrderStep.ts +++ b/apps/cowswap-frontend/src/modules/swap/services/ethFlow/steps/signEthFlowOrderStep.ts @@ -33,7 +33,7 @@ export async function signEthFlowOrderStep( orderId: string, orderParams: PostOrderParams, ethFlowContract: CoWSwapEthFlow, - addInFlightOrderId: (orderId: string) => void + addInFlightOrderId: (orderId: string) => void, ): Promise { logTradeFlow('ETH FLOW', '[EthFlow::SignEthFlowOrderStep] - signing orderParams onchain', orderParams) @@ -58,21 +58,29 @@ export async function signEthFlowOrderStep( } const ethTxOptions = { value: etherValue.quotient.toString() } - const estimatedGas = await ethFlowContract.estimateGas - .createOrder(ethOrderParams, { value: etherValue.quotient.toString() }) - .catch((error) => { - logTradeFlowError( - 'ETH FLOW', - '[EthFlow::SignEthFlowOrderStep] Error estimating createOrder gas. Using default ' + GAS_LIMIT_DEFAULT, - error - ) - return GAS_LIMIT_DEFAULT - }) - - const txReceipt = await ethFlowContract.createOrder(ethOrderParams, { + const estimatedGas = await ethFlowContract.estimateGas.createOrder(ethOrderParams, ethTxOptions).catch((error) => { + logTradeFlowError( + 'ETH FLOW', + '[EthFlow::SignEthFlowOrderStep] Error estimating createOrder gas. Using default ' + GAS_LIMIT_DEFAULT, + error, + ) + return GAS_LIMIT_DEFAULT + }) + + // This used to be done with a higher level of abstraction like this: + // const txReceipt = await ethFlowContract.createOrder(ethOrderParams, { + // ...ethTxOptions, + // gasLimit: calculateGasMargin(estimatedGas), + // }) + // However, to **try** to prevent wallet issues, we want to explicitly send along the chainId + // But that wrapper doesn't accept it. + // So we must build the tx first, then send it using the contract's signer + const tx = await ethFlowContract.populateTransaction.createOrder(ethOrderParams, { ...ethTxOptions, gasLimit: calculateGasMargin(estimatedGas), }) + const txReceipt = await ethFlowContract.signer.sendTransaction({ ...tx, chainId: orderParams.chainId }) + addInFlightOrderId(orderId) logTradeFlow('ETH FLOW', '[EthFlow::SignEthFlowOrderStep] Sent transaction onchain', orderId, txReceipt)