nova-ui/services/gasOracle/oracle.ts

61 lines
1.6 KiB
TypeScript
Raw Permalink Normal View History

import { GasPriceOracle } from '@tornado/gas-price-oracle'
import { GasPrice } from '@/types'
2022-12-04 09:02:30 +03:00
import { ChainId } from '@/types'
import { numbers, RPC_LIST } from '@/constants'
import { getProvider } from '@/services'
import { gweiToWei, bump } from '@/utilities'
const SECONDS = 3
const TEN_SECOND = SECONDS * numbers?.SECOND
const percentBump = {
INSTANT: 150,
FAST: 130,
STANDARD: 85,
LOW: 50,
}
const getGasPrice = async (chainId: ChainId): Promise<GasPrice> => {
if (chainId === ChainId.BSC) {
return await getGasPriceFromRpc(chainId)
}
const instance = new GasPriceOracle({
chainId,
timeout: TEN_SECOND,
defaultRpc: RPC_LIST[chainId],
})
const result = (await instance.gasPrices({ isLegacy: true })) as GasPrice
2022-12-04 09:02:30 +03:00
if (chainId === ChainId.XDAI || chainId === ChainId.MAINNET) {
return {
instant: bump(gweiToWei(result.instant), percentBump.INSTANT),
fast: bump(gweiToWei(result.instant), percentBump.FAST),
standard: bump(gweiToWei(result.standard), percentBump.STANDARD),
low: bump(gweiToWei(result.low), percentBump.LOW),
}
}
return result
}
const getGasPriceFromRpc = async (chainId: ChainId) => {
try {
const { provider } = getProvider(chainId)
const current = await provider.getGasPrice()
return {
instant: bump(current, percentBump.INSTANT),
fast: bump(current, percentBump.FAST),
standard: bump(current, percentBump.STANDARD),
low: bump(current, percentBump.LOW),
}
} catch (err) {
throw new Error('getGasPriceFromRpc has error')
}
}
export { getGasPrice, getGasPriceFromRpc }