-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_chain.go
53 lines (47 loc) · 1.32 KB
/
client_chain.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
package fuel
import (
"context"
"fmt"
"github.com/sentioxyz/fuel-go/query"
"github.com/sentioxyz/fuel-go/types"
)
type GetChainOption struct {
Simple bool
GetBlockOption
}
func (o GetChainOption) BuildIgnoreChecker() query.IgnoreChecker {
if o.Simple {
return query.MergeIgnores(
query.IgnoreOtherFields(types.ChainInfo{}, "Name", "LatestBlock"),
o.GetBlockOption.BuildIgnoreChecker(),
)
}
return o.GetBlockOption.BuildIgnoreChecker()
}
func (c *Client) GetChain(ctx context.Context, opt GetChainOption) (types.ChainInfo, error) {
q := fmt.Sprintf("{ chain { %s} }",
query.Simple.GenObjectQuery(types.ChainInfo{}, opt.BuildIgnoreChecker()),
)
type resultType struct {
Chain types.ChainInfo `json:"chain"`
}
result, err := ExecuteQuery[resultType](ctx, c, q)
if err != nil {
return types.ChainInfo{}, err
}
return result.Chain, nil
}
func (c *Client) GetLatestBlockHeight(ctx context.Context) (types.U32, error) {
info, err := c.GetChain(ctx, GetChainOption{
Simple: true,
GetBlockOption: GetBlockOption{},
})
return info.LatestBlock.Height, err
}
func (c *Client) GetLatestBlockHeader(ctx context.Context) (types.Header, error) {
info, err := c.GetChain(ctx, GetChainOption{
Simple: true,
GetBlockOption: GetBlockOption{WithHeader: true},
})
return info.LatestBlock.Header, err
}