Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

client: add feeHistory method #2413

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions packages/client/lib/rpc/modules/eth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,9 @@ export class Eth {
1,
[[validators.blockOption]]
)

this.feeHistory = middleware(this.feeHistory.bind(this), 0, [])
this.gasPrice = middleware(this.gasPrice.bind(this), 0, [])
}

/**
Expand Down Expand Up @@ -986,4 +989,37 @@ export class Eth {
const block = await getBlockByOption(blockOpt, this._chain)
return intToHex(block.transactions.length)
}

async feeHistory(_params = []) {
const latest = await this._chain.getCanonicalHeadHeader()
return {
baseFeePerGas: [
'0x' + latest.baseFeePerGas?.toString(16),
'0x' + latest.baseFeePerGas?.toString(16),
],
oldestBlock: '0x' + latest.number.toString(16),
gasUsedRatio: [0.5],
}
}

/**
* Returns an estimate of the current gas price based on the most recent block
* @returns the current gasPrice in gwei as a hex prefixed string
*/
async gasPrice() {
let gasPrice: bigint = BigInt(0)
const latest = await this._chain.getCanonicalHeadHeader()
if (this._vm !== undefined && this._vm._common.isActivatedEIP(1559)) {
gasPrice = latest.calcNextBaseFee()
} else {
if (latest.gasUsed > BigInt(0)) gasPrice = latest.gasUsed
else {
while (gasPrice === BigInt(0)) {
const nextBlock = await this._chain.getBlock(latest.number - BigInt(1))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are you looping always on the same block ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is out of date. We merged a separate PR recently for eth_gasPrice.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So to add: see comment here #2413 (comment)

if (nextBlock.header.gasUsed > BigInt(0)) gasPrice = nextBlock.header.gasUsed
}
}
}
return '0x' + gasPrice.toString(16)
}
}