-
Notifications
You must be signed in to change notification settings - Fork 18
/
etherscan.go
142 lines (123 loc) · 3.58 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
package main
import (
"encoding/json"
"errors"
"math/big"
"strconv"
"strings"
"github.com/parnurzeal/gorequest"
)
var (
// APIKEY ethereum oauth token
APIKEY string
request *gorequest.SuperAgent
)
// AccountRespBody EtherScan Response body
type AccountRespBody struct {
Status string `json:"status"`
Message string `json:"message"`
Result string `json:"result"`
}
// ProxyRespBody EtherScan Response body
type ProxyRespBody struct {
JSONRPC string `json:"jsonrpc"`
ID int `json:"id"`
Result string `json:"result"`
}
func (es EtherScan) getBalance(address string) (*big.Int, error) {
resp, body, err := request.Get(etherscan.URL).Query(map[string]interface{}{
"module": "account",
"action": "balance",
"address": address,
"tag": "latest",
"apikey": APIKEY,
}).End()
if err != nil {
return nil, errors.New(strings.Join([]string{"etherscan: get balance error:", address, err[0].Error()}, " "))
}
if handleStatus(resp) {
var (
respBody = new(AccountRespBody)
balance = new(big.Int)
)
if err := json.Unmarshal([]byte(body), respBody); err != nil {
return nil, errors.New("etherscan getBalance Unmarshal error")
}
balance.SetString(respBody.Result, 10)
return balance, nil
}
return nil, errors.New("etherscan get balance error")
}
func (es EtherScan) getGasPrice() (*big.Int, error) {
resp, body, err := request.Get(etherscan.URL).Query(map[string]interface{}{
"module": "proxy",
"action": "eth_gasPrice",
"apikey": APIKEY,
}).End()
if err != nil {
return nil, errors.New(strings.Join([]string{"etherscan: get balance error:", err[0].Error()}, " "))
}
if handleStatus(resp) {
var (
respBody = new(ProxyRespBody)
gasPrice = new(big.Int)
)
if err := json.Unmarshal([]byte(body), respBody); err != nil {
return nil, errors.New("etherscan getBalance Unmarshal error")
}
resultWithoutHex := strings.Replace(respBody.Result, "0x", "", -1)
gasPrice.SetString(resultWithoutHex, 10)
return gasPrice, nil
}
return nil, errors.New("etherscan get gasPrice error")
}
func (es EtherScan) getAccountNonce(address string) (*uint64, error) {
resp, body, err := request.Get(etherscan.URL).Query(map[string]interface{}{
"module": "proxy",
"action": "eth_getTransactionCount",
"address": address,
"tag": "latest",
"apikey": APIKEY,
}).End()
if err != nil {
return nil, errors.New(strings.Join([]string{"etherscan: get account nonce error:", address, err[0].Error()}, " "))
}
if handleStatus(resp) {
var respBody = new(ProxyRespBody)
if err := json.Unmarshal([]byte(body), respBody); err != nil {
return nil, errors.New("etherscan getBalance Unmarshal error")
}
nonce, _ := strconv.ParseUint(strings.Replace(respBody.Result, "0x", "", -1), 16, 64)
return &nonce, nil
}
return nil, errors.New("etherscan get account nonce error")
}
func (es EtherScan) etherscanConstructTxField(address string) (*big.Int, *uint64, *big.Int, error) {
balance, err := es.getBalance(address)
if err != nil {
return nil, nil, nil, err
}
err = balanceIsLessThanConfig(address, balance)
if err != nil {
return nil, nil, nil, err
}
accountNonce, err := es.getAccountNonce(address)
if err != nil {
return nil, nil, nil, errors.New(strings.Join([]string{"etherscan: get account nonce error:", address, err.Error()}, " "))
}
gasPrice, err := es.getGasPrice()
if err != nil {
return nil, nil, nil, err
}
return balance, accountNonce, gasPrice, nil
}
func handleStatus(resp gorequest.Response) bool {
if resp.StatusCode == 200 {
return true
}
return false
}
func init() {
APIKEY = etherscan.Key
request = gorequest.New()
}