forked from nanmu42/etherscan-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
270 lines (245 loc) · 9.59 KB
/
response.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
/*
* Copyright (c) 2018 LI Zhennan
*
* Use of this work is governed by a MIT License.
* You may find a license copy in project root.
*/
package etherscan
import (
"encoding/json"
"fmt"
"strconv"
"strings"
)
// Envelope is the carrier of nearly every response
type Envelope struct {
// 1 for good, 0 for error
Status int `json:"status,string"`
// OK for good, other words when Status equals 0
Message string `json:"message"`
// where response lies
Result json.RawMessage `json:"result"`
}
// AccountBalance account and its balance in pair
type AccountBalance struct {
Account string `json:"account"`
Balance *BigInt `json:"balance"`
}
// NormalTx holds info from normal tx query
type NormalTx struct {
BlockNumber int `json:"blockNumber,string"`
TimeStamp Time `json:"timeStamp"`
Hash string `json:"hash"`
Nonce int `json:"nonce,string"`
BlockHash string `json:"blockHash"`
TransactionIndex int `json:"transactionIndex,string"`
From string `json:"from"`
To string `json:"to"`
Value *BigInt `json:"value"`
Gas *BigInt `json:"gas"`
GasPrice *BigInt `json:"gasPrice"`
IsError int `json:"isError,string"`
TxReceiptStatus string `json:"txreceipt_status"`
Input string `json:"input"`
ContractAddress string `json:"contractAddress"`
CumulativeGasUsed *BigInt `json:"cumulativeGasUsed"`
GasUsed *BigInt `json:"gasUsed"`
Confirmations int `json:"confirmations,string"`
FunctionName string `json:"functionName"`
MethodId string `json:"methodId"`
}
// InternalTx holds info from internal tx query
type InternalTx struct {
BlockNumber int `json:"blockNumber,string"`
TimeStamp Time `json:"timeStamp"`
Hash string `json:"hash"`
From string `json:"from"`
To string `json:"to"`
Value *BigInt `json:"value"`
ContractAddress string `json:"contractAddress"`
Input string `json:"input"`
Type string `json:"type"`
Gas int `json:"gas,string"`
GasUsed int `json:"gasUsed,string"`
TraceID string `json:"traceId"`
IsError int `json:"isError,string"`
ErrCode string `json:"errCode"`
}
// ERC20Transfer holds info from ERC20 token transfer event query
type ERC20Transfer struct {
BlockNumber int `json:"blockNumber,string"`
TimeStamp Time `json:"timeStamp"`
Hash string `json:"hash"`
Nonce int `json:"nonce,string"`
BlockHash string `json:"blockHash"`
From string `json:"from"`
ContractAddress string `json:"contractAddress"`
To string `json:"to"`
Value *BigInt `json:"value"`
TokenName string `json:"tokenName"`
TokenSymbol string `json:"tokenSymbol"`
TokenDecimal uint8 `json:"tokenDecimal,string"`
TransactionIndex int `json:"transactionIndex,string"`
Gas int `json:"gas,string"`
GasPrice *BigInt `json:"gasPrice"`
GasUsed int `json:"gasUsed,string"`
CumulativeGasUsed int `json:"cumulativeGasUsed,string"`
Input string `json:"input"`
Confirmations int `json:"confirmations,string"`
}
// ERC721Transfer holds info from ERC721 token transfer event query
type ERC721Transfer struct {
BlockNumber int `json:"blockNumber,string"`
TimeStamp Time `json:"timeStamp"`
Hash string `json:"hash"`
Nonce int `json:"nonce,string"`
BlockHash string `json:"blockHash"`
From string `json:"from"`
ContractAddress string `json:"contractAddress"`
To string `json:"to"`
TokenID *BigInt `json:"tokenID"`
TokenName string `json:"tokenName"`
TokenSymbol string `json:"tokenSymbol"`
TokenDecimal uint8 `json:"tokenDecimal,string"`
TransactionIndex int `json:"transactionIndex,string"`
Gas int `json:"gas,string"`
GasPrice *BigInt `json:"gasPrice"`
GasUsed int `json:"gasUsed,string"`
CumulativeGasUsed int `json:"cumulativeGasUsed,string"`
Input string `json:"input"`
Confirmations int `json:"confirmations,string"`
}
// ERC1155Transfer holds info from ERC1155 token transfer event query
type ERC1155Transfer struct {
BlockNumber int `json:"blockNumber,string"`
TimeStamp Time `json:"timeStamp"`
Hash string `json:"hash"`
Nonce int `json:"nonce,string"`
BlockHash string `json:"blockHash"`
From string `json:"from"`
ContractAddress string `json:"contractAddress"`
To string `json:"to"`
TokenID *BigInt `json:"tokenID"`
TokenName string `json:"tokenName"`
TokenSymbol string `json:"tokenSymbol"`
TokenDecimal uint8 `json:"tokenDecimal,string"`
TokenValue uint8 `json:"tokenValue,string"`
TransactionIndex int `json:"transactionIndex,string"`
Gas int `json:"gas,string"`
GasPrice *BigInt `json:"gasPrice"`
GasUsed int `json:"gasUsed,string"`
CumulativeGasUsed int `json:"cumulativeGasUsed,string"`
Input string `json:"input"`
Confirmations int `json:"confirmations,string"`
}
// MinedBlock holds info from query for mined block by address
type MinedBlock struct {
BlockNumber int `json:"blockNumber,string"`
TimeStamp Time `json:"timeStamp"`
BlockReward *BigInt `json:"blockReward"`
}
// ContractSource holds info from query for contract source code
type ContractSource struct {
SourceCode string `json:"SourceCode"`
ABI string `json:"ABI"`
ContractName string `json:"ContractName"`
CompilerVersion string `json:"CompilerVersion"`
OptimizationUsed int `json:"OptimizationUsed,string"`
Runs int `json:"Runs,string"`
ConstructorArguments string `json:"ConstructorArguments"`
EVMVersion string `json:"EVMVersion"`
Library string `json:"Library"`
LicenseType string `json:"LicenseType"`
Proxy string `json:"Proxy"`
Implementation string `json:"Implementation"`
SwarmSource string `json:"SwarmSource"`
}
// ExecutionStatus holds info from query for transaction execution status
type ExecutionStatus struct {
// 0 = pass, 1 = error
IsError int `json:"isError,string"`
ErrDescription string `json:"errDescription"`
}
// BlockRewards holds info from query for block and uncle rewards
type BlockRewards struct {
BlockNumber int `json:"blockNumber,string"`
TimeStamp Time `json:"timeStamp"`
BlockMiner string `json:"blockMiner"`
BlockReward *BigInt `json:"blockReward"`
Uncles []struct {
Miner string `json:"miner"`
UnclePosition int `json:"unclePosition,string"`
BlockReward *BigInt `json:"blockreward"`
} `json:"uncles"`
UncleInclusionReward *BigInt `json:"uncleInclusionReward"`
}
// LatestPrice holds info from query for latest ether price
type LatestPrice struct {
ETHBTC float64 `json:"ethbtc,string"`
ETHBTCTimestamp Time `json:"ethbtc_timestamp"`
ETHUSD float64 `json:"ethusd,string"`
ETHUSDTimestamp Time `json:"ethusd_timestamp"`
}
type Log struct {
Address string `json:"address"`
Topics []string `json:"topics"`
Data string `json:"data"`
BlockNumber string `json:"blockNumber"`
TransactionHash string `json:"transactionHash"`
BlockHash string `json:"blockHash"`
LogIndex string `json:"logIndex"`
Removed bool `json:"removed"`
}
// GasPrices holds info for Gas Oracle queries
// Gas Prices are returned in Gwei
type GasPrices struct {
LastBlock int
SafeGasPrice float64
ProposeGasPrice float64
FastGasPrice float64
SuggestBaseFeeInGwei float64 `json:"suggestBaseFee"`
GasUsedRatio []float64 `json:"gasUsedRatio"`
}
func (gp *GasPrices) UnmarshalJSON(data []byte) error {
_gp := struct {
LastBlock string
SafeGasPrice string
ProposeGasPrice string
FastGasPrice string
SuggestBaseFeeInGwei string `json:"suggestBaseFee"`
GasUsedRatio string `json:"gasUsedRatio"`
}{}
err := json.Unmarshal(data, &_gp)
if err != nil {
return err
}
gp.LastBlock, err = strconv.Atoi(_gp.LastBlock)
if err != nil {
return fmt.Errorf("Unable to convert LastBlock %s to int: %w", _gp.LastBlock, err)
}
gp.SafeGasPrice, err = strconv.ParseFloat(_gp.SafeGasPrice, 64)
if err != nil {
return fmt.Errorf("Unable to convert SafeGasPrice %s to float64: %w", _gp.SafeGasPrice, err)
}
gp.ProposeGasPrice, err = strconv.ParseFloat(_gp.ProposeGasPrice, 64)
if err != nil {
return fmt.Errorf("Unable to convert ProposeGasPrice %s to float64: %w", _gp.ProposeGasPrice, err)
}
gp.FastGasPrice, err = strconv.ParseFloat(_gp.FastGasPrice, 64)
if err != nil {
return fmt.Errorf("Unable to convert FastGasPrice %s to float64: %w", _gp.FastGasPrice, err)
}
gp.SuggestBaseFeeInGwei, err = strconv.ParseFloat(_gp.SuggestBaseFeeInGwei, 64)
if err != nil {
return fmt.Errorf("Unable to convert SuggestBaseFeeInGwei %s to float64: %w", _gp.SuggestBaseFeeInGwei, err)
}
gasRatios := strings.Split(_gp.GasUsedRatio, ",")
gp.GasUsedRatio = make([]float64, len(gasRatios))
for i, gasRatio := range gasRatios {
gp.GasUsedRatio[i], err = strconv.ParseFloat(gasRatio, 64)
if err != nil {
return fmt.Errorf("Unable to convert gasRatio %s to float64: %w", gasRatio, err)
}
}
return nil
}