-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
77 lines (61 loc) · 1.94 KB
/
client.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
package gosm
import (
"context"
"golang.org/x/mod/semver"
wasm_types "github.com/CosmWasm/wasmd/x/wasm/types"
rpc_http "github.com/cometbft/cometbft/rpc/client/http"
core_types "github.com/cometbft/cometbft/rpc/core/types"
jsonrpc_client "github.com/cometbft/cometbft/rpc/jsonrpc/client"
)
const (
cometEncodingThreshold = "v0.37.0-alpha"
)
//go:generate mockery --name GosmClient --filename mock_gosm_client.go
type GosmClient interface {
// cometbft
ABCIQuery(ctx context.Context, path string, req Marshaler) (*core_types.ResultABCIQuery, error)
Block(ctx context.Context, height *int64) (*core_types.ResultBlock, error)
BlockResults(ctx context.Context, height *int64) (*core_types.ResultBlockResults, error)
CometLegacyEncoding() bool
// cosmwasm
QuerySmartContractState(ctx context.Context, address string, query any) (*wasm_types.QuerySmartContractStateResponse, error)
}
type RPCClient struct {
chainID string
rpcConn CometClient
rpcCaller jsonrpc_client.Caller
cometLegacyEncoding bool
}
var _ GosmClient = (*RPCClient)(nil)
func NewRPCClient(
chainID string,
rpcConn CometClient,
rpcCaller jsonrpc_client.Caller,
cometLegacyEncoding bool,
) *RPCClient {
return &RPCClient{
chainID: chainID,
rpcConn: rpcConn,
rpcCaller: rpcCaller,
cometLegacyEncoding: cometLegacyEncoding,
}
}
func Dial(ctx context.Context, rpcEndpoint string) (*RPCClient, error) {
rpcConn, err := rpc_http.New(rpcEndpoint, "/websocket")
if err != nil {
return nil, err
}
rpcCaller, err := jsonrpc_client.New(rpcEndpoint)
if err != nil {
return nil, err
}
status, err := rpcConn.Status(ctx)
if err != nil {
return nil, err
}
client := NewRPCClient(status.NodeInfo.Network, rpcConn, rpcCaller, useLegacyEncodedEvents(status.NodeInfo.Version))
return client, nil
}
func useLegacyEncodedEvents(version string) bool {
return semver.Compare("v"+version, cometEncodingThreshold) < 0
}