-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconn.go
281 lines (238 loc) · 6.81 KB
/
conn.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
package beth
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"math/big"
"net/http"
"time"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
)
// ErrCannotConvertToBigInt is returned when string cannot be parsed into a
// big.Int format.
var ErrCannotConvertToBigInt = errors.New("cannot convert hex string to int: invalid format")
// Client will have a connection to an ethereum client (specified by the url)
type Client struct {
ethClient *ethclient.Client
addrBook AddressBook
url string
}
// Connect to an infura network (Supported networks: mainnet and kovan).
func Connect(url string) (Client, error) {
ethClient, err := ethclient.Dial(url)
if err != nil {
return Client{}, err
}
netID, err := ethClient.NetworkID(context.Background())
if err != nil {
return Client{}, err
}
return Client{
ethClient: ethClient,
addrBook: DefaultAddressBook(netID.Int64()),
url: url,
}, nil
}
// WriteAddress to the address book, overwrite if already exists
func (client *Client) WriteAddress(key string, address common.Address) {
client.addrBook[key] = address
}
// ReadAddress from the address book, return an error if the address does not
// exist
func (client *Client) ReadAddress(key string) (common.Address, error) {
if address, ok := client.addrBook[key]; ok {
return address, nil
}
return common.Address{}, ErrAddressNotFound
}
// WaitMined waits for tx to be mined on the blockchain.
// It stops waiting when the context is canceled.
func (client *Client) WaitMined(ctx context.Context, tx *types.Transaction) (*types.Receipt, error) {
return bind.WaitMined(ctx, client.ethClient, tx)
}
// Get will perform a read-only transaction on the ethereum blockchain.
func (client *Client) Get(ctx context.Context, f func() error) (err error) {
sleepDurationMs := time.Duration(1000)
// Keep retrying until the read-only transaction succeeds or until context
// times out
for {
select {
case <-ctx.Done():
if err == nil {
return ctx.Err()
}
return
default:
}
if err = f(); err == nil {
return
}
// If transaction errors, wait for sometime before retrying
select {
case <-ctx.Done():
if err == nil {
return ctx.Err()
}
return
case <-time.After(sleepDurationMs * time.Millisecond):
}
// Increase delay for next round but saturate at 30s
sleepDurationMs = time.Duration(float64(sleepDurationMs) * 1.6)
if sleepDurationMs > 30000 {
sleepDurationMs = 30000
}
}
}
// BalanceOf returns the ethereum balance of the addr passed.
func (client *Client) BalanceOf(ctx context.Context, addr common.Address) (val *big.Int, err error) {
err = client.Get(ctx, func() (err error) {
val, err = client.ethClient.BalanceAt(ctx, addr, nil)
return
})
return
}
// EthClient returns the ethereum client connection.
func (client *Client) EthClient() *ethclient.Client {
return client.ethClient
}
// TxBlockNumber retrieves tx's block number using the tx hash.
func (client *Client) TxBlockNumber(ctx context.Context, hash string) (*big.Int, error) {
type Result struct {
BlockNumber string `json:"blockNumber,omitempty"`
}
type JSONResponse struct {
Result Result `json:"result,omitempty"`
}
var data JSONResponse
var jsonStr = `{"jsonrpc":"2.0","method":"eth_getTransactionByHash",` +
`"params":["` + hash + `"],"id":1}`
// Keep retrying until a block number is returned or until context times out
for {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
response, err := sendInfuraRequest(ctx, client.url, jsonStr)
if err != nil {
continue
}
err = json.Unmarshal(response, &data)
if err != nil || data.Result == (Result{}) || data.Result.BlockNumber == "" {
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-time.After(5 * time.Millisecond):
}
continue
}
break
}
return hexToBigInt(data.Result.BlockNumber)
}
// CurrentBlockNumber will retrieve the current block that is confirmed by
// infura.
func (client *Client) CurrentBlockNumber(ctx context.Context) (*big.Int, error) {
type Result struct {
Number string `json:"number,omitempty"`
}
type JSONResponse struct {
Result Result `json:"result,omitempty"`
}
var data JSONResponse
var jsonStr = `{"jsonrpc":"2.0","method":"eth_getBlockByNumber",` +
`"params":["latest", false],"id":1}`
// Keep retrying until a block number is returned or until context times out
for {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
response, err := sendInfuraRequest(ctx, client.url, jsonStr)
if err != nil {
continue
}
err = json.Unmarshal(response, &data)
if err != nil || data.Result == (Result{}) || data.Result.Number == "" {
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-time.After(5 * time.Millisecond):
}
continue
}
break
}
return hexToBigInt(data.Result.Number)
}
// hexToBigInt will convert a hex value in string format to the corresponding
// big.Int value. For example : "0xFD6CE" will return big.Int(1038030).
func hexToBigInt(hex string) (*big.Int, error) {
bigInt := big.NewInt(0)
bigIntStr := hex[2:]
bigInt, ok := bigInt.SetString(bigIntStr, 16)
if !ok {
return bigInt, ErrCannotConvertToBigInt
}
return bigInt, nil
}
// sendInfuraRequest will send a request to infura and return the unmarshalled data
// back to the caller. It will retry until a valid response is returned, or
// until the context times out.
func sendInfuraRequest(ctx context.Context, url string, request string) (body []byte, err error) {
sleepDurationMs := time.Duration(1000)
// Retry until a valid response is returned or until context times out
for {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
if body, err = func() ([]byte, error) {
// Create a new http POST request
req, err := http.NewRequest("POST", url, bytes.NewBuffer([]byte(request)))
if err != nil {
return nil, err
}
// Send http POST request
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
// Decode response body
return func() ([]byte, error) {
defer resp.Body.Close()
// Check status
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status %v", resp.StatusCode)
}
// Check body
if resp.Body != nil {
return ioutil.ReadAll(resp.Body)
}
return nil, fmt.Errorf("response body is nil")
}()
}(); err == nil {
break
}
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-time.After(sleepDurationMs * time.Millisecond):
}
// Increase delay for next round but saturate at 30s
sleepDurationMs = time.Duration(float64(sleepDurationMs) * 1.6)
if sleepDurationMs > 30000 {
sleepDurationMs = 30000
}
}
return
}