-
Notifications
You must be signed in to change notification settings - Fork 40
/
client.go
183 lines (163 loc) · 4.64 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package ion
import (
"bytes"
"encoding/json"
"fmt"
"io"
"time"
"github.com/avast/retry-go/v4"
provtypes "github.com/cometbft/cometbft/light/provider"
prov "github.com/cometbft/cometbft/light/provider/http"
rpcclient "github.com/cometbft/cometbft/rpc/client"
rpchttp "github.com/cometbft/cometbft/rpc/client/http"
libclient "github.com/cometbft/cometbft/rpc/jsonrpc/client"
"github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/gogoproto/proto"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
)
var (
// Variables used for retries
RtyAttNum = uint(5)
RtyAtt = retry.Attempts(RtyAttNum)
RtyDel = retry.Delay(time.Millisecond * 400)
RtyErr = retry.LastErrorOnly(true)
)
type Client struct {
Codec Codec
Keybase keyring.Keyring
RPCClient rpcclient.Client
LightProvider provtypes.Provider
Input io.Reader
Output io.Writer
Config *ChainClientConfig
KeyringOptions []keyring.Option
}
// UnpackAny implements types.AnyUnpacker.
func (c *Client) UnpackAny(any *types.Any, iface interface{}) error {
return c.Codec.Marshaler.UnpackAny(any, iface)
}
func (c *Client) GetKeybase() keyring.Keyring {
return c.Keybase
}
func NewClient(cfg *ChainClientConfig, input io.Reader, output io.Writer, kro ...keyring.Option) (*Client, error) {
c := &Client{
KeyringOptions: kro,
Config: cfg,
Input: input,
Output: output,
Codec: MakeCodec(cfg.Modules, nil),
}
return c.init()
}
func (c *Client) init() (*Client, error) {
keybase, err := keyring.New(c.Config.ChainID, c.Config.KeyringBackend, c.Config.KeyDirectory, c.Input, c.Codec.Marshaler, c.KeyringOptions...)
if err != nil {
log.WithField("err", err).Error("keybase new keyring error")
return nil, err
}
// TODO: figure out how to deal with input or maybe just make all keyring backends test?
timeout, _ := time.ParseDuration(c.Config.Timeout)
rpcClient, err := NewRPCClient(c.Config.RPCAddr, timeout)
if err != nil {
log.WithField("err", err).Error("keybase new keyring error")
return nil, err
}
lightprovider, err := prov.New(c.Config.ChainID, c.Config.RPCAddr)
if err != nil {
log.WithField("err", err).Error("failed to load light provider")
return nil, err
}
c.RPCClient = rpcClient
c.LightProvider = lightprovider
c.Keybase = keybase
return c, nil
}
func (cc *Client) GetKeyAddress() (addr sdk.AccAddress, err error) {
done := cc.SetSDKContext()
defer done()
info, err := cc.Keybase.Key(cc.Config.Key)
if err != nil {
log.WithField("component", "get-key-address").WithError(err).Error("Failed to get key from keybase")
return nil, err
}
addr, err = info.GetAddress()
if err != nil {
log.WithField("component", "get-key-address").WithError(err).Error("Failed to get address")
return nil, err
}
return addr, err
}
func NewRPCClient(addr string, timeout time.Duration) (*rpchttp.HTTP, error) {
httpClient, err := libclient.DefaultHTTPClient(addr)
if err != nil {
return nil, err
}
httpClient.Timeout = timeout
rpcClient, err := rpchttp.NewWithClient(addr, "/websocket", httpClient)
if err != nil {
return nil, err
}
return rpcClient, nil
}
// TODO: actually do something different here have a couple of levels of verbosity
func (cc *Client) PrintTxResponse(res *sdk.TxResponse) error {
return cc.PrintObject(res)
}
func (cc *Client) HandleAndPrintMsgSend(res *sdk.TxResponse, err error) error {
if err != nil {
if res != nil {
return fmt.Errorf("failed to withdraw rewards: code(%d) msg(%s)", res.Code, res.Logs)
}
return fmt.Errorf("failed to withdraw rewards: err(%w)", err)
}
return cc.PrintTxResponse(res)
}
func (cc *Client) PrintObject(res interface{}) error {
var (
bz []byte
err error
)
switch cc.Config.OutputFormat {
case "json":
if m, ok := res.(proto.Message); ok {
bz, err = cc.MarshalProto(m)
} else {
bz, err = json.Marshal(res)
}
if err != nil {
return err
}
case "indent":
if m, ok := res.(proto.Message); ok {
bz, err = cc.MarshalProto(m)
if err != nil {
return err
}
buf := bytes.NewBuffer([]byte{})
if err = json.Indent(buf, bz, "", " "); err != nil {
return err
}
bz = buf.Bytes()
} else {
bz, err = json.MarshalIndent(res, "", " ")
if err != nil {
return err
}
}
case "yaml":
bz, err = yaml.Marshal(res)
if err != nil {
return err
}
default:
return fmt.Errorf("unknown output type: %s", cc.Config.OutputFormat)
}
fmt.Fprint(cc.Output, string(bz), "\n")
return nil
}
func (cc *Client) MarshalProto(res proto.Message) ([]byte, error) {
return cc.Codec.Marshaler.MarshalJSON(res)
}