/
block.go
51 lines (46 loc) · 1.25 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
// APACHE NOTICE
// Sourced with modifications from https://github.com/strangelove-ventures/lens
package query
import (
coretypes "github.com/cometbft/cometbft/rpc/core/types"
)
func BlockResultsRPC(q *Query) (*coretypes.ResultBlockResults, error) {
var height int64
ctx, cancel := q.GetQueryContext()
defer cancel()
// If height is not specified, default value is 0, query the latest available block then
if q.Options.Height == 0 {
resStatus, err := q.Client.RPCClient.Status(ctx)
if err != nil {
return nil, err
}
height = resStatus.SyncInfo.LatestBlockHeight
} else {
height = q.Options.Height
}
res, err := q.Client.RPCClient.BlockResults(ctx, &height)
if err != nil {
return nil, err
}
return res, nil
}
func BlockRPC(q *Query) (*coretypes.ResultBlock, error) {
var height int64
ctx, cancel := q.GetQueryContext()
defer cancel()
// If height is not specified, default value is 0, query the latest available block then
if q.Options.Height == 0 {
resStatus, err := q.Client.RPCClient.Status(ctx)
if err != nil {
return nil, err
}
height = resStatus.SyncInfo.LatestBlockHeight
} else {
height = q.Options.Height
}
res, err := q.Client.RPCClient.Block(ctx, &height)
if err != nil {
return nil, err
}
return res, nil
}