tornado-oracles/src/feeOracleV5.ts
Theo 3c1352ea41 Update to stable version 1.0.0:
- Change functions naming to more intuitive: 'getGasPrice' returns gas price in hexed number, 'getGasPriceParams' now returns legacy or EIP-1559 gas parameters
- The library has become more versatile for working with third-party libraries (without BigNumber from 'ethers' library in return values, only hexed string-numbers)
- Simplify functions for refund calculations, no need to provide transaction type, because we need to calculate refund only on user side
- Add interface for TokenPriceOracle and change typings
2023-08-22 06:40:18 -07:00

76 lines
2.5 KiB
TypeScript

import { ChainId } from './config';
import { TornadoFeeOracle } from './feeOracle';
import {
ITornadoFeeOracle,
TransactionData,
TxType,
LegacyGasPrices,
LegacyGasPriceKey,
GasPriceParams,
} from './types';
import { GasPriceOracle } from '@tornado/gas-price-oracle';
import { bump } from './utils';
import { TornadoFeeOracleV4 } from './feeOracleV4';
export class TornadoFeeOracleV5 extends TornadoFeeOracle implements ITornadoFeeOracle {
private fallbackFeeOracle: TornadoFeeOracleV4;
public constructor(chainId: number, rpcUrl: string, defaultGasPrices?: LegacyGasPrices) {
const oracleConfig = {
chainId,
defaultRpc: rpcUrl,
minPriority: chainId === 1 || chainId === 5 ? 2 : 0.05,
percentile: 5,
blocksCount: 20,
defaultFallbackGasPrices: defaultGasPrices,
};
const gasPriceOracle = new GasPriceOracle(oracleConfig);
super(chainId, rpcUrl, gasPriceOracle);
this.fallbackFeeOracle = new TornadoFeeOracleV4(chainId, rpcUrl, defaultGasPrices);
}
async getGasLimit(tx?: TransactionData, type: TxType = 'other', bumpPercent: number = 20): Promise<number> {
if (!tx || Object.keys(tx).length === 0) return this.fallbackFeeOracle.getGasLimit(tx, type, bumpPercent);
/* Relayer gas limit must be lower so that fluctuations in gas price cannot lead to the fact that
* the relayer will actually pay for gas more than the money allocated for this by the user
* (that is, in fact, relayer will pay for gas from his own money, unless we make the bump percent less for him)
*/
if (type === 'relayer_withdrawal') bumpPercent = 10;
if (type === 'user_withdrawal') bumpPercent = 30;
try {
const fetchedGasLimit = await this.provider.estimateGas(tx);
return bump(fetchedGasLimit, bumpPercent).toNumber();
} catch (e) {
return this.fallbackFeeOracle.getGasLimit(tx, type, bumpPercent);
}
}
async getGasPriceParams(
type: TxType = 'other',
speed: LegacyGasPriceKey = 'fast',
bumpPercent?: number,
): Promise<GasPriceParams> {
// Only if bump percent didn't provided (if user provides 0, no need to recalculate)
if (bumpPercent === undefined) {
switch (this.chainId) {
case ChainId.GOERLI:
bumpPercent = type === 'user_withdrawal' ? 100 : 50;
break;
case ChainId.POLYGON:
case ChainId.AVAX:
case ChainId.XDAI:
bumpPercent = 30;
break;
default:
bumpPercent = 10;
}
}
return super.getGasPriceParams(type, speed, bumpPercent);
}
}