-
Notifications
You must be signed in to change notification settings - Fork 0
/
address.go
140 lines (116 loc) · 3.9 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
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
package navexplorer
import (
"encoding/json"
"fmt"
log "github.com/sirupsen/logrus"
"strings"
"time"
)
type ValidateAddress struct {
Valid bool `json:"isValid"`
Address string `json:"address"`
StakingAddress string `json:"stakingAddress"`
SpendingAddress string `json:"spendingAddress"`
ColdStaking bool `json:"isColdStaking"`
}
type Address struct {
Hash string `json:"hash"`
Received int64 `json:"received"`
ReceivedCount uint `json:"receivedCount"`
Sent int64 `json:"sent"`
SentCount uint `json:"sentCount"`
Staked int64 `json:"staked"`
StakedCount uint `json:"stakedCount"`
ColdStaked int64 `json:"coldStaked"`
ColdStakedCount uint `json:"coldStakedCount"`
ColdStakedSent int64 `json:"coldSent"`
ColdStakedReceived int64 `json:"coldStakedReceived"`
ColdStakedBalance int64 `json:"coldStakedBalance"`
Balance int64 `json:"balance"`
Position int64 `json:"position"`
}
type Transaction struct {
Time time.Time `json:"time"`
Address string `json:"address"`
Type string `json:"type"`
Transaction string `json:"transaction"`
Height int `json:"height"`
Balance float64 `json:"balance"`
Sent float64 `json:"sent"`
Received float64 `json:"received"`
ColdStaking bool `json:"coldStaking"`
ColdStakingBalance float64 `json:"coldStakingBalance"`
ColdStakingSent float64 `json:"coldStakingSent"`
ColdStakingReceived float64 `json:"coldStakingReceived"`
}
type Balance struct {
Hash string `json:"hash"`
Height uint64 `json:"height"`
Spendable uint64 `json:"spendable"`
Stakable uint64 `json:"stakable"`
VotingWeight uint64 `json:"voting_weight"`
}
type TransactionType string
const (
TX_SEND TransactionType = "SEND"
TX_RECEIVE TransactionType = "RECEIVE"
TX_STAKING TransactionType = "STAKING"
TX_COLD_STAKING TransactionType = "COLD_STAKING"
)
func (e *ExplorerApi) GetAddresses(page int, size int) (addresses []Address, paginator Paginator, err error) {
method := fmt.Sprintf("/address?page=%d&size=%d", page, size)
log.Info(method)
response, paginator, err := e.client.call(method)
if err != nil {
return
}
err = json.Unmarshal(response, &addresses)
return
}
func (e *ExplorerApi) GetAddress(hash string) (address Address, err error) {
method := fmt.Sprintf("/address/%s", hash)
log.Info(method)
response, _, err := e.client.call(method)
if err != nil {
return
}
err = json.Unmarshal(response, &address)
return
}
func (e *ExplorerApi) ValidateAddress(hash string) (validateAddress ValidateAddress, err error) {
method := fmt.Sprintf("/address/%s/validate", hash)
log.Info(method)
response, _, err := e.client.call(method)
if err != nil {
return
}
err = json.Unmarshal(response, &validateAddress)
return
}
func (e *ExplorerApi) GetAddressTransactions(hash string, filters []TransactionType, page int, size int) (transactions []Transaction, paginator Paginator, err error) {
method := fmt.Sprintf("/address/%s/tx?page=%d&size=%d&filters=%s", hash, page, size, filtersToString(filters))
log.Info(method)
response, paginator, err := e.client.call(method)
if err != nil {
return
}
err = json.Unmarshal(response, &transactions)
return
}
func (e *ExplorerApi) GetBalances(addresses []string) (balances []Balance, err error) {
method := fmt.Sprintf("/balance?addresses=%s", strings.Join(addresses, ","))
log.Info(method)
response, _, err := e.client.call(method)
if err != nil {
return
}
err = json.Unmarshal(response, &balances)
return
}
func filtersToString(filters []TransactionType) string {
filterString := make([]string, len(filters))
for i := range filters {
filterString[i] = string(filters[i])
}
return strings.Join(filterString, ",")
}