Skip to content

Commit fc3f3ca

Browse files
committed
[dev] account module
1 parent 9ca3a8f commit fc3f3ca

File tree

2 files changed

+174
-6
lines changed

2 files changed

+174
-6
lines changed

account.go

Lines changed: 98 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,109 @@
77

88
package etherscan
99

10-
import "math/big"
10+
// AccountBalance gets ether balance for a single address
11+
func (c *Client) AccountBalance(address string) (balance *BigInt, err error) {
12+
param := M{
13+
"tag": "latest",
14+
"address": address,
15+
}
16+
balance = new(BigInt)
17+
err = c.call("account", "balance", param, balance)
18+
return
19+
}
1120

12-
// SingleAddrBalance get latest address balance
13-
func (c *Client) SingleAddrBalance(address string) (balance *big.Int, err error) {
21+
// MultiAccountBalance gets ether balance for multiple addresses in a single call
22+
func (c *Client) MultiAccountBalance(addresses ...string) (balances []AccountBalance, err error) {
1423
param := M{
1524
"tag": "latest",
25+
"address": addresses,
26+
}
27+
balances = make([]AccountBalance, 0, len(addresses))
28+
err = c.call("account", "balancemulti", param, &balances)
29+
return
30+
}
31+
32+
// NormalTxByAddress gets a list of "normal" transactions by address
33+
// startBlock and endBlock can be nil
34+
// if desc is true, result will be sorted in blockNum descendant order.
35+
func (c *Client) NormalTxByAddress(address string, startBlock *int, endBlock *int, page int, offset int, desc bool) (txs []NormalTx, err error) {
36+
param := M{
37+
"address": address,
38+
"page": page,
39+
"offset": offset,
40+
}
41+
compose(param, "startblock", startBlock)
42+
compose(param, "endblock", endBlock)
43+
if desc {
44+
param["sort"] = "desc"
45+
} else {
46+
param["sort"] = "asc"
47+
}
48+
49+
err = c.call("account", "txlist", param, &txs)
50+
return
51+
}
52+
53+
// InternalTxByAddress gets a list of "internal" transactions by address
54+
// startBlock and endBlock can be nil
55+
// if desc is true, result will be sorted in descendant order.
56+
func (c *Client) InternalTxByAddress(address string, startBlock *int, endBlock *int, page int, offset int, desc bool) (txs []InternalTx, err error) {
57+
param := M{
1658
"address": address,
59+
"page": page,
60+
"offset": offset,
61+
}
62+
compose(param, "startblock", startBlock)
63+
compose(param, "endblock", endBlock)
64+
if desc {
65+
param["sort"] = "desc"
66+
} else {
67+
param["sort"] = "asc"
68+
}
69+
70+
err = c.call("account", "txlistinternal", param, &txs)
71+
return
72+
}
73+
74+
// ERC20TransferTx get a list of "erc20 - token transfer events" by
75+
// contract address and/or from/to address.
76+
// leave undesired condition to nil.
77+
func (c *Client) ERC20TransferTx(contractAddress, address *string, startBlock *int, endBlock *int, page int, offset int) (txs []ERC20Transfer, err error) {
78+
param := M{
79+
"page": page,
80+
"offset": offset,
81+
}
82+
compose(param, "contractaddress", contractAddress)
83+
compose(param, "address", address)
84+
compose(param, "startblock", startBlock)
85+
compose(param, "endblock", endBlock)
86+
87+
err = c.call("account", "tokentx", param, &txs)
88+
return
89+
}
90+
91+
// BlocksMinedByAddress gets list of blocks mined by address
92+
func (c *Client) BlocksMinedByAddress(address string, page int, offset int) (mined []MinedBlock, err error) {
93+
param := M{
94+
"address": address,
95+
"blocktype": "blocks",
96+
"page": page,
97+
"offset": offset,
98+
}
99+
100+
err = c.call("account", "getminedblocks", param, &mined)
101+
return
102+
}
103+
104+
// UnclesMinedByAddress gets list of uncles mined by address
105+
func (c *Client) UnclesMinedByAddress(address string, page int, offset int) (mined []MinedBlock, err error) {
106+
param := M{
107+
"address": address,
108+
"blocktype": "uncles",
109+
"page": page,
110+
"offset": offset,
17111
}
18112

19-
var b BigInt
20-
err = c.call("account", "balance", param, &b)
21-
balance = (*big.Int)(&b)
113+
err = c.call("account", "getminedblocks", param, &mined)
22114
return
23115
}

response.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,79 @@ type Envelope struct {
1818
// where response lies
1919
Result json.RawMessage `json:"result"`
2020
}
21+
22+
// AccountBalance account and its balance in pair
23+
type AccountBalance struct {
24+
Account string `json:"account"`
25+
Balance *BigInt `json:"balance"`
26+
}
27+
28+
// NormalTx holds info from normal tx query
29+
type NormalTx struct {
30+
BlockNumber int `json:"blockNumber,string"`
31+
TimeStamp Time `json:"timeStamp"`
32+
Hash string `json:"hash"`
33+
Nonce int `json:"nonce,string"`
34+
BlockHash string `json:"blockHash"`
35+
TransactionIndex int `json:"transactionIndex,string"`
36+
From string `json:"from"`
37+
To string `json:"to"`
38+
Value *BigInt `json:"value"`
39+
Gas int `json:"gas,string"`
40+
GasPrice *BigInt `json:"gasPrice"`
41+
IsError int `json:"isError,string"`
42+
TxReceiptStatus string `json:"txreceipt_status"`
43+
Input string `json:"input"`
44+
ContractAddress string `json:"contractAddress"`
45+
CumulativeGasUsed int `json:"cumulativeGasUsed,string"`
46+
GasUsed int `json:"gasUsed,string"`
47+
Confirmations int `json:"confirmations,string"`
48+
}
49+
50+
// InternalTx holds info from internal tx query
51+
type InternalTx struct {
52+
BlockNumber int `json:"blockNumber,string"`
53+
TimeStamp Time `json:"timeStamp"`
54+
Hash string `json:"hash"`
55+
From string `json:"from"`
56+
To string `json:"to"`
57+
Value *BigInt `json:"value"`
58+
ContractAddress string `json:"contractAddress"`
59+
Input string `json:"input"`
60+
Type string `json:"type"`
61+
Gas int `json:"gas,string"`
62+
GasUsed int `json:"gasUsed,string"`
63+
TraceID int `json:"traceId,string"`
64+
IsError int `json:"isError,string"`
65+
ErrCode string `json:"errCode"`
66+
}
67+
68+
// ERC20Transfer holds info from ERC20 token transfer event query
69+
type ERC20Transfer struct {
70+
BlockNumber int `json:"blockNumber,string"`
71+
TimeStamp Time `json:"timeStamp"`
72+
Hash string `json:"hash"`
73+
Nonce int `json:"nonce,string"`
74+
BlockHash string `json:"blockHash"`
75+
From string `json:"from"`
76+
ContractAddress string `json:"contractAddress"`
77+
To string `json:"to"`
78+
Value *BigInt `json:"value"`
79+
TokenName string `json:"tokenName"`
80+
TokenSymbol string `json:"tokenSymbol"`
81+
TokenDecimal uint8 `json:"tokenDecimal,string"`
82+
TransactionIndex int `json:"transactionIndex,string"`
83+
Gas int `json:"gas,string"`
84+
GasPrice *BigInt `json:"gasPrice"`
85+
GasUsed int `json:"gasUsed,string"`
86+
CumulativeGasUsed int `json:"cumulativeGasUsed,string"`
87+
Input string `json:"input"`
88+
Confirmations int `json:"confirmations,string"`
89+
}
90+
91+
// MinedBlock holds info from query for mined block by address
92+
type MinedBlock struct {
93+
BlockNumber int `json:"blockNumber,string"`
94+
TimeStamp Time `json:"timeStamp"`
95+
BlockReward *BigInt `json:"blockReward"`
96+
}

0 commit comments

Comments
 (0)