-
Notifications
You must be signed in to change notification settings - Fork 8
/
block.go
57 lines (48 loc) 路 1.38 KB
/
block.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
package api
import (
"encoding/json"
"strconv"
"time"
)
type BlockResponse struct {
Jsonrpc string `json:"jsonrpc"`
ID string `json:"id,omitempty"`
Result *BlockResult `json:"result,omitempty"`
Error *Error `json:"error,omitempty"`
}
type BlockResult struct {
Hash string `json:"hash"`
Height string `json:"height"`
Time time.Time `json:"time"`
NumTxs string `json:"num_txs"`
TotalTxs string `json:"total_txs"`
Transactions []TransactionResult `json:"transactions"`
BlockReward string `json:"block_reward"`
Size string `json:"size"`
Proposer string `json:"proposer"`
Validators []struct {
PubKey string `json:"pub_key"`
Signed bool `json:"signed"`
} `json:"validators"`
}
// Returns block data at given height.
func (a *Api) Block(height int) (*BlockResult, error) {
params := make(map[string]string)
params["height"] = strconv.Itoa(height)
res, err := a.client.R().SetQueryParams(params).Get("/block")
if err != nil {
return nil, err
}
if res.IsError() {
return nil, NewResponseError(res)
}
response := new(BlockResponse)
err = json.Unmarshal(res.Body(), response)
if err != nil {
return nil, err
}
if response.Error != nil {
return nil, response.Error
}
return response.Result, nil
}