Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Estimate gas limit in MayachainAction #1302

Merged
merged 2 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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