-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathevmc.go
283 lines (239 loc) · 5.88 KB
/
evmc.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package evmc
import (
"context"
"errors"
"net/http"
"net/url"
"strings"
"sync"
"github.com/bbaktaeho/evmc/evmctypes"
"github.com/ethereum/go-ethereum/common/lru"
"github.com/ethereum/go-ethereum/rpc"
"github.com/gorilla/websocket"
)
// TODO: websocket RPC
// TODO: backoff retry
type clientInfo interface {
ChainID() uint64
NodeClient() (name, version string)
IsWebsocket() bool
}
type caller interface {
call(ctx context.Context, result interface{}, method Procedure, params ...interface{}) error
batchCall(ctx context.Context, elements []rpc.BatchElem) error
}
type subscriber interface {
subscribe(ctx context.Context, namespace string, ch interface{}, args ...interface{}) (evmctypes.Subscription, error)
}
type nodeSetter interface {
setNode(clientVersion string)
}
type transactionSender interface {
sendRawTransaction(ctx context.Context, rawTx string) (string, error)
}
type Evmc struct {
c *rpc.Client
isWebsocket bool
chainID uint64
nodeName ClientName
nodeVersion string
maxBatchItems int
eth *ethNamespace
web3 *web3Namespace
debug *debugNamespace
// trace *traceNamespace
// ots *otsNamespace
contract *contract
erc20 *erc20Contract
erc721 *erc721Contract
erc1155 *erc1155Contract
abiCache *lru.Cache[string, interface{}]
}
func httpClient(o *options) *http.Client {
transport := http.DefaultTransport.(*http.Transport)
transport.MaxIdleConns = o.connPool
transport.MaxIdleConnsPerHost = o.connPool
transport.MaxConnsPerHost = o.connPool
transport.IdleConnTimeout = o.idleConnTimeout
transport.DisableKeepAlives = false
return &http.Client{Transport: transport, Timeout: o.reqTimeout}
}
func New(httpURL string, opts ...Options) (*Evmc, error) {
u, err := url.Parse(httpURL)
if err != nil {
return nil, err
}
if u.Scheme != "http" && u.Scheme != "https" {
return nil, errors.New("invalid http scheme")
}
return newClient(context.Background(), httpURL, false, opts...)
}
func NewWebsocket(ctx context.Context, wsURL string, opts ...Options) (*Evmc, error) {
u, err := url.Parse(wsURL)
if err != nil {
return nil, err
}
if u.Scheme != "ws" && u.Scheme != "wss" {
return nil, errors.New("invalid websocket scheme")
}
return newClient(ctx, wsURL, true, opts...)
}
func newClient(ctx context.Context, url string, isWs bool, opts ...Options) (*Evmc, error) {
o := newOps()
for _, opt := range opts {
opt.apply(o)
}
rpcClient, err := rpc.DialOptions(
ctx,
url,
rpc.WithHTTPClient(httpClient(o)),
rpc.WithBatchItemLimit(o.maxBatchItems),
rpc.WithBatchResponseSizeLimit(o.maxBatchSize),
rpc.WithWebsocketDialer(websocket.Dialer{
ReadBufferSize: o.wsReadBufferSize,
WriteBufferSize: o.wsWriteBufferSize,
}),
rpc.WithWebsocketMessageSizeLimit(int64(o.wsMessageSizeLimit)),
)
if err != nil {
return nil, err
}
evmc := &Evmc{
c: rpcClient,
isWebsocket: isWs,
abiCache: lru.NewCache[string, interface{}](10),
maxBatchItems: o.maxBatchItems,
}
evmc.eth = ðNamespace{info: evmc, c: evmc, s: evmc, ts: evmc}
evmc.web3 = &web3Namespace{c: evmc, n: evmc}
evmc.debug = &debugNamespace{c: evmc}
evmc.contract = &contract{c: evmc}
evmc.erc20 = &erc20Contract{info: evmc, c: evmc, ts: evmc}
chainID, err := evmc.eth.ChainID()
if err != nil {
return nil, err
}
evmc.chainID = chainID
nodeClient, err := evmc.web3.ClientVersion()
if err != nil {
return nil, err
}
evmc.setNode(nodeClient)
return evmc, nil
}
func (e *Evmc) Close() {
e.c.Close()
}
func (e *Evmc) IsWebsocket() bool {
return e.isWebsocket
}
func (e *Evmc) ChainID() uint64 {
return e.chainID
}
func (e *Evmc) NodeClient() (name, version string) {
return e.nodeName.String(), e.nodeVersion
}
func (e *Evmc) Web3() *web3Namespace {
return e.web3
}
func (e *Evmc) Eth() *ethNamespace {
return e.eth
}
func (e *Evmc) Debug() *debugNamespace {
return e.debug
}
func (e *Evmc) Contract() *contract {
return e.contract
}
// func (e *Evmc) Trace() *traceNamespace {
// return e.trace
// }
// func (e *Evmc) Ots() *otsNamespace {
// return e.ots
// }
func (e *Evmc) ERC20() *erc20Contract {
return e.erc20
}
func (e *Evmc) ERC721() *erc721Contract {
return e.erc721
}
func (e *Evmc) ERC1155() *erc1155Contract {
return e.erc1155
}
// func (e *Evmc) Ots() {}
func (e *Evmc) call(
ctx context.Context,
result interface{},
method Procedure,
params ...interface{},
) error {
if err := e.c.CallContext(ctx, result, method.String(), params...); err != nil {
return err
}
return nil
}
func (e *Evmc) batchCall(ctx context.Context, elements []rpc.BatchElem) error {
var (
size = len(elements)
loopCount = size / e.maxBatchItems
wg = new(sync.WaitGroup)
errCh = make(chan error)
errs = make([]error, 0, 1)
)
if size%e.maxBatchItems != 0 {
loopCount++
}
go func() {
for e := range errCh {
errs = append(errs, e)
}
}()
for i := 0; i < loopCount; i++ {
low := e.maxBatchItems * i
high := e.maxBatchItems * (i + 1)
if high > size {
high = size
}
div := elements[low:high]
wg.Add(1)
go func(subElements []rpc.BatchElem) {
defer wg.Done()
if err := e.c.BatchCallContext(ctx, subElements); err != nil {
errCh <- err
}
}(div)
}
wg.Wait()
close(errCh)
if len(errs) > 0 {
return errs[0]
}
return nil
}
func (e *Evmc) subscribe(
ctx context.Context,
namespace string,
ch interface{},
args ...interface{},
) (evmctypes.Subscription, error) {
subscription, err := e.c.Subscribe(ctx, namespace, ch, args...)
if err != nil {
return nil, err
}
return subscription, nil
}
func (e *Evmc) sendRawTransaction(ctx context.Context, rawTx string) (string, error) {
result := new(string)
if err := e.call(ctx, result, EthSendRawTransaction, rawTx); err != nil {
return "", err
}
return *result, nil
}
func (e *Evmc) setNode(cv string) {
cvarr := strings.Split(cv, "/")
if len(cvarr) < 2 {
return
}
e.nodeName = ClientName(cvarr[0])
e.nodeVersion = cvarr[1]
}