-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patheth_on_block.go
109 lines (90 loc) · 3.26 KB
/
eth_on_block.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package bloxroute_sdk_go
import (
"context"
"fmt"
"github.com/bloXroute-Labs/gateway/v2/types"
)
// OnBlockParams is the params object for the eth_onBlock subscription
type OnBlockParams struct {
// Include is a list of fields to include in the response
// Optional
Include []string `json:"include"`
// CallParams is used to build an RPC call request
// Required
CallParams []OnBlockParamsCallParams `json:"call-params"`
}
// OnBlockParamsCallParams represents a value in the CallParams array
type OnBlockParamsCallParams interface {
isEthOnBlockParamsCallParams()
}
// OnBlockParamsCallParamsCommon is the common fields for all CallParams
type OnBlockParamsCallParamsCommon struct {
// Method is the RPC method to call
Method string `json:"method"`
//
Tag string `json:"tag,omitempty"`
// Name is a unique string identifier for call
Name string `json:"name,omitempty"`
}
func (*OnBlockParamsCallParamsCommon) isEthOnBlockParamsCallParams() {}
// OnBlockParamsEthCall represents params for eth_call
// https://ethereum.org/en/developers/docs/apis/json-rpc/#eth_call
type OnBlockParamsEthCall struct {
OnBlockParamsCallParamsCommon
From string `json:"from,omitempty"`
To string `json:"to"`
Gas string `json:"gas"`
Value string `json:"value"`
Data string `json:"data"`
}
// OnBlockParamsGetBalance represents params for eth_getBalance
// https://ethereum.org/en/developers/docs/apis/json-rpc/#eth_getbalance
type OnBlockParamsGetBalance struct {
OnBlockParamsCallParamsCommon
Address string `json:"address"`
}
// OnBlockParamsGetTransactionCount represents params for eth_getTransactionCount
// https://ethereum.org/en/developers/docs/apis/json-rpc/#eth_gettransactioncount
type OnBlockParamsGetTransactionCount struct {
OnBlockParamsCallParamsCommon
Address string `json:"address"`
}
// OnBlockParamsGetCode represents params for eth_getCode
// https://ethereum.org/en/developers/docs/apis/json-rpc/#eth_getcode
type OnBlockParamsGetCode struct {
OnBlockParamsCallParamsCommon
Address string `json:"address"`
}
// OnBlockParamsGetStorageAt represents params for eth_getStorageAt
// https://ethereum.org/en/developers/docs/apis/json-rpc/#eth_getstorageat
type OnBlockParamsGetStorageAt struct {
OnBlockParamsCallParamsCommon
Address string `json:"address"`
Pos string `json:"pos"`
}
// OnBlockParamsBlockNumber represents params for eth_blockNumber
// https://ethereum.org/en/developers/docs/apis/json-rpc/#eth_blocknumber
type OnBlockParamsBlockNumber struct {
OnBlockParamsCallParamsCommon
}
// OnBlock subscribes to stream of changes in the EVM state when a new block is mined
func (c *Client) OnBlock(ctx context.Context, params *OnBlockParams, callbackFunc CallbackFunc[*OnBlockNotification]) error {
if params == nil {
return ErrNilParams
}
if len(params.CallParams) == 0 {
return fmt.Errorf("at least one call_params is required")
}
wrap := func(ctx context.Context, err error, result any) {
if err != nil {
callbackFunc(ctx, err, nil)
return
}
callbackFunc(ctx, err, result.(*OnBlockNotification))
}
return c.handler.Subscribe(ctx, types.OnBlockFeed, params, wrap)
}
// UnsubscribeFromEthOnBlock unsubscribes from the eth_onBlock feed
func (c *Client) UnsubscribeFromEthOnBlock() error {
return c.handler.UnsubscribeRetry(types.OnBlockFeed)
}