forked from blockcypher/gobcy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blockchain.go
65 lines (61 loc) · 1.57 KB
/
blockchain.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
package gobcy
import (
"errors"
"net/url"
"strconv"
)
//GetChain returns the current state of the
//configured Coin/Chain.
func (api *API) GetChain() (chain Blockchain, err error) {
u, err := api.buildURL("", nil)
if err != nil {
return
}
err = getResponse(u, &chain)
return
}
//GetBlock returns a Block based on either height
//or hash. If both height and hash are sent, it will
//throw an error.
func (api *API) GetBlock(height int, hash string, params map[string]string) (block Block, err error) {
var u *url.URL
ustr := "/blocks/"
if height != 0 && hash != "" {
err = errors.New("Func GetBlockPage: Cannot send both height and hash")
return
} else if height != 0 {
ustr = ustr + strconv.Itoa(height)
} else if hash != "" {
ustr = ustr + hash
}
u, err = api.buildURL(ustr, params)
if err != nil {
return
}
err = getResponse(u, &block)
return
}
//GetBlockNextTXs returns the the next page of TXids based
//on the NextTXs URL in this Block. If NextTXs is empty,
//this will return an error.
func (api *API) GetBlockNextTXs(this Block) (next Block, err error) {
if this.NextTXs == "" {
err = errors.New("Func GetNextTXs: This Block doesn't have more transactions")
return
}
if len(this.TXids) == 0 {
err = errors.New("Func GetNextTXs: This Block doesn't have any TXids in the array, meaning no more transactions")
return
}
txurl, err := url.Parse(this.NextTXs)
if err != nil {
return
}
params := make(map[string]string)
query := txurl.Query()
for k := range query {
params[k] = query.Get(k)
}
next, err = api.GetBlock(0, this.Hash, params)
return
}