Skip to content

Commit 41020fa

Browse files
authored
Add fee history rpc call (#192)
* Add fee history rpc call * Add Changelog
1 parent 76ba1f4 commit 41020fa

File tree

5 files changed

+90
-5
lines changed

5 files changed

+90
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
# 0.1.2 (Unreleased)
33

4+
- Add `eth_feeHistory` rpc endpoint [[GH-192](https://github.com/umbracle/ethgo/issues/192)]
45
- Update `testserver` to `go-ethereum:v1.10.15` [[GH-191](https://github.com/umbracle/ethgo/issues/191)]
56
- Do not decode `to` in `Transaction` if not exists [[GH-190](https://github.com/umbracle/ethgo/issues/190)]
67

jsonrpc/eth.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,53 @@ func (e *Eth) ChainID() (*big.Int, error) {
227227
}
228228
return parseBigInt(out), nil
229229
}
230+
231+
// FeeHistory is the result of the eth_feeHistory endpoint
232+
type FeeHistory struct {
233+
OldestBlock *big.Int `json:"oldestBlock"`
234+
Reward [][]*big.Int `json:"reward,omitempty"`
235+
BaseFee []*big.Int `json:"baseFeePerGas,omitempty"`
236+
GasUsedRatio []float64 `json:"gasUsedRatio"`
237+
}
238+
239+
func (f *FeeHistory) UnmarshalJSON(data []byte) error {
240+
var raw struct {
241+
OldestBlock *ArgBig `json:"oldestBlock"`
242+
Reward [][]*ArgBig `json:"reward,omitempty"`
243+
BaseFee []*ArgBig `json:"baseFeePerGas,omitempty"`
244+
GasUsedRatio []float64 `json:"gasUsedRatio"`
245+
}
246+
if err := json.Unmarshal(data, &raw); err != nil {
247+
return err
248+
}
249+
if raw.OldestBlock != nil {
250+
f.OldestBlock = raw.OldestBlock.Big()
251+
}
252+
if raw.Reward != nil {
253+
f.Reward = [][]*big.Int{}
254+
for _, r := range raw.Reward {
255+
elem := []*big.Int{}
256+
for _, i := range r {
257+
elem = append(elem, i.Big())
258+
}
259+
f.Reward = append(f.Reward, elem)
260+
}
261+
}
262+
if raw.BaseFee != nil {
263+
f.BaseFee = []*big.Int{}
264+
for _, i := range raw.BaseFee {
265+
f.BaseFee = append(f.BaseFee, i.Big())
266+
}
267+
}
268+
f.GasUsedRatio = raw.GasUsedRatio
269+
return nil
270+
}
271+
272+
// FeeHistory returns base fee per gas and transaction effective priority fee
273+
func (e *Eth) FeeHistory(from, to ethgo.BlockNumber) (*FeeHistory, error) {
274+
var out *FeeHistory
275+
if err := e.c.Call("eth_feeHistory", &out, from.String(), to.String(), nil); err != nil {
276+
return nil, err
277+
}
278+
return out, nil
279+
}

jsonrpc/eth_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,3 +367,17 @@ func TestEthGetStorageAt(t *testing.T) {
367367
assert.True(t, strings.HasSuffix(res.String(), "a"))
368368
}
369369
}
370+
371+
func TestEthFeeHistory(t *testing.T) {
372+
c, _ := NewClient(testutil.TestInfuraEndpoint(t))
373+
374+
lastBlock, err := c.Eth().BlockNumber()
375+
assert.NoError(t, err)
376+
377+
from := ethgo.BlockNumber(lastBlock - 2)
378+
to := ethgo.BlockNumber(lastBlock)
379+
380+
fee, err := c.Eth().FeeHistory(from, to)
381+
assert.NoError(t, err)
382+
assert.NotNil(t, fee)
383+
}

jsonrpc/subscribe.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func (c *Client) SubscriptionEnabled() bool {
1616
func (c *Client) Subscribe(method string, callback func(b []byte)) (func() error, error) {
1717
pub, ok := c.transport.(transport.PubSubTransport)
1818
if !ok {
19-
return nil, fmt.Errorf("Transport does not support the subscribe method")
19+
return nil, fmt.Errorf("transport does not support the subscribe method")
2020
}
2121
close, err := pub.Subscribe(method, callback)
2222
return close, err

jsonrpc/util.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,30 @@ import (
88
"strings"
99
)
1010

11+
type ArgBig big.Int
12+
13+
func (a *ArgBig) UnmarshalText(input []byte) error {
14+
buf, err := parseHexBytes(string(input))
15+
if err != nil {
16+
return err
17+
}
18+
b := new(big.Int)
19+
b.SetBytes(buf)
20+
*a = ArgBig(*b)
21+
return nil
22+
}
23+
24+
func (a *ArgBig) Big() *big.Int {
25+
b := big.Int(*a)
26+
return &b
27+
}
28+
1129
func encodeUintToHex(i uint64) string {
1230
return fmt.Sprintf("0x%x", i)
1331
}
1432

1533
func parseBigInt(str string) *big.Int {
16-
if strings.HasPrefix(str, "0x") {
17-
str = str[2:]
18-
}
34+
str = strings.TrimPrefix(str, "0x")
1935
num := new(big.Int)
2036
num.SetString(str, 16)
2137
return num
@@ -38,7 +54,11 @@ func parseHexBytes(str string) ([]byte, error) {
3854
if !strings.HasPrefix(str, "0x") {
3955
return nil, fmt.Errorf("it does not have 0x prefix")
4056
}
41-
buf, err := hex.DecodeString(str[2:])
57+
str = strings.TrimPrefix(str, "0x")
58+
if len(str)%2 != 0 {
59+
str = "0" + str
60+
}
61+
buf, err := hex.DecodeString(str)
4262
if err != nil {
4363
return nil, err
4464
}

0 commit comments

Comments
 (0)