-
Notifications
You must be signed in to change notification settings - Fork 0
/
etherscan.go
158 lines (135 loc) · 3.51 KB
/
etherscan.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
package etherscan
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"github.com/wangwei123/go-web3"
"github.com/wangwei123/go-web3/jsonrpc/codec"
"github.com/valyala/fasthttp"
)
// Etherscan is a provider using the Etherscan api
type Etherscan struct {
client fasthttp.Client
url string
apiKey string
}
// NewEtherscanFromNetwork creates a new client from the network id
func NewEtherscanFromNetwork(n web3.Network, apiKey string) (*Etherscan, error) {
var url string
switch n {
case web3.Mainnet:
url = "https://api.etherscan.io"
case web3.Ropsten:
url = "https://ropsten.etherscan.io"
case web3.Rinkeby:
url = "https://rinkeby.etherscan.io"
case web3.Goerli:
url = "https://goerli.etherscan.io"
default:
return nil, fmt.Errorf("unknwon network id %d", n)
}
return NewEtherscan(url, apiKey), nil
}
// NewEtherscan creates a new Etherscan service from a url
func NewEtherscan(url, apiKey string) *Etherscan {
return &Etherscan{url: url}
}
type proxyResponse struct {
Status string
Message string
Result json.RawMessage
}
// Query sends a query to Etherscan
func (e *Etherscan) Query(module, action string, out interface{}, params map[string]string) error {
url := fmt.Sprintf("%s/api?module=%s&action=%s", e.url, module, action)
if len(params) != 0 {
res := []string{}
for k, v := range params {
res = append(res, k+"="+v)
}
url += "&" + strings.Join(res, "&")
}
if e.apiKey != "" {
url = url + "&apikey=" + e.apiKey
}
req := fasthttp.AcquireRequest()
res := fasthttp.AcquireResponse()
defer fasthttp.ReleaseRequest(req)
defer fasthttp.ReleaseResponse(res)
req.SetRequestURI(url)
req.Header.SetMethod("GET")
if err := e.client.Do(req, res); err != nil {
return err
}
// Decode json-rpc response
var result json.RawMessage
if module == "proxy" {
var response codec.Response
if err := json.Unmarshal(res.Body(), &response); err != nil {
return err
}
if response.Error != nil {
return response.Error
}
result = response.Result
} else {
var response proxyResponse
if err := json.Unmarshal(res.Body(), &response); err != nil {
return err
}
result = response.Result
}
if err := json.Unmarshal(result, out); err != nil {
return err
}
return nil
}
// BlockNumber returns the number of most recent block.
func (e *Etherscan) BlockNumber() (uint64, error) {
var out string
if err := e.Query("proxy", "eth_blockNumber", &out, nil); err != nil {
return 0, err
}
return parseUint64orHex(out)
}
// GetBlockByNumber returns information about a block by block number.
func (e *Etherscan) GetBlockByNumber(i web3.BlockNumber, full bool) (*web3.Block, error) {
var b *web3.Block
params := map[string]string{
"tag": i.String(),
"boolean": strconv.FormatBool(full),
}
if err := e.Query("proxy", "eth_getBlockByNumber", &b, params); err != nil {
return nil, err
}
return b, nil
}
type ContractCode struct {
SourceCode string
ContractName string
Runs string
CompilerVersion string
ConstructorArguments string
}
func (e *Etherscan) GetContractCode(addr web3.Address) (*ContractCode, error) {
var out []*ContractCode
err := e.Query("contract", "getsourcecode", &out, map[string]string{
"address": addr.String(),
})
if err != nil {
return nil, err
}
if len(out) != 1 {
return nil, fmt.Errorf("incorrect values")
}
return out[0], nil
}
func parseUint64orHex(str string) (uint64, error) {
base := 10
if strings.HasPrefix(str, "0x") {
str = str[2:]
base = 16
}
return strconv.ParseUint(str, base, 64)
}