Skip to content

Commit

Permalink
feat(abstract-eth): fetch gasPrice and gasLimit from explorer
Browse files Browse the repository at this point in the history
TICKET: COIN-1201
  • Loading branch information
venkateshv1266 committed Sep 12, 2024
1 parent b293ed9 commit ab0299b
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 19 deletions.
107 changes: 89 additions & 18 deletions modules/abstract-eth/src/abstractEthLikeNewCoins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1520,10 +1520,14 @@ export abstract class AbstractEthLikeNewCoins extends AbstractEthLikeCoin {
}
}

const gasLimit = new optionalDeps.ethUtil.BN(this.setGasLimit(params.gasLimit));
// If gasLimit is not passed as a param, then default gasLimit 50000 will be set here
let gasLimit = new optionalDeps.ethUtil.BN(this.setGasLimit(params.gasLimit));

const gasPrice = params.eip1559
? new optionalDeps.ethUtil.BN(params.eip1559.maxFeePerGas)
: new optionalDeps.ethUtil.BN(this.setGasPrice(params.gasPrice));
: params.gasPrice
? new optionalDeps.ethUtil.BN(this.setGasPrice(params.gasPrice))
: await this.getGasPriceFromExternalAPI();

const bitgoFeeAddressNonce = await this.getAddressNonce(bitgoFeeAddress);

Expand Down Expand Up @@ -1588,14 +1592,6 @@ export abstract class AbstractEthLikeNewCoins extends AbstractEthLikeCoin {
await new Promise((resolve) => setTimeout(resolve, 1000));
const sequenceId = await this.querySequenceId(walletContractAddress);

const txInfo = {
recipients: recipients,
expireTime: this.getDefaultExpireTime(),
contractSequenceId: sequenceId,
gasLimit: gasLimit.toString(10),
isEvmBasedCrossChainRecovery: true,
};

const network = this.getNetwork();
const batcherContractAddress = network?.batcherContractAddress as string;

Expand Down Expand Up @@ -1646,8 +1642,30 @@ export abstract class AbstractEthLikeNewCoins extends AbstractEthLikeCoin {
txBuilder.walletVersion(4);
}

// If gasLimit was not passed as a param, then fetch the gasLimit from Explorer
if (!params.gasLimit) {
const sendData = txBuilder.getSendData();
gasLimit = await this.getGasLimitFromExternalAPI(
params.bitgoFeeAddress as string,
params.walletContractAddress,
sendData
);
txBuilder.fee({
...txFee,
gasLimit: gasLimit.toString(),
});
}

const tx = await txBuilder.build();

const txInfo = {
recipients: recipients,
expireTime: this.getDefaultExpireTime(),
contractSequenceId: sequenceId,
gasLimit: gasLimit.toString(10),
isEvmBasedCrossChainRecovery: true,
};

const response: OfflineVaultTxInfo = {
txHex: tx.toBroadcastFormat(),
userKey,
Expand Down Expand Up @@ -1740,14 +1758,6 @@ export abstract class AbstractEthLikeNewCoins extends AbstractEthLikeCoin {
await new Promise((resolve) => setTimeout(resolve, 1000));
const sequenceId = await this.querySequenceId(params.walletContractAddress);

const txInfo = {
recipients: recipients,
expireTime: this.getDefaultExpireTime(),
contractSequenceId: sequenceId,
gasLimit: gasLimit.toString(10),
isEvmBasedCrossChainRecovery: true,
};

const txBuilder = this.getTransactionBuilder(params.common) as TransactionBuilder;
txBuilder.counter(bitgoFeeAddressNonce);
txBuilder.contract(params.walletContractAddress as string);
Expand Down Expand Up @@ -1799,8 +1809,29 @@ export abstract class AbstractEthLikeNewCoins extends AbstractEthLikeCoin {
txBuilder.walletVersion(4);
}

if (!params.gasLimit) {
const sendData = txBuilder.getSendData();
gasLimit = await this.getGasLimitFromExternalAPI(
params.bitgoFeeAddress as string,
params.walletContractAddress,
sendData
);
txBuilder.fee({
...txFee,
gasLimit: gasLimit.toString(),
});
}

const tx = await txBuilder.build();

const txInfo = {
recipients: recipients,
expireTime: this.getDefaultExpireTime(),
contractSequenceId: sequenceId,
gasLimit: gasLimit.toString(10),
isEvmBasedCrossChainRecovery: true,
};

const response: OfflineVaultTxInfo = {
txHex: tx.toBroadcastFormat(),
userKey,
Expand Down Expand Up @@ -2599,4 +2630,44 @@ export abstract class AbstractEthLikeNewCoins extends AbstractEthLikeCoin {
}
}
}

/**
* Fetch the gas price from the explorer
*/
async getGasPriceFromExternalAPI(): Promise<BN> {
try {
const res = await this.recoveryBlockchainExplorerQuery({
module: 'proxy',
action: 'eth_gasPrice',
});
const gasPrice = new BN(res.result.slice(2), 16);
console.log(` Got gas price: ${gasPrice}`);
return gasPrice;
} catch (e) {
throw new Error('Failed to get gas price');
}
}

/**
* Fetch the gas limit from the explorer
* @param from
* @param to
* @param data
*/
async getGasLimitFromExternalAPI(from: string, to: string, data: string): Promise<BN> {
try {
const res = await this.recoveryBlockchainExplorerQuery({
module: 'proxy',
action: 'eth_estimateGas',
from,
to,
data,
});
const gasLimit = new BN(res.result.slice(2), 16);
console.log(`Got gas limit: ${gasLimit}`);
return gasLimit;
} catch (e) {
throw new Error('Failed to get gas limit: ');
}
}
}
2 changes: 1 addition & 1 deletion modules/abstract-eth/src/lib/transactionBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ export abstract class TransactionBuilder extends BaseTransactionBuilder {
*
* @returns {string} serialized sendMultiSig data
*/
private getSendData(): string {
public getSendData(): string {
if (!this._transfer) {
throw new BuildTransactionError('Missing transfer information');
}
Expand Down

0 comments on commit ab0299b

Please sign in to comment.