Skip to content

Commit

Permalink
Merge pull request #1302 from xchainjs/estimate-gas-limit-maya
Browse files Browse the repository at this point in the history
Estimate gas limit in MayachainAction
  • Loading branch information
0xp3gasus authored Oct 9, 2024
2 parents ffd7c09 + 5a23369 commit 460f53d
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/angry-roses-sit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@xchainjs/xchain-wallet': minor
---

Estimate gas limit for EVM clients
5 changes: 5 additions & 0 deletions .changeset/slow-starfishes-add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@xchainjs/xchain-mayachain-amm': patch
---

Estimate gas limit before calling Router contract in MayachainAction
12 changes: 9 additions & 3 deletions packages/xchain-mayachain-amm/src/mayachain-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion packages/xchain-wallet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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:*",
Expand Down
29 changes: 29 additions & 0 deletions packages/xchain-wallet/src/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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<BigNumber> {
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
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 460f53d

Please sign in to comment.