-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc.go
53 lines (43 loc) · 1.39 KB
/
rpc.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 client
import (
"context"
"fmt"
"time"
rpc "github.com/cometbft/cometbft/rpc/client/http"
tmctypes "github.com/cometbft/cometbft/rpc/core/types"
)
type RPCClient struct {
client *rpc.HTTP
}
// ConnectRPCWithTimeout connects RPC client connection with timeout.
func ConnectRPCWithTimeout(addr string, timeout time.Duration) (*RPCClient, error) {
rpcClient, err := rpc.NewWithTimeout(addr, "/websocket", uint(timeout.Seconds()))
if err != nil {
return nil, err
}
return &RPCClient{client: rpcClient}, nil
}
// Block returns block information for the height.
func (c *RPCClient) Block(ctx context.Context, height int64) (*tmctypes.ResultBlock, error) {
return c.client.Block(ctx, &height)
}
// LatestBlockHeight returns the latest block height on the network.
func (c *RPCClient) LatestBlockHeight(ctx context.Context) (int64, error) {
resp, err := c.client.Status(ctx)
if err != nil {
return 0, err
}
return resp.SyncInfo.LatestBlockHeight, nil
}
// NetworkChainID returns network chain id.
func (c *RPCClient) NetworkChainID(ctx context.Context) (string, error) {
status, err := c.client.Status(ctx)
if err != nil {
return "", fmt.Errorf("failed to get status: %w", err)
}
return status.NodeInfo.Network, nil
}
// Status returns the status of the blockchain network.
func (c *RPCClient) Status(ctx context.Context) (*tmctypes.ResultStatus, error) {
return c.client.Status(ctx)
}