Skip to content

Commit

Permalink
fix(eth-flow): set chainId explicitly when sending ethFlow tx (#5244)
Browse files Browse the repository at this point in the history
  • Loading branch information
alfetopito authored Dec 20, 2024
1 parent bf2b08d commit 983536d
Showing 1 changed file with 21 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export async function signEthFlowOrderStep(
orderId: string,
orderParams: PostOrderParams,
ethFlowContract: CoWSwapEthFlow,
addInFlightOrderId: (orderId: string) => void
addInFlightOrderId: (orderId: string) => void,
): Promise<EthFlowResponse> {
logTradeFlow('ETH FLOW', '[EthFlow::SignEthFlowOrderStep] - signing orderParams onchain', orderParams)

Expand All @@ -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)
Expand Down

0 comments on commit 983536d

Please sign in to comment.