66 lines
2.4 KiB
TypeScript
66 lines
2.4 KiB
TypeScript
|
import { ChainId, defaultWithdrawalGasLimit } from './config';
|
||
|
import { TornadoFeeOracle } from './feeOracle';
|
||
|
import { ITornadoFeeOracle, GasPrice, TransactionData, TxType, LegacyGasPrices, LegacyGasPriceKey } from './types';
|
||
|
import { GasPriceOracle } from '@tornado/gas-price-oracle';
|
||
|
import { BigNumber } from 'ethers';
|
||
|
import { bump } from './utils';
|
||
|
|
||
|
export class TornadoFeeOracleV5 extends TornadoFeeOracle implements ITornadoFeeOracle {
|
||
|
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);
|
||
|
}
|
||
|
|
||
|
async getGasLimit(tx?: TransactionData, type: TxType = 'other', bumpPercent: number = 20): Promise<BigNumber> {
|
||
|
if (!tx) return BigNumber.from(21_000);
|
||
|
|
||
|
/* 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);
|
||
|
} catch (e) {
|
||
|
if (type.endsWith('withdrawal')) return bump(defaultWithdrawalGasLimit[this.chainId], bumpPercent);
|
||
|
return BigNumber.from(21_000);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
async getGasPrice(
|
||
|
type: TxType = 'other',
|
||
|
speed: LegacyGasPriceKey = 'fast',
|
||
|
bumpPercent?: number,
|
||
|
): Promise<GasPrice> {
|
||
|
// 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.getGasPrice(type, speed, bumpPercent);
|
||
|
}
|
||
|
}
|