-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathaddress.go
67 lines (60 loc) · 1.5 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
package blockchain
import (
"fmt"
"net/url"
"strconv"
)
type Address struct {
Hash160 string
Address string
TransactionCount int64 `json:"n_tx"`
TotalReceived int64 `json:"total_received"`
TotalSent int64 `json:"total_sent"`
FinalBalance int64 `json:"final_balance"`
Transactions []Transaction `json:"txs"`
// These are used for the NextTransaction iterator.
bc *BlockChain
txOffset int
txPosition int
txLimit int
TxSortDescending bool
}
func (a *Address) NextTransaction() (Transaction, error) {
if a.txPosition < len(a.Transactions) {
a.txPosition = a.txPosition + 1
return a.Transactions[a.txPosition-1], nil
}
if len(a.Transactions) < a.txLimit {
return Transaction{}, IterDone
}
a.Transactions = nil
if err := a.load(a.bc); err != nil {
return Transaction{}, err
}
return a.NextTransaction()
}
func (a *Address) addressURL() string {
v := url.Values{}
v.Set("format", "json")
if a.TxSortDescending {
v.Set("sort", "0")
} else {
v.Set("sort", "1")
}
v.Set("offset", strconv.Itoa(a.txOffset))
v.Set("limit", strconv.Itoa(a.txLimit))
return fmt.Sprintf("%s/address/%s?%s", rootURL, a.Address, v.Encode())
}
func (a *Address) load(bc *BlockChain) error {
a.bc = bc
if a.txLimit == 0 {
a.txLimit = maxTransactionLimit
}
url := a.addressURL()
if err := bc.httpGetJSON(url, a); err != nil {
return err
}
a.txOffset = a.txOffset + a.txLimit
a.txPosition = 0
return nil
}