2023-08-22 16:39:59 +03:00
|
|
|
import { ChainId } from './config';
|
2023-08-18 00:38:38 +03:00
|
|
|
import { TornadoFeeOracle } from './feeOracle';
|
2023-08-22 16:39:59 +03:00
|
|
|
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';
|
2023-08-21 19:30:24 +03:00
|
|
|
import { TornadoFeeOracleV4 } from './feeOracleV4';
|
2023-08-18 00:38:38 +03:00
|
|
|
|
|
|
|
export class TornadoFeeOracleV5 extends TornadoFeeOracle implements ITornadoFeeOracle {
|
2023-08-21 19:30:24 +03:00
|
|
|
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);
|
2023-08-21 19:30:24 +03:00
|
|
|
|
|
|
|
this.fallbackFeeOracle = new TornadoFeeOracleV4(chainId, rpcUrl, defaultGasPrices);
|
2023-08-18 00:38:38 +03:00
|
|
|
}
|
|
|
|
|
2023-08-22 16:39:59 +03:00
|
|
|
async getGasLimit(tx?: TransactionData, type: TxType = 'other', bumpPercent: number = 20): Promise<number> {
|
2023-08-21 19:30:24 +03:00
|
|
|
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);
|
2023-08-22 16:39:59 +03:00
|
|
|
return bump(fetchedGasLimit, bumpPercent).toNumber();
|
2023-08-18 00:38:38 +03:00
|
|
|
} catch (e) {
|
2023-08-22 16:39:59 +03:00
|
|
|
return this.fallbackFeeOracle.getGasLimit(tx, type, bumpPercent);
|
2023-08-18 00:38:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-22 16:39:59 +03:00
|
|
|
async getGasPriceParams(
|
2023-08-18 00:38:38 +03:00
|
|
|
type: TxType = 'other',
|
|
|
|
speed: LegacyGasPriceKey = 'fast',
|
|
|
|
bumpPercent?: number,
|
2023-08-22 16:39:59 +03:00
|
|
|
): 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-22 16:39:59 +03:00
|
|
|
return super.getGasPriceParams(type, speed, bumpPercent);
|
2023-08-18 00:38:38 +03:00
|
|
|
}
|
|
|
|
}
|