tornado-oracles/src/feeOracleV5.ts

76 lines
2.5 KiB
TypeScript
Raw Normal View History

import { ChainId } from './config';
2023-08-18 00:38:38 +03:00
import { TornadoFeeOracle } from './feeOracle';
import {
ITornadoFeeOracle,
TransactionData,
TxType,
LegacyGasPrices,
LegacyGasPriceKey,
GasPriceParams,
} from './types';
2023-08-18 00:38:38 +03:00
import { GasPriceOracle } from '@tornado/gas-price-oracle';
import { bump } from './utils';
import { TornadoFeeOracleV4 } from './feeOracleV4';
2023-08-18 00:38:38 +03:00
export class TornadoFeeOracleV5 extends TornadoFeeOracle implements ITornadoFeeOracle {
private fallbackFeeOracle: TornadoFeeOracleV4;
2023-08-18 00:38:38 +03:00
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);
2023-08-18 00:38:38 +03:00
}
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);
2023-08-18 00:38:38 +03:00
/* 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();
2023-08-18 00:38:38 +03:00
} catch (e) {
return this.fallbackFeeOracle.getGasLimit(tx, type, bumpPercent);
2023-08-18 00:38:38 +03:00
}
}
async getGasPriceParams(
2023-08-18 00:38:38 +03:00
type: TxType = 'other',
speed: LegacyGasPriceKey = 'fast',
bumpPercent?: number,
): Promise<GasPriceParams> {
2023-08-18 00:38:38 +03:00
// 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);
2023-08-18 00:38:38 +03:00
}
}