Add function to get gas price as hex string

This commit is contained in:
Theo 2023-08-18 23:01:18 -07:00
parent b7769d4548
commit 2da3bf094f
4 changed files with 21 additions and 4 deletions

@ -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<GasPrice>} 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<string>} Gas price in WEI (hex string)
*/
async getGasPriceInHex(
type: TxType = 'other',
speed: LegacyGasPriceKey = 'fast',
bumpPercent: number = 0,
): Promise<string> {
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)

@ -17,12 +17,12 @@ export class TornadoFeeOracleV4 extends TornadoFeeOracle implements ITornadoFeeO
super(chainId, rpcUrl, gasPriceOracle);
}
async getGasLimit(tx?: TransactionData, type: TxType = 'other'): Promise<BigNumber> {
async getGasLimit(tx?: TransactionData, type: TxType = 'other', bumpPercent?: number): Promise<BigNumber> {
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);

@ -28,6 +28,7 @@ export interface TransactionData {
export interface ITornadoFeeOracle {
getGas: (tx?: TransactionData, type?: TxType) => Promise<BigNumber>;
getGasPrice: (type?: TxType, speed?: LegacyGasPriceKey, bumpPercent?: number) => Promise<GasPrice>;
getGasPriceInHex: (type?: TxType, speed?: LegacyGasPriceKey, bumpPercent?: number) => Promise<string>;
getGasLimit: (tx?: TransactionData, type?: TxType, bumpPercent?: number) => Promise<BigNumber>;
calculateRefundInETH: () => Promise<BigNumber>;
calculateWithdrawalFeeViaRelayer: (

@ -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 {