-
Notifications
You must be signed in to change notification settings - Fork 8
/
address.go
88 lines (72 loc) 路 2.04 KB
/
address.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
package api
import (
"encoding/json"
"strconv"
)
type AddressResponse struct {
Jsonrpc string `json:"jsonrpc"`
ID string `json:"id,omitempty"`
Result *AddressResult `json:"result,omitempty"`
Error *Error `json:"error,omitempty"`
}
type Balance struct {
ID string `json:"coin_id"`
Symbol string `json:"symbol"`
Value string `json:"value"`
}
type AddressResult struct {
Balance []Balance `json:"balances"`
TransactionCount string `json:"transaction_count"`
}
// Returns coins list, balance and transaction count (for nonce) of an address.
func (a *Api) Address(address string) (*AddressResult, error) {
return a.AddressAtHeight(address, LatestBlockHeight)
}
// Returns coins list, balance and transaction count (for nonce) of an address.
func (a *Api) AddressAtHeight(address string, height int) (*AddressResult, error) {
params := make(map[string]string)
params["address"] = address
if height > 0 {
params["height"] = strconv.Itoa(height)
}
res, err := a.client.R().SetQueryParams(params).Get("/address")
if err != nil {
return nil, err
}
if res.IsError() {
return nil, NewResponseError(res)
}
response := new(AddressResponse)
err = json.Unmarshal(res.Body(), response)
if err != nil {
return nil, err
}
if response.Error != nil {
return nil, response.Error
}
return response.Result, nil
}
// Returns balance of an address.
func (a *Api) Balance(address string) ([]Balance, error) {
return a.BalanceAtHeight(address, LatestBlockHeight)
}
// Returns balance of an address.
func (a *Api) BalanceAtHeight(address string, height int) ([]Balance, error) {
response, err := a.AddressAtHeight(address, height)
if err != nil {
return nil, err
}
return response.Balance, nil
}
// Returns next transaction number (nonce) of an address.
func (a *Api) Nonce(address string) (uint64, error) {
response, err := a.Address(address)
if err != nil {
return 0, err
}
nonce, err := strconv.ParseUint(response.TransactionCount, 10, 64)
if err != nil {
return 0, err
}
return nonce + 1, nil
}