-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
230 lines (197 loc) · 6.05 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package client
import (
"fmt"
"os"
"sync"
xWasmTypes "github.com/CosmWasm/wasmd/x/wasm/types"
rpcClient "github.com/cometbft/cometbft/rpc/client"
rpcHttp "github.com/cometbft/cometbft/rpc/client/http"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
cryptoTypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/types"
xAuthTypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/stafihub/neutron-relay-sdk/common/log"
)
type Client struct {
clientCtx client.Context
msgClient xWasmTypes.MsgClient
queryClient xWasmTypes.QueryClient
rpcClientList []rpcClient.Client
gasPrice string
denom string
accountNumber uint64
accountPrefix string
rpcClientIndex int
changeEndpointMutex sync.Mutex
logger log.Logger
}
func NewClient(k keyring.Keyring, fromName, gasPrice, accountPrefix string, endPointList []string, logger log.Logger) (*Client, error) {
if len(endPointList) == 0 {
return nil, fmt.Errorf("no endpoint")
}
if logger == nil {
return nil, fmt.Errorf("logger is nil")
}
encodingConfig := MakeEncodingConfig()
retClient := &Client{
accountPrefix: accountPrefix,
rpcClientIndex: 0,
logger: logger,
}
for _, endPoint := range endPointList {
rClient, err := rpcHttp.New(endPoint, "/websocket")
if err != nil {
return nil, err
}
retClient.rpcClientList = append(retClient.rpcClientList, rClient)
}
if len(fromName) != 0 {
info, err := k.Key(fromName)
if err != nil {
return nil, fmt.Errorf("keyring get address from name:%s err: %s", fromName, err)
}
fromAddress, err := info.GetAddress()
if err != nil {
return nil, err
}
initClientCtx := client.Context{}.
WithCodec(encodingConfig.Marshaler).
WithInterfaceRegistry(encodingConfig.InterfaceRegistry).
WithTxConfig(encodingConfig.TxConfig).
WithLegacyAmino(encodingConfig.Amino).
WithInput(os.Stdin).
WithAccountRetriever(xAuthTypes.AccountRetriever{}).
WithBroadcastMode(flags.BroadcastSync).
WithClient(retClient.rpcClientList[0]).
WithSkipConfirmation(true). //skip password confirm
WithFromName(fromName). //keyBase need FromName to find key info
WithFromAddress(fromAddress). //accountRetriever need FromAddress
WithKeyring(k)
retClient.clientCtx = initClientCtx
chainId, err := retClient.GetChainId()
if err != nil {
return nil, err
}
retClient.clientCtx = retClient.clientCtx.WithChainID(chainId)
account, err := retClient.GetAccount()
if err != nil {
return nil, fmt.Errorf("Client.GetAccount failed: %s", err)
}
retClient.accountNumber = account.GetAccountNumber()
if accountPrefix == "neutron" {
retClient.setDenom("untrn")
} else {
bondedDenom, err := retClient.QueryBondedDenom()
if err != nil {
return nil, err
}
retClient.setDenom(bondedDenom.Params.BondDenom)
}
err = retClient.SetGasPrice(gasPrice)
if err != nil {
return nil, err
}
retClient.msgClient = xWasmTypes.NewMsgClient(retClient.clientCtx)
retClient.queryClient = xWasmTypes.NewQueryClient(retClient.clientCtx)
} else {
initClientCtx := client.Context{}.
WithCodec(encodingConfig.Marshaler).
WithInterfaceRegistry(encodingConfig.InterfaceRegistry).
WithTxConfig(encodingConfig.TxConfig).
WithLegacyAmino(encodingConfig.Amino).
WithInput(os.Stdin).
WithAccountRetriever(xAuthTypes.AccountRetriever{}).
WithBroadcastMode(flags.BroadcastSync).
WithClient(retClient.rpcClientList[0]).
WithSkipConfirmation(true) //skip password confirm
retClient.clientCtx = initClientCtx
if accountPrefix == "neutron" {
retClient.setDenom("untrn")
} else {
bondedDenom, err := retClient.QueryBondedDenom()
if err != nil {
return nil, err
}
retClient.setDenom(bondedDenom.Params.BondDenom)
}
chainId, err := retClient.GetChainId()
if err != nil {
return nil, err
}
retClient.clientCtx = retClient.clientCtx.WithChainID(chainId)
retClient.queryClient = xWasmTypes.NewQueryClient(retClient.clientCtx)
}
return retClient, nil
}
func (c *Client) GetAccountPrefix() string {
return c.accountPrefix
}
func (c *Client) SetAccountPrefix(prefix string) {
c.accountPrefix = prefix
}
// SetFromName update clientCtx.FromName and clientCtx.FromAddress
func (c *Client) SetFromName(fromName string) error {
info, err := c.clientCtx.Keyring.Key(fromName)
if err != nil {
return fmt.Errorf("keyring get address from fromName err: %s", err)
}
fromAddress, err := info.GetAddress()
if err != nil {
return err
}
c.clientCtx = c.clientCtx.WithFromName(fromName).WithFromAddress(fromAddress)
account, err := c.GetAccount()
if err != nil {
return err
}
c.accountNumber = account.GetAccountNumber()
return nil
}
func (c *Client) GetFromName() string {
return c.clientCtx.FromName
}
func (c *Client) GetFromAddress() types.AccAddress {
return c.clientCtx.FromAddress
}
func (c *Client) SetGasPrice(gasPrice string) error {
_, err := types.ParseDecCoins(gasPrice)
if err != nil {
return err
}
c.gasPrice = gasPrice
return nil
}
func (c *Client) setDenom(denom string) {
c.denom = denom
}
func (c *Client) GetDenom() string {
return c.denom
}
func (c *Client) GetTxConfig() client.TxConfig {
return c.clientCtx.TxConfig
}
func (c *Client) GetLegacyAmino() *codec.LegacyAmino {
return c.clientCtx.LegacyAmino
}
func (c *Client) Sign(fromName string, toBeSigned []byte) ([]byte, cryptoTypes.PubKey, error) {
return c.clientCtx.Keyring.Sign(fromName, toBeSigned)
}
func (c *Client) Ctx() client.Context {
return c.clientCtx
}
func (c *Client) GetRpcClient() *rpcClient.Client {
return &c.rpcClientList[0]
}
func (c *Client) ChangeEndpoint() {
c.changeEndpointMutex.Lock()
defer c.changeEndpointMutex.Unlock()
willUseIndex := (c.rpcClientIndex + 1) % len(c.rpcClientList)
c.clientCtx = c.clientCtx.WithClient(c.rpcClientList[willUseIndex])
c.rpcClientIndex = willUseIndex
}
func (c *Client) CurrentEndpointIndex() int {
return c.rpcClientIndex
}