From 2da3bf094fe30744fdfeb6207238ea29e831fbe0 Mon Sep 17 00:00:00 2001 From: Theo Date: Fri, 18 Aug 2023 23:01:18 -0700 Subject: [PATCH] Add function to get gas price as hex string --- src/feeOracle.ts | 18 +++++++++++++++++- src/feeOracleV4.ts | 4 ++-- src/types.ts | 1 + src/utils.ts | 2 +- 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/feeOracle.ts b/src/feeOracle.ts index a0ca88f..55fd9f0 100644 --- a/src/feeOracle.ts +++ b/src/feeOracle.ts @@ -60,7 +60,7 @@ export abstract class TornadoFeeOracle implements ITornadoFeeOracle { * @param {TxType} type Tornado transaction type (to select correct default bump percent) * @param {LegacyGasPriceKey} speed Preferred transaction speed, if uses legacy gas (before EIP-1559) * @param {number} bumpPercent Gas bump percent to prioritize transaction - * @returns {GasPrice} Estimated gas price info in WEI (hexed) - legacy object with gasPrice property or EIP-1559 object with maxFeePerGas + * @returns {Promise} Estimated gas price info in WEI (hexed) - legacy object with gasPrice property or EIP-1559 object with maxFeePerGas * and maxPriorityFeePerGas properties */ async getGasPrice( @@ -75,6 +75,22 @@ export abstract class TornadoFeeOracle implements ITornadoFeeOracle { } } + /** + * Estimate next block gas price + * @param {TxType} type Tornado transaction type (to select correct default bump percent) + * @param {LegacyGasPriceKey} speed Preferred transaction speed, if uses legacy gas (before EIP-1559) + * @param {number} bumpPercent Gas bump percent to prioritize transaction + * @returns {Promise} Gas price in WEI (hex string) + */ + async getGasPriceInHex( + type: TxType = 'other', + speed: LegacyGasPriceKey = 'fast', + bumpPercent: number = 0, + ): Promise { + const gasPrice = await this.getGasPrice(type, speed, bumpPercent); + return calculateGasPriceInWei(gasPrice).toHexString(); + } + /** * Estimates gas limit for transaction (or basic gas limit, if no tx data provided) * @param {TransactionData} tx Transaction data (object in web3 / ethers format) diff --git a/src/feeOracleV4.ts b/src/feeOracleV4.ts index 1f1fb75..4ce7069 100644 --- a/src/feeOracleV4.ts +++ b/src/feeOracleV4.ts @@ -17,12 +17,12 @@ export class TornadoFeeOracleV4 extends TornadoFeeOracle implements ITornadoFeeO super(chainId, rpcUrl, gasPriceOracle); } - async getGasLimit(tx?: TransactionData, type: TxType = 'other'): Promise { + async getGasLimit(tx?: TransactionData, type: TxType = 'other', bumpPercent?: number): Promise { if (type === 'user_withdrawal') return BigNumber.from(defaultWithdrawalGasLimit[this.chainId]); // Need to bump relayer gas limit for transaction, because predefined gas limit to small to be 100% sure that transaction will be sent // This leads to fact that relayer often pays extra for gas from his own funds, however, this was designed by previous developers - if (type === 'relayer_withdrawal') return bump(defaultWithdrawalGasLimit[this.chainId], 20); + if (type === 'relayer_withdrawal') return bump(defaultWithdrawalGasLimit[this.chainId], bumpPercent || 20); if (!tx) return BigNumber.from(21_000); return this.provider.estimateGas(tx); diff --git a/src/types.ts b/src/types.ts index 3d501ce..0f70e53 100644 --- a/src/types.ts +++ b/src/types.ts @@ -28,6 +28,7 @@ export interface TransactionData { export interface ITornadoFeeOracle { getGas: (tx?: TransactionData, type?: TxType) => Promise; getGasPrice: (type?: TxType, speed?: LegacyGasPriceKey, bumpPercent?: number) => Promise; + getGasPriceInHex: (type?: TxType, speed?: LegacyGasPriceKey, bumpPercent?: number) => Promise; getGasLimit: (tx?: TransactionData, type?: TxType, bumpPercent?: number) => Promise; calculateRefundInETH: () => Promise; calculateWithdrawalFeeViaRelayer: ( diff --git a/src/utils.ts b/src/utils.ts index efa38fd..0ed4102 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -15,7 +15,7 @@ export function serializeTx(tx?: TransactionData | string): string { export function calculateGasPriceInWei(gasPrice: GasPrice): BigNumber { // @ts-ignore - return gasPrice.gasPrice || gasPrice.maxFeePerGas; + return BigNumber.from(gasPrice.gasPrice || gasPrice.maxFeePerGas); } export function bump(value: BigNumberish, percent: number): BigNumber {