Skip to content

Commit 97bd6cd

Browse files
authored
internal/ethapi: accept both hex and decimal for blockCount (#23363)
1 parent d60cfd2 commit 97bd6cd

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

internal/ethapi/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ type feeHistoryResult struct {
8787
GasUsedRatio []float64 `json:"gasUsedRatio"`
8888
}
8989

90-
func (s *PublicEthereumAPI) FeeHistory(ctx context.Context, blockCount hexutil.Uint, lastBlock rpc.BlockNumber, rewardPercentiles []float64) (*feeHistoryResult, error) {
90+
func (s *PublicEthereumAPI) FeeHistory(ctx context.Context, blockCount rpc.DecimalOrHex, lastBlock rpc.BlockNumber, rewardPercentiles []float64) (*feeHistoryResult, error) {
9191
oldest, reward, baseFee, gasUsed, err := s.b.FeeHistory(ctx, int(blockCount), lastBlock, rewardPercentiles)
9292
if err != nil {
9393
return nil, err

rpc/types.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"encoding/json"
2222
"fmt"
2323
"math"
24+
"strconv"
2425
"strings"
2526

2627
"github.com/ethereum/go-ethereum/common"
@@ -191,3 +192,24 @@ func BlockNumberOrHashWithHash(hash common.Hash, canonical bool) BlockNumberOrHa
191192
RequireCanonical: canonical,
192193
}
193194
}
195+
196+
// DecimalOrHex unmarshals a non-negative decimal or hex parameter into a uint64.
197+
type DecimalOrHex uint64
198+
199+
// UnmarshalJSON implements json.Unmarshaler.
200+
func (dh *DecimalOrHex) UnmarshalJSON(data []byte) error {
201+
input := strings.TrimSpace(string(data))
202+
if len(input) >= 2 && input[0] == '"' && input[len(input)-1] == '"' {
203+
input = input[1 : len(input)-1]
204+
}
205+
206+
value, err := strconv.ParseUint(input, 10, 64)
207+
if err != nil {
208+
value, err = hexutil.DecodeUint64(input)
209+
}
210+
if err != nil {
211+
return err
212+
}
213+
*dh = DecimalOrHex(value)
214+
return nil
215+
}

0 commit comments

Comments
 (0)