-
Notifications
You must be signed in to change notification settings - Fork 1
/
request.go
152 lines (119 loc) · 3.67 KB
/
request.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package ethrpc
import (
"context"
"encoding/binary"
"math/big"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient/gethclient"
)
type Call struct {
ABI abi.ABI
UnpackABI []abi.ABI
Target string
Method string
Params []interface{}
Output []interface{}
}
func (c *Call) SetOutput(output []interface{}) *Call {
c.Output = output
return c
}
// autofillUnpackABI fills the call's UnpackABI in case it's not set
func (c *Call) autofillUnpackABI() {
if c.UnpackABI == nil {
c.UnpackABI = []abi.ABI{c.ABI}
return
}
if len(c.UnpackABI) == 0 {
c.UnpackABI = append(c.UnpackABI, c.ABI)
return
}
}
type Request struct {
client *Client
Method string
RequireSuccess bool
Calls []*Call
ctx context.Context
RawCallMsg ethereum.CallMsg
BlockNumber *big.Int
BlockHash common.Hash
Overrides map[common.Address]gethclient.OverrideAccount
}
// Context method returns the Context if it's already set in request
// otherwise it creates new one using `context.Background()`.
func (r *Request) Context() context.Context {
if r.ctx == nil {
return context.Background()
}
return r.ctx
}
// SetContext method sets the context.Context for current Request. It allows
// to interrupt the request execution if ctx.Done() channel is closed.
// See https://blog.golang.org/context article and the "context" package
// documentation.
func (r *Request) SetContext(ctx context.Context) *Request {
r.ctx = ctx
return r
}
// AddCall adds a call to the request
// it will autofill the UnpackABI in case it's not set
func (r *Request) AddCall(c *Call, output []interface{}) *Request {
c.autofillUnpackABI()
c.SetOutput(output)
r.Calls = append(r.Calls, c)
return r
}
func (r *Request) SetRequireSuccess(requireSuccess bool) *Request {
r.RequireSuccess = requireSuccess
return r
}
func (r *Request) SetBlockNumber(blockNumber *big.Int) *Request {
r.BlockNumber = blockNumber
return r
}
// SetOverrides method sets the state overrides of blockchain for the request
// NOTE: Overrides only work with no blockhash set
func (r *Request) SetOverrides(overrides map[common.Address]gethclient.OverrideAccount) *Request {
r.Overrides = overrides
return r
}
func (r *Request) SetBlockHash(blockHash common.Hash) *Request {
r.BlockHash = blockHash
return r
}
func (r *Request) Execute(method string) (*Response, error) {
r.Method = method
return r.client.execute(r)
}
func (r *Request) Call() (*Response, error) {
return r.Execute(MethodCall)
}
func (r *Request) Aggregate() (*Response, error) {
return r.Execute(MethodAggregate)
}
func (r *Request) TryAggregate() (*Response, error) {
return r.Execute(MethodTryAggregate)
}
func (r *Request) GetCurrentBlockTimestamp() (uint64, error) {
res, err := r.Execute(MethodGetCurrentBlockTimestamp)
if err != nil {
return 0, err
}
// Block timestamp is a uint256 number so returned byte array should have length equals 256/8.
if len(res.RawResponse) != 256/8 {
return 0, ErrUnexpectedResponse
}
// Golang does not have uint256 but uint64 is enough to store a timestamp value.
// Data is transferred in network byte order therefore just need to parse last 8 bytes in array.
blockTimestamp := binary.BigEndian.Uint64(res.RawResponse[len(res.RawResponse)-8:])
return blockTimestamp, nil
}
func (r *Request) GetStorageAt(account common.Address, key common.Hash, abi abi.Arguments) ([]interface{}, error) {
return r.client.getStorageAt(r.Context(), account, key, abi)
}
func (r *Request) TryBlockAndAggregate() (*Response, error) {
return r.Execute(MethodTryBlockAndAggregate)
}