Skip to content

Commit

Permalink
fix(wrap/unwrap): enforce chainId when wrapping/unwrapping (#5349)
Browse files Browse the repository at this point in the history
  • Loading branch information
alfetopito authored Jan 27, 2025
1 parent 8eb858d commit 7023f6e
Showing 1 changed file with 32 additions and 8 deletions.
40 changes: 32 additions & 8 deletions apps/cowswap-frontend/src/legacy/hooks/useWrapCallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export interface WrapDescription {
operationMessage: string
summary: string
}

export interface WrapUnwrapContext {
chainId: SupportedChainId
wethContract: Contract
Expand All @@ -43,7 +44,7 @@ export interface WrapUnwrapContext {

export async function wrapUnwrapCallback(
context: WrapUnwrapContext,
params: WrapUnwrapCallbackParams = { useModals: true }
params: WrapUnwrapCallbackParams = { useModals: true },
): Promise<TransactionResponse | null> {
const { chainId, amount, wethContract, addTransaction, openTransactionConfirmationModal, closeModals } = context
const isNativeIn = getIsNativeToken(amount.currency)
Expand All @@ -57,7 +58,7 @@ export async function wrapUnwrapCallback(
wrapAnalytics('Send', operationMessage)

const wrapUnwrap = isNativeIn ? wrapContractCall : unwrapContractCall
const txReceipt = await wrapUnwrap(wethContract, amountHex)
const txReceipt = await wrapUnwrap(wethContract, amountHex, chainId)
wrapAnalytics('Sign', operationMessage)

addTransaction({
Expand Down Expand Up @@ -88,7 +89,7 @@ export async function wrapUnwrapCallback(
function getWrapDescription(
chainId: SupportedChainId,
isWrap: boolean,
inputAmount: CurrencyAmount<Currency>
inputAmount: CurrencyAmount<Currency>,
): WrapDescription {
const { native, wrapped } = getChainCurrencySymbols(chainId)
const baseSummarySuffix = isWrap ? `${native} to ${wrapped}` : `${wrapped} to ${native}`
Expand All @@ -104,24 +105,47 @@ function getWrapDescription(
}
}

async function wrapContractCall(wethContract: Contract, amountHex: string): Promise<TransactionResponse> {
async function wrapContractCall(
wethContract: Contract,
amountHex: string,
chainId: SupportedChainId,
): Promise<TransactionResponse> {
const estimatedGas = await wethContract.estimateGas.deposit({ value: amountHex }).catch(_handleGasEstimateError)
const gasLimit = calculateGasMargin(estimatedGas)

return wethContract.deposit({ value: amountHex, gasLimit })
const network = await wethContract.provider.getNetwork()
if (network.chainId !== chainId) {
throw new Error('Wallet chain differs from order params.')
}

const tx = await wethContract.populateTransaction.deposit({ value: amountHex, gasLimit })

return wethContract.signer.sendTransaction({ ...tx, chainId: network.chainId })
}

async function unwrapContractCall(wethContract: Contract, amountHex: string): Promise<TransactionResponse> {
async function unwrapContractCall(
wethContract: Contract,
amountHex: string,
chainId: SupportedChainId,
): Promise<TransactionResponse> {
const estimatedGas = await wethContract.estimateGas.withdraw(amountHex).catch(_handleGasEstimateError)
const gasLimit = calculateGasMargin(estimatedGas)
return wethContract.withdraw(amountHex, { gasLimit })

const tx = await wethContract.populateTransaction.withdraw(amountHex, { gasLimit })

const network = await wethContract.provider.getNetwork()
if (network.chainId !== chainId) {
throw new Error('Wallet chain differs from order params.')
}

return wethContract.signer.sendTransaction({ ...tx, chainId: network.chainId })
}

function _handleGasEstimateError(error: any): BigNumber {
console.log(
'[useWrapCallback] Error estimating gas for wrap/unwrap. Using default gas limit ' +
WRAP_UNWRAP_GAS_LIMIT_DEFAULT.toString(),
error
error,
)
return WRAP_UNWRAP_GAS_LIMIT_DEFAULT
}

0 comments on commit 7023f6e

Please sign in to comment.