Skip to content

Commit 9ad5080

Browse files
lightclientfjl
andauthored
ethereum, ethclient: add FeeHistory support (#25403)
Co-authored-by: Felix Lange <fjl@twurst.com>
1 parent 377c7d7 commit 9ad5080

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

ethclient/ethclient.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,38 @@ func (ec *Client) SuggestGasTipCap(ctx context.Context) (*big.Int, error) {
505505
return (*big.Int)(&hex), nil
506506
}
507507

508+
type feeHistoryResultMarshaling struct {
509+
OldestBlock *hexutil.Big `json:"oldestBlock"`
510+
Reward [][]*hexutil.Big `json:"reward,omitempty"`
511+
BaseFee []*hexutil.Big `json:"baseFeePerGas,omitempty"`
512+
GasUsedRatio []float64 `json:"gasUsedRatio"`
513+
}
514+
515+
// FeeHistory retrieves the fee market history.
516+
func (ec *Client) FeeHistory(ctx context.Context, blockCount uint64, lastBlock *big.Int, rewardPercentiles []float64) (*ethereum.FeeHistory, error) {
517+
var res feeHistoryResultMarshaling
518+
if err := ec.c.CallContext(ctx, &res, "eth_feeHistory", hexutil.Uint(blockCount), toBlockNumArg(lastBlock), rewardPercentiles); err != nil {
519+
return nil, err
520+
}
521+
reward := make([][]*big.Int, len(res.Reward))
522+
for i, r := range res.Reward {
523+
reward[i] = make([]*big.Int, len(r))
524+
for j, r := range r {
525+
reward[i][j] = (*big.Int)(r)
526+
}
527+
}
528+
baseFee := make([]*big.Int, len(res.BaseFee))
529+
for i, b := range res.BaseFee {
530+
baseFee[i] = (*big.Int)(b)
531+
}
532+
return &ethereum.FeeHistory{
533+
OldestBlock: (*big.Int)(res.OldestBlock),
534+
Reward: reward,
535+
BaseFee: baseFee,
536+
GasUsedRatio: res.GasUsedRatio,
537+
}, nil
538+
}
539+
508540
// EstimateGas tries to estimate the gas needed to execute a specific transaction based on
509541
// the current pending state of the backend blockchain. There is no guarantee that this is
510542
// the true gas limit requirement as other transactions may be added or removed by miners,

ethclient/ethclient_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,29 @@ func testStatusFunctions(t *testing.T, client *rpc.Client) {
508508
if gasTipCap.Cmp(big.NewInt(234375000)) != 0 {
509509
t.Fatalf("unexpected gas tip cap: %v", gasTipCap)
510510
}
511+
512+
// FeeHistory
513+
history, err := ec.FeeHistory(context.Background(), 1, big.NewInt(2), []float64{95, 99})
514+
if err != nil {
515+
t.Fatalf("unexpected error: %v", err)
516+
}
517+
want := &ethereum.FeeHistory{
518+
OldestBlock: big.NewInt(2),
519+
Reward: [][]*big.Int{
520+
{
521+
big.NewInt(234375000),
522+
big.NewInt(234375000),
523+
},
524+
},
525+
BaseFee: []*big.Int{
526+
big.NewInt(765625000),
527+
big.NewInt(671627818),
528+
},
529+
GasUsedRatio: []float64{0.008912678667376286},
530+
}
531+
if !reflect.DeepEqual(history, want) {
532+
t.Fatalf("FeeHistory result doesn't match expected: (got: %v, want: %v)", history, want)
533+
}
511534
}
512535

513536
func testCallContractAtHash(t *testing.T, client *rpc.Client) {

interfaces.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,15 @@ type GasPricer interface {
201201
SuggestGasPrice(ctx context.Context) (*big.Int, error)
202202
}
203203

204+
// FeeHistory provides recent fee market data that consumers can use to determine
205+
// a reasonable maxPriorityFeePerGas value.
206+
type FeeHistory struct {
207+
OldestBlock *big.Int // block coresponding to first response value
208+
Reward [][]*big.Int // list every txs priority fee per block
209+
BaseFee []*big.Int // list of each block's base fee
210+
GasUsedRatio []float64 // ratio of gas used out of the total available limit
211+
}
212+
204213
// A PendingStateReader provides access to the pending state, which is the result of all
205214
// known executable transactions which have not yet been included in the blockchain. It is
206215
// commonly used to display the result of ’unconfirmed’ actions (e.g. wallet value

internal/ethapi/api.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ type feeHistoryResult struct {
8686
GasUsedRatio []float64 `json:"gasUsedRatio"`
8787
}
8888

89+
// FeeHistory returns the fee market history.
8990
func (s *EthereumAPI) FeeHistory(ctx context.Context, blockCount rpc.DecimalOrHex, lastBlock rpc.BlockNumber, rewardPercentiles []float64) (*feeHistoryResult, error) {
9091
oldest, reward, baseFee, gasUsed, err := s.b.FeeHistory(ctx, int(blockCount), lastBlock, rewardPercentiles)
9192
if err != nil {

0 commit comments

Comments
 (0)