From d2877b3e46705c89658d301d3b1881e1f6706803 Mon Sep 17 00:00:00 2001 From: 0xp3gasus <0xp3gasus@proton.me> Date: Tue, 8 Oct 2024 19:29:56 +0200 Subject: [PATCH 1/2] Estimate gas limit --- .changeset/angry-roses-sit.md | 5 +++++ packages/xchain-wallet/package.json | 3 ++- packages/xchain-wallet/src/wallet.ts | 29 ++++++++++++++++++++++++++++ yarn.lock | 1 + 4 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 .changeset/angry-roses-sit.md diff --git a/.changeset/angry-roses-sit.md b/.changeset/angry-roses-sit.md new file mode 100644 index 000000000..de8609f98 --- /dev/null +++ b/.changeset/angry-roses-sit.md @@ -0,0 +1,5 @@ +--- +'@xchainjs/xchain-wallet': minor +--- + +Estimate gas limit for EVM clients diff --git a/packages/xchain-wallet/package.json b/packages/xchain-wallet/package.json index 9083949c1..9282b2b14 100644 --- a/packages/xchain-wallet/package.json +++ b/packages/xchain-wallet/package.json @@ -35,7 +35,8 @@ "@xchainjs/xchain-radix": "workspace:*", "@xchainjs/xchain-thorchain": "workspace:*", "@xchainjs/xchain-util": "workspace:*", - "@xchainjs/xchain-utxo": "workspace:*" + "@xchainjs/xchain-utxo": "workspace:*", + "ethers": "5.7.2" }, "devDependencies": { "@xchainjs/xchain-bitcoin": "workspace:*", diff --git a/packages/xchain-wallet/src/wallet.ts b/packages/xchain-wallet/src/wallet.ts index 3c81c8606..4504c42d3 100644 --- a/packages/xchain-wallet/src/wallet.ts +++ b/packages/xchain-wallet/src/wallet.ts @@ -26,6 +26,7 @@ import { getContractAddressFromAsset, } from '@xchainjs/xchain-util' import { Client as UtxoClient } from '@xchainjs/xchain-utxo' +import { BigNumber } from 'ethers' import { ChainBalances, CosmosTxParams, EvmTxParams, RadixTxParams, UtxoTxParams } from './types' @@ -373,6 +374,34 @@ export class Wallet { return client.getFees() } + /** + * Estimates gas limit for a transaction. + * + * @param {TxParams} params The transaction and fees options. + * @returns {BigNumber} The estimated gas limit. + */ + public async estimateGasLimit({ + asset, + recipient, + amount, + memo, + from, + isMemoEncoded, + }: EvmTxParams & { from?: Address }): Promise { + const client = this.getClient(asset.chain) + if (!this.isEvmClient(client)) { + throw Error(`estimateGasLimit method not supported in ${asset.chain} chain`) + } + return client.estimateGasLimit({ + asset, + recipient, + amount, + memo, + from, + isMemoEncoded, + }) + } + /** * Make a transaction * @param {UtxoTxParams | EvmTxParams | CosmosTxParams} params txParams - The parameters to make the transfer diff --git a/yarn.lock b/yarn.lock index fafee5c3c..82476a591 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4836,6 +4836,7 @@ __metadata: "@xchainjs/xchain-thorchain": "workspace:*" "@xchainjs/xchain-util": "workspace:*" "@xchainjs/xchain-utxo": "workspace:*" + ethers: "npm:5.7.2" languageName: unknown linkType: soft From 5a23369da28c54d675ce7b8c993d54b648f4e53f Mon Sep 17 00:00:00 2001 From: 0xp3gasus <0xp3gasus@proton.me> Date: Tue, 8 Oct 2024 19:33:25 +0200 Subject: [PATCH 2/2] Estimate gas limit before router contract call --- .changeset/slow-starfishes-add.md | 5 +++++ .../xchain-mayachain-amm/src/mayachain-action.ts | 12 +++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) create mode 100644 .changeset/slow-starfishes-add.md diff --git a/.changeset/slow-starfishes-add.md b/.changeset/slow-starfishes-add.md new file mode 100644 index 000000000..504235978 --- /dev/null +++ b/.changeset/slow-starfishes-add.md @@ -0,0 +1,5 @@ +--- +'@xchainjs/xchain-mayachain-amm': patch +--- + +Estimate gas limit before calling Router contract in MayachainAction diff --git a/packages/xchain-mayachain-amm/src/mayachain-action.ts b/packages/xchain-mayachain-amm/src/mayachain-action.ts index 428fe615f..4853af300 100644 --- a/packages/xchain-mayachain-amm/src/mayachain-action.ts +++ b/packages/xchain-mayachain-amm/src/mayachain-action.ts @@ -12,7 +12,7 @@ import { getContractAddressFromAsset, isSynthAsset, } from '@xchainjs/xchain-util' -import { Wallet } from '@xchainjs/xchain-wallet' +import { EvmTxParams, Wallet } from '@xchainjs/xchain-wallet' import { ethers } from 'ethers' import { TxSubmitted } from './types' @@ -144,14 +144,20 @@ export class MayachainAction { const nativeAsset = wallet.getAssetInfo(assetAmount.asset.chain) - const hash = await wallet.transfer({ + const tx: EvmTxParams = { asset: nativeAsset.asset, amount: isERC20 ? baseAmount(0, nativeAsset.decimal) : assetAmount.baseAmount, memo: unsignedTx.data, recipient: inboundDetails.router, gasPrice: gasPrices.fast, isMemoEncoded: true, - gasLimit: ethers.BigNumber.from(160000), + } + + const gasLimit = await wallet.estimateGasLimit(tx) + + const hash = await wallet.transfer({ + ...tx, + gasLimit, }) return {