This repository has been archived by the owner on Jul 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
269 lines (223 loc) · 6.63 KB
/
client.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
package rockside
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"math/big"
"net/http"
"net/http/httputil"
"net/url"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
)
var (
Testnet Network = "ropsten"
Mainnet Network = "mainnet"
PoaNetworkMainnet Network = "poanetwork"
GethPrivateNet Network = "gethprivate"
)
type Network string
func (n Network) ExplorerURL() string {
switch n {
case Mainnet:
return "https://etherscan.io"
case Testnet:
return fmt.Sprintf("https://%s.etherscan.io", n)
case PoaNetworkMainnet:
return fmt.Sprintf("https://blockscout.com/poa/core")
default:
return ""
}
}
func (n Network) ChainID() *big.Int {
switch n {
case Mainnet:
return big.NewInt(1)
case Testnet:
return big.NewInt(3)
case PoaNetworkMainnet:
return big.NewInt(99)
case GethPrivateNet:
return big.NewInt(1337)
default:
return big.NewInt(0)
}
}
type endpoint struct {
client *Client
}
type Client struct {
rocksideURL *url.URL
network Network
logger *log.Logger
authHTTPClient *http.Client
RPCClient *RPCClient
EOA *EOA
SmartWallets *SmartWallets
Transaction *Transactions
Tokens *Tokens
Forwarder *Forwarder
Relay *Relay
}
const defaultRocksideURL = "https://api.rockside.io"
func NewClientFromAPIKey(apiKey string, net Network, rocksideBaseURL ...string) (*Client, error) {
if len(apiKey) == 0 {
return nil, fmt.Errorf("init client: no API key found. Try with env variable ROCKSIDE_API_KEY")
}
if len(apiKey) != 32 {
return nil, fmt.Errorf("init client: expected length of API key to be 32 but got %d", len(apiKey))
}
baseURL := defaultRocksideURL
if len(rocksideBaseURL) > 0 {
baseURL = rocksideBaseURL[0]
}
authFunc := func(req *http.Request) {
req.Header.Set("apikey", apiKey)
}
return newClient(&http.Client{Transport: &authenticatedHeaderTransport{authFunc}}, net, baseURL)
}
func NewClientFromToken(token, origin string, net Network, rocksideBaseURL ...string) (*Client, error) {
baseURL := defaultRocksideURL
if len(rocksideBaseURL) > 0 {
baseURL = rocksideBaseURL[0]
}
authFunc := func(req *http.Request) {
req.Header.Set("Origin", origin)
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
}
return newClient(&http.Client{Transport: &authenticatedHeaderTransport{authFunc}}, net, baseURL)
}
func newClient(authenticatedHTTPClient *http.Client, net Network, baseURL string) (*Client, error) {
var network Network
switch net {
case Mainnet, Testnet:
network = net
default:
return nil, fmt.Errorf("init client: invalid network '%s' for client. Expecting: %s or %s", net, Mainnet, Testnet)
}
u, err := url.Parse(baseURL)
if err != nil {
return nil, err
}
if u.Scheme != "https" {
return nil, fmt.Errorf("init client: expected base URL with HTTPS scheme but got URL '%s'", u)
}
rpcEndpoint, err := url.Parse(fmt.Sprintf("%s/ethereum/%s/jsonrpc", u, network))
if err != nil {
return nil, fmt.Errorf("cannot build RPC URL from %s (%s)", u, network)
}
rpcClient, err := rpc.DialHTTPWithClient(rpcEndpoint.String(), authenticatedHTTPClient)
if err != nil {
return nil, fmt.Errorf("cannot RPC dial with custom HTTP client: %s", err)
}
c := &Client{
RPCClient: &RPCClient{
endpoint: rpcEndpoint,
authHTTPClient: authenticatedHTTPClient,
Client: ethclient.NewClient(rpcClient),
},
authHTTPClient: authenticatedHTTPClient,
rocksideURL: u,
network: network,
logger: log.New(ioutil.Discard, "", 0),
}
c.EOA = &EOA{c}
c.SmartWallets = &SmartWallets{c}
c.Transaction = &Transactions{c}
c.Tokens = &Tokens{c}
c.Forwarder = &Forwarder{c}
c.Relay = &Relay{c}
return c, nil
}
func (c *Client) SetLogger(l *log.Logger) {
c.logger = l
}
func (c *Client) CurrentNetwork() Network {
return c.network
}
func (c *Client) URL() string {
return c.rocksideURL.String()
}
func (c *Client) get(urlPath string, body interface{}, decode interface{}) (*http.Response, error) {
return c.performRequest(http.MethodGet, urlPath, body, decode)
}
func (c *Client) post(urlPath string, body interface{}, decode interface{}) (*http.Response, error) {
return c.performRequest(http.MethodPost, urlPath, body, decode)
}
func (c *Client) delete(urlPath string, body interface{}, decode interface{}) (*http.Response, error) {
return c.performRequest(http.MethodDelete, urlPath, body, decode)
}
func (c *Client) put(urlPath string, body interface{}, decode interface{}) (*http.Response, error) {
return c.performRequest(http.MethodPut, urlPath, body, decode)
}
func (c *Client) performRequest(method, urlPath string, body interface{}, decode interface{}) (*http.Response, error) {
path, err := url.Parse(urlPath)
if err != nil {
return nil, err
}
fullURL := c.rocksideURL.ResolveReference(path)
var buf io.ReadWriter
if body != nil {
buf = new(bytes.Buffer)
if err := json.NewEncoder(buf).Encode(body); err != nil {
return nil, err
}
}
req, err := http.NewRequest(method, fullURL.String(), buf)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", "rockside-sdk-go")
if body != nil {
req.Header.Set("Content-Type", "application/json")
}
dump, _ := httputil.DumpRequestOut(req, true)
c.logger.Printf(">>>>>> Request %s-----\n\n", dump)
resp, err := c.authHTTPClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
dump, _ = httputil.DumpResponse(resp, true)
c.logger.Printf("<<<<<< Response %s\n-----\n\n", dump)
if status := resp.StatusCode; status > 299 || status < 200 {
context := c.errorContextString(resp)
if msg, err := decodeJSONErr(resp.Body); err != nil {
return resp, fmt.Errorf("non JSON body returned (try verbose mode) %s: %s", context, err)
} else {
return resp, fmt.Errorf("%s %s", msg, context)
}
}
if decode != nil {
if err := json.NewDecoder(resp.Body).Decode(decode); err != nil {
return resp, err
}
}
return resp, nil
}
func (c *Client) errorContextString(resp *http.Response) string {
return fmt.Sprintf("[status: %s, URL: '%s', network: %s, request ID: %s]", resp.Status, c.URL(), c.CurrentNetwork(), resp.Header.Get("X-Request-ID"))
}
func decodeJSONErr(body io.Reader) (string, error) {
v := struct {
Err string `json:"error"`
Message string `json:"message"`
}{}
if err := json.NewDecoder(body).Decode(&v); err != nil {
return "", err
}
if v.Err != "" {
return v.Err, nil
}
return v.Message, nil
}
type authenticatedHeaderTransport struct {
authFunc func(req *http.Request)
}
func (t *authenticatedHeaderTransport) RoundTrip(req *http.Request) (*http.Response, error) {
t.authFunc(req)
return http.DefaultTransport.RoundTrip(req)
}