tornado-pool-relayer/src/services/gas-price.service.ts

40 lines
1.1 KiB
TypeScript
Raw Normal View History

import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
2021-10-11 18:06:41 +02:00
import { BigNumber } from 'ethers';
import { GasPriceOracle } from 'gas-price-oracle';
import { ChainId } from '@/types';
import { RPC_LIST, numbers } from '@/constants';
2021-10-11 18:06:41 +02:00
import { toWei } from '@/utilities';
@Injectable()
export class GasPriceService {
private readonly chainId: number;
constructor(private configService: ConfigService) {
this.chainId = this.configService.get<number>('base.chainId');
}
2021-10-11 18:06:41 +02:00
async getGasPrice() {
const TIMER = 3;
const INTERVAL = TIMER * numbers.SECOND;
const instance = new GasPriceOracle({
timeout: INTERVAL,
2021-10-11 18:06:41 +02:00
defaultRpc: RPC_LIST[ChainId.XDAI],
});
2021-10-11 18:06:41 +02:00
const fast = await instance.fetchGasPriceFromRpc();
2021-10-11 18:06:41 +02:00
const bnGas = BigNumber.from(toWei(String(fast), 'gwei'));
return {
2021-10-13 17:18:24 +02:00
instant: bnGas.mul(150).div(100).toHexString(),
fast: bnGas.mul(130).div(100).toHexString(),
2021-10-11 18:06:41 +02:00
standard: bnGas.mul(85).div(100).toHexString(),
low: bnGas.mul(50).div(100).toHexString(),
};
}
}