1

I'm looking for a way to fetch current base fee using JSON RPC (and go-ethereum ethclient.Client as client)

I'm aware of new JSON RPC eth_maxPriorityFeePerGas which gives me second parameter - but this endpoint dosen't anwser the base fee question

Only way to get near the result I expected is eth_gasPrice - eth_maxPriorityFeePerGas, which gives me good approximation, but numbers don't match with etherscan (block view -> Base Fee Per Gas) and seem to be off by anything between 0.5 to 10 GWEI.

4
  • 1
    you can get the base fee from last block and then multiply it using the same formula as in ethereum golang
    – Nulik
    Aug 26, 2021 at 17:14
  • @Nulik this sounds like workaround, but okay - that's very good pointer though, I think I can construct anwser out of your pointer. Aug 26, 2021 at 17:56
  • 1
    ok, the base fee is in the header, and the function to calculate base fee for the next block is here: github.com/ethereum/go-ethereum/blob/…
    – Nulik
    Aug 26, 2021 at 18:13
  • @Nulik yes, I have working sample code, but Im doing multiple blocks verification if it works as expected before I post self-anwser. Aug 26, 2021 at 18:16

1 Answer 1

2

JSON RPC does not contain method that can return this data, unfortunately.

As suggested by @Nulik in comment, it's possible to calculate this precisely from go-ethereum library, using misc.CalcBaseFee() function, where config can be gathered from params.MainnetChainConfig

While this isn't direct solution (dosen't use JSON RPC), it's only solution that is immidietely available.

Full code example (removed error handling for brievety):

package main

import (
    "context"
    "fmt"
    "math/big"

    "github.com/ethereum/go-ethereum/consensus/misc"
    "github.com/ethereum/go-ethereum/ethclient"
    "github.com/ethereum/go-ethereum/params"
)

const ethJSONRPCEndpointAddress = "http://path-to-json-rpc:8545"

func main() {
    config := params.MainnetChainConfig
    ethClient, _ := ethclient.DialContext(context.Background(), ethJSONRPCEndpointAddress)
    bn, _ := ethClient.BlockNumber(context.Background())

    bignumBn := big.NewInt(0).SetUint64(bn)
    blk, _ := ethClient.BlockByNumber(context.Background(), bignumBn)
    baseFee := misc.CalcBaseFee(config, blk.Header())
    fmt.Printf("Base fee for block %d is %s\n", bn+1, baseFee.String())
}

example output:

Base fee for block 13102540 is 92382639576

Etherscan agrees with this estimation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.