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

ethclient: add FeeHistory support #25403

Merged
merged 5 commits into from
Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
40 changes: 40 additions & 0 deletions ethclient/ethclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,46 @@ func (ec *Client) SuggestGasTipCap(ctx context.Context) (*big.Int, error) {
return (*big.Int)(&hex), nil
}

// FeeHistoryResult is the result of FeeHistory.
type FeeHistoryResult struct {
lightclient marked this conversation as resolved.
Show resolved Hide resolved
OldestBlock *big.Int `json:"oldestBlock"`
Reward [][]*big.Int `json:"reward,omitempty"`
BaseFee []*big.Int `json:"baseFeePerGas,omitempty"`
GasUsedRatio []float64 `json:"gasUsedRatio"`
}

type feeHistoryResultMarshaling struct {
OldestBlock *hexutil.Big `json:"oldestBlock"`
Reward [][]*hexutil.Big `json:"reward,omitempty"`
BaseFee []*hexutil.Big `json:"baseFeePerGas,omitempty"`
GasUsedRatio []float64 `json:"gasUsedRatio"`
}

// FeeHistory retrieves the fee market history.
func (ec *Client) FeeHistory(ctx context.Context, blockCount uint64, lastBlock *big.Int, rewardPercentiles []float64) (*FeeHistoryResult, error) {
var res feeHistoryResultMarshaling
if err := ec.c.CallContext(ctx, &res, "eth_feeHistory", hexutil.Uint(blockCount), hexutil.Big(*lastBlock), rewardPercentiles); err != nil {
lightclient marked this conversation as resolved.
Show resolved Hide resolved
return nil, err
}
reward := make([][]*big.Int, len(res.Reward))
for i, r := range res.Reward {
reward[i] = make([]*big.Int, len(r))
for j, r := range r {
reward[i][j] = (*big.Int)(r)
}
}
baseFee := make([]*big.Int, len(res.BaseFee))
for i, b := range res.BaseFee {
baseFee[i] = (*big.Int)(b)
}
return &FeeHistoryResult{
OldestBlock: (*big.Int)(res.OldestBlock),
Reward: reward,
BaseFee: baseFee,
GasUsedRatio: res.GasUsedRatio,
}, nil
}

// EstimateGas tries to estimate the gas needed to execute a specific transaction based on
// the current pending state of the backend blockchain. There is no guarantee that this is
// the true gas limit requirement as other transactions may be added or removed by miners,
Expand Down
23 changes: 23 additions & 0 deletions ethclient/ethclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,29 @@ func testStatusFunctions(t *testing.T, client *rpc.Client) {
if gasTipCap.Cmp(big.NewInt(234375000)) != 0 {
t.Fatalf("unexpected gas tip cap: %v", gasTipCap)
}

// FeeHistory
history, err := ec.FeeHistory(context.Background(), 1, big.NewInt(2), []float64{95, 99})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
want := &FeeHistoryResult{
OldestBlock: big.NewInt(2),
Reward: [][]*big.Int{
{
big.NewInt(234375000),
big.NewInt(234375000),
},
},
BaseFee: []*big.Int{
big.NewInt(765625000),
big.NewInt(671627818),
},
GasUsedRatio: []float64{0.008912678667376286},
}
if !reflect.DeepEqual(history, want) {
t.Fatalf("FeeHistory result doesn't match expected: (got: %v, want: %v)", history, want)
}
}

func testCallContractAtHash(t *testing.T, client *rpc.Client) {
Expand Down
1 change: 1 addition & 0 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ type feeHistoryResult struct {
GasUsedRatio []float64 `json:"gasUsedRatio"`
}

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