/
external.go
132 lines (112 loc) · 3.54 KB
/
external.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
package EthereumAPI
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
//https://etherscan.io/apis
//https://testnet.etherscan.io/apis
//https://api.etherscan.io/api?module=account&action=txlist&address=0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae&startblock=0&endblock=99999999&sort=asc&apikey=YourApiKeyToken
//https://testnet.etherscan.io/api?module=account&action=txlist&address=0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae&startblock=0&endblock=99999999&sort=asc&apikey=YourApiKeyToken
var EtherscanTestNet bool = true
var EtherscanTestNetName string = "ropsten"
var EtherscanAPIKeyToken string = "UninitializedApiKey"
func CallRest(url string, dst interface{}) error {
postGet := "GET"
req, err := http.NewRequest(postGet, url, nil)
if err != nil {
return err
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
err = json.Unmarshal(body, dst)
if err != nil {
return err
}
return nil
}
type EtherscanResponse struct {
Status string `json:"status"`
Message string `json:"message"`
Result interface{} `json:"result"`
}
type EtherscanTransaction struct {
BlockNumber string `json:"blockNumber"`
TimeStamp string `json:"timeStamp"`
Hash string `json:"hash"`
Nonce string `json:"nonce"`
BlockHash string `json:"blockHash"`
TransactionIndex string `json:"transactionIndex"`
From string `json:"from"`
To string `json:"to"`
Value string `json:"value"`
Gas string `json:"gas"`
GasPrice string `json:"gasPrice"`
IsError string `json:"isError"`
Input string `json:"input"`
ContractAddress string `json:"contractAddress"`
CumulativeGasUsed string `json:"cumulativeGasUsed"`
GasUsed string `json:"gasUsed"`
Confirmations string `json:"confirmations"`
}
func (e *EtherscanTransaction) JSONByte() ([]byte, error) {
return EncodeJSON(e)
}
func (e *EtherscanTransaction) JSONString() (string, error) {
return EncodeJSONString(e)
}
func (e *EtherscanTransaction) JSONBuffer(b *bytes.Buffer) error {
return EncodeJSONToBuffer(e, b)
}
func (e *EtherscanTransaction) String() string {
str, _ := e.JSONString()
return str
}
func EtherscanTxList(address string) ([]*EtherscanTransaction, error) {
prefix := "api"
if EtherscanTestNet == true {
prefix = EtherscanTestNetName
}
url := fmt.Sprintf("https://%v.etherscan.io/api?module=account&action=txlist&address=%v&startblock=0&endblock=99999999&sort=asc&apikey=%v", prefix, address, EtherscanAPIKeyToken)
dst := new(EtherscanResponse)
dst.Result = []*EtherscanTransaction{}
err := CallRest(url, dst)
if err != nil {
return nil, err
}
list := []*EtherscanTransaction{}
err = MapToObject(dst.Result, &list)
if err != nil {
return nil, err
}
return list, nil
}
func EtherscanTxListWithStartBlock(address string, startBlock int64) ([]*EtherscanTransaction, error) {
prefix := "api"
if EtherscanTestNet == true {
prefix = EtherscanTestNetName
}
url := fmt.Sprintf("https://%v.etherscan.io/api?module=account&action=txlist&address=%v&startblock=%v&endblock=99999999&sort=asc&apikey=%v", prefix, address, startBlock, EtherscanAPIKeyToken)
dst := new(EtherscanResponse)
dst.Result = []*EtherscanTransaction{}
err := CallRest(url, dst)
if err != nil {
return nil, err
}
list := []*EtherscanTransaction{}
err = MapToObject(dst.Result, &list)
if err != nil {
return nil, err
}
return list, nil
}