-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
blocks.go
114 lines (103 loc) · 3.17 KB
/
blocks.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
package whatsonchain
import (
"context"
"encoding/json"
"fmt"
"net/http"
)
// GetBlockByHash this endpoint retrieves block details with given hash.
//
// For more information: https://developers.whatsonchain.com/#get-by-hash
func (c *Client) GetBlockByHash(ctx context.Context, hash string) (blockInfo *BlockInfo, err error) {
var resp string
// https://api.whatsonchain.com/v1/bsv/<network>/block/hash/<hash>
if resp, err = c.request(
ctx,
fmt.Sprintf("%s%s/block/hash/%s", apiEndpoint, c.Network(), hash),
http.MethodGet, nil,
); err != nil {
return
}
if len(resp) == 0 {
return nil, ErrBlockNotFound
}
err = json.Unmarshal([]byte(resp), &blockInfo)
return
}
// GetBlockByHeight this endpoint retrieves block details with given block height.
//
// For more information: https://developers.whatsonchain.com/#get-by-height
func (c *Client) GetBlockByHeight(ctx context.Context, height int64) (blockInfo *BlockInfo, err error) {
var resp string
// https://api.whatsonchain.com/v1/bsv/<network>/block/height/<height>
if resp, err = c.request(
ctx,
fmt.Sprintf("%s%s/block/height/%d", apiEndpoint, c.Network(), height),
http.MethodGet, nil,
); err != nil {
return
}
if len(resp) == 0 {
return nil, ErrBlockNotFound
}
err = json.Unmarshal([]byte(resp), &blockInfo)
return
}
// GetBlockPages if the block has more than 1000 transactions the page URIs will
// be provided in the "pages element" when getting a block by hash or height.
//
// For more information: https://developers.whatsonchain.com/#get-block-pages
func (c *Client) GetBlockPages(ctx context.Context, hash string, page int) (txList BlockPagesInfo, err error) {
var resp string
// https://api.whatsonchain.com/v1/bsv/<network>/block/hash/<hash>/page/1
if resp, err = c.request(
ctx,
fmt.Sprintf("%s%s/block/hash/%s/page/%d", apiEndpoint, c.Network(), hash, page),
http.MethodGet, nil,
); err != nil {
return
}
if len(resp) == 0 {
return nil, ErrBlockNotFound
}
err = json.Unmarshal([]byte(resp), &txList)
return
}
// GetHeaderByHash this endpoint retrieves block header details with given hash.
//
// For more information: https://developers.whatsonchain.com/#get-header-by-hash
func (c *Client) GetHeaderByHash(ctx context.Context, hash string) (headerInfo *BlockInfo, err error) {
var resp string
// https://api.whatsonchain.com/v1/bsv/<network>/block/<hash>/header
if resp, err = c.request(
ctx,
fmt.Sprintf("%s%s/block/%s/header", apiEndpoint, c.Network(), hash),
http.MethodGet, nil,
); err != nil {
return
}
if len(resp) == 0 {
return nil, ErrBlockNotFound
}
err = json.Unmarshal([]byte(resp), &headerInfo)
return
}
// GetHeaders this endpoint retrieves last 10 block headers.
//
// For more information: https://developers.whatsonchain.com/#get-headers
func (c *Client) GetHeaders(ctx context.Context) (blockHeaders []*BlockInfo, err error) {
var resp string
// https://api.whatsonchain.com/v1/bsv/<network>/block/headers
if resp, err = c.request(
ctx,
fmt.Sprintf("%s%s/block/headers", apiEndpoint, c.Network()),
http.MethodGet, nil,
); err != nil {
return
}
if len(resp) == 0 {
return nil, ErrHeadersNotFound
}
err = json.Unmarshal([]byte(resp), &blockHeaders)
return
}