import { GAS_PRICE_DEFAULT } from '@config' ; import { isEIP1559Supported } from '@helpers' ; import { ProviderHandler } from '@services/EthService' ; import { GasEstimates , ITxObject , Network , StoreAccount } from '@types' ; import { bigify , bigNumGasPriceToViewableGwei } from '@utils' ; import { estimateFees } from './eip1559' ; import { fetchGasEstimates } from './gas' ; export function getDefaultEstimates ( network ? ) { const time = Date . now ( ) ; if ( ! network ) { const gasSettings = GAS_PRICE_DEFAULT ; return { safeLow : gasSettings . min , standard : gasSettings . initial , fast : gasSettings . initial , fastest : gasSettings . max , isDefault : true , chainId : 60 , time } ; } else { const gasSettings = network . isCustom ? GAS_PRICE_DEFAULT : network . gasPriceSettings ; return { safeLow : gasSettings . min , standard : gasSettings . initial , fast : gasSettings . initial , fastest : gasSettings . max , isDefault : true , chainId : network . chainId , time } ; } } export async function fetchGasPriceEstimates ( network ? ) { if ( ! network || network . isCustom || ! network . shouldEstimateGasPrice ) { const defaultEstimates = getDefaultEstimates ( network ) ; return defaultEstimates ; } return fetchGasEstimates ( ) . catch ( ( err ) => { console . error ( err ) ; return getDefaultEstimates ( network ) ; } ) ; } export async function fetchEIP1559PriceEstimates ( network ) { const provider = new ProviderHandler ( network ) ; return estimateFees ( provider ) ; } export type UniversalGasEstimationResult = { gasPrice : string ; maxFeePerGas : undefined ; maxPriorityFeePerGas : undefined ; } | { gasPrice : undefined ; maxFeePerGas : string ; maxPriorityFeePerGas : string ; } ; export async function fetchUniversalGasPriceEstimate ( network ? , account ? ) { if ( network && account && isEIP1559Supported ( network , account ) ) { const { maxFeePerGas , maxPriorityFeePerGas , baseFee } = await fetchEIP1559PriceEstimates ( network ) ; return { baseFee , estimate : { maxFeePerGas : maxFeePerGas && bigNumGasPriceToViewableGwei ( maxFeePerGas ) , maxPriorityFeePerGas : maxPriorityFeePerGas && bigNumGasPriceToViewableGwei ( maxPriorityFeePerGas ) } } ; } const { fast } = await fetchGasPriceEstimates ( network ) ; return { estimate : { gasPrice : fast . toString ( ) } } ; } export const getGasEstimate = async ( network , tx ) => { const provider = new ProviderHandler ( network ) ; return provider . estimateGas ( tx ) . then ( bigify ) . then ( ( n ) => n . multipliedBy ( 1.2 ) . integerValue ( 7 ) . toString ( 10 ) ) ; } ;