Skip to content

Commit

Permalink
Fallback to standard gas prices when eth_feeHistory not available (#659)
Browse files Browse the repository at this point in the history
  • Loading branch information
mholtzman authored Dec 4, 2021
1 parent e234226 commit 9c3e672
Showing 1 changed file with 28 additions and 24 deletions.
52 changes: 28 additions & 24 deletions main/chains/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,45 +57,49 @@ class ChainConnection extends EventEmitter {
_createBlockMonitor (provider) {
const monitor = new BlockMonitor(provider)

monitor.on('data', block => {
monitor.on('data', async block => {
let feeMarket

const gasCalculator = new GasCalculator(provider)

if ('baseFeePerGas' in block) {
this.chainConfig.setHardforkByBlockNumber(block.number)
try {
// only consider this an EIP-1559 block if fee market can be loaded
feeMarket = await gasCalculator.getFeePerGas()

this.chainConfig.setHardforkByBlockNumber(block.number)

if (!this.chainConfig.gteHardfork(Hardfork.London)) {
// if baseFeePerGas is present in the block header, the hardfork
// must be at least London
this.chainConfig.setHardfork(Hardfork.London)
if (!this.chainConfig.gteHardfork(Hardfork.London)) {
// if baseFeePerGas is present in the block header, the hardfork
// must be at least London
this.chainConfig.setHardfork(Hardfork.London)
}
} catch (e) {
log.error(`could not load EIP-1559 fee market for chain ${this.chainId}`, e)
}
}

const gasCalculator = new GasCalculator(provider)
const useFeeMarket = this.chainConfig.isActivatedEIP(1559)

if (useFeeMarket) {
gasCalculator.getFeePerGas().then(fees => {
const gasPrice = parseInt(fees.maxBaseFeePerGas) + parseInt(fees.maxPriorityFeePerGas)
try {
if (feeMarket) {
const gasPrice = parseInt(feeMarket.maxBaseFeePerGas) + parseInt(feeMarket.maxPriorityFeePerGas)

store.setGasFees(this.type, this.chainId, fees)
store.setGasFees(this.type, this.chainId, feeMarket)
store.setGasPrices(this.type, this.chainId, { fast: addHexPrefix(gasPrice.toString(16)) })
store.setGasDefault(this.type, this.chainId, 'fast')

accounts.updatePendingFees(this.chainId)
}).catch(err => {
log.error(`could not update gas fees for chain ${this.chainId}`, err)
})
} else {
gasCalculator.getGasPrices().then(gas => {
} else {
const gas = await gasCalculator.getGasPrices()
const customLevel = store('main.networksMeta', this.type, this.chainId, 'gas.price.levels.custom')

store.setGasFees(this.type, this.chainId, null)
store.setGasPrices(this.type, this.chainId, {
...gas,
custom: customLevel || gas.fast
})
}

accounts.updatePendingFees(this.chainId)
}).catch(err => {
log.error(`could not update gas prices for chain ${this.chainId}`, err)
})
accounts.updatePendingFees(this.chainId)
} catch (e) {
log.error(`could not update gas prices for chain ${this.chainId}`, { feeMarket, chainConfig: this.chainConfig }, e)
}
})
}
Expand Down

0 comments on commit 9c3e672

Please sign in to comment.