-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_client.go
253 lines (219 loc) · 7.11 KB
/
create_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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package client
import (
"bytes"
"context"
"fmt"
"os"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
sdktypes "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/cosmos/go-bip39"
"github.com/tendermint/starport/starport/pkg/spn"
rpchttp "github.com/tendermint/tendermint/rpc/client/http"
)
const (
KeyringBackendTest = "test"
)
// ChainClient is client to interact with SPN.
type ChainClient struct {
factory tx.Factory
clientCtx client.Context
out *bytes.Buffer
Address string `json:"address"`
AddressPrefix string `json:"account_address_prefix"`
RPC string `json:"rpc"`
Key string `json:"key"`
Mnemonic string `json:"mnemonic"`
KeyringServiceName string `json:"keyring_service_name"`
HDPath string `json:"hd_path"`
Enabled bool `json:"enabled"`
ChainName string `json:"chain_name"`
Denom string `json:"denom"`
}
func CreateChainClient(nodeAddress, keyringServiceName, chainID, homePath string) (*ChainClient, error) {
kr, err := keyring.New(keyringServiceName, KeyringBackendTest, homePath, os.Stdin)
if err != nil {
return nil, err
}
wsClient, err := rpchttp.New(nodeAddress, "/websocket")
if err != nil {
return nil, err
}
out := &bytes.Buffer{}
clientCtx := spn.NewClientCtx(kr, wsClient, out, homePath).WithChainID(chainID).WithNodeURI(nodeAddress)
factory := spn.NewFactory(clientCtx)
return &ChainClient{
factory: factory,
clientCtx: clientCtx,
out: out,
}, nil
}
// ImportMnemonic is to import existing account mnemonic in keyring
func (c ChainClient) ImportMnemonic(keyName, mnemonic, hdPath string) (acc spn.Account, err error) {
acc, err = c.AccountCreate(keyName, mnemonic, hdPath)
if err != nil {
return acc, err
}
return acc, nil
}
// AccountCreate creates an account by name and mnemonic (optional) in the keyring.
func (c *ChainClient) AccountCreate(accountName, mnemonic, hdPath string) (spn.Account, error) {
if mnemonic == "" {
entropySeed, err := bip39.NewEntropy(256)
if err != nil {
return spn.Account{}, err
}
mnemonic, err = bip39.NewMnemonic(entropySeed)
if err != nil {
return spn.Account{}, err
}
}
algos, _ := c.clientCtx.Keyring.SupportedAlgorithms()
algo, err := keyring.NewSigningAlgoFromString(string(hd.Secp256k1Type), algos)
if err != nil {
return spn.Account{}, err
}
info, err := c.clientCtx.Keyring.NewAccount(accountName, mnemonic, "", hdPath, algo)
if err != nil {
return spn.Account{}, err
}
account := c.ToAccount(info)
account.Mnemonic = mnemonic
return account, nil
}
func (c ChainClient) ToAccount(info keyring.Info) spn.Account {
ko, _ := sdktypes.Bech32ifyAddressBytes(c.AddressPrefix, info.GetAddress())
return spn.Account{
Name: info.GetName(),
Address: ko,
}
}
// GetAccountBalances returns the balance of the account
func (c ChainClient) GetAccountBalances(address, denom string) (*sdktypes.Coin, error) {
res, err := banktypes.NewQueryClient(c.clientCtx).
Balance(context.Background(), &banktypes.QueryBalanceRequest{
Address: address,
Denom: denom,
})
if err != nil {
return nil, err
}
if res == nil {
return nil, fmt.Errorf("not able to fetch balance: got response nil")
}
return res.Balance, err
}
// AccountList returns a list of accounts.
func (c *ChainClient) AccountList() (accounts []spn.Account, err error) {
infos, err := c.clientCtx.Keyring.List()
if err != nil {
return nil, err
}
for _, info := range infos {
accounts = append(accounts, c.ToAccount(info))
}
return accounts, nil
}
// AccountGet retrieves an account by name from the keyring.
func (c ChainClient) AccountGet(accountName string) (spn.Account, error) {
info, err := c.clientCtx.Keyring.Key(accountName)
if err != nil {
return spn.Account{}, err
}
return c.ToAccount(info), nil
}
// GetContext return context of client
func (c *ChainClient) GetContext() client.Context {
return c.clientCtx
}
// GetKeyring return keyring of client
func (c *ChainClient) GetKeyring() keyring.Keyring {
return c.clientCtx.Keyring
}
// GetAccAddress return hex address from given account name
func (c *ChainClient) GetAccAddress(accountName string) (sdktypes.AccAddress, error) {
info, err := c.clientCtx.Keyring.Key(accountName)
if err != nil {
return nil, err
}
return info.GetAddress(), nil
}
// GetBondedValidators returns bonded validators list
func (c *ChainClient) GetBondedValidators() (stakingtypes.Validators, error) {
res, err := stakingtypes.NewQueryClient(c.clientCtx).
Validators(context.Background(), &stakingtypes.QueryValidatorsRequest{
Status: stakingtypes.BondStatusBonded,
})
if err != nil {
return nil, err
}
if res == nil {
return nil, fmt.Errorf("not able to fetch validators: got response nil")
}
return res.Validators, err
}
// GetUnbondingDelegations returns unbonding delegations of delegator address
func (c *ChainClient) GetUnbondingDelegations(address string) (stakingtypes.UnbondingDelegations, error) {
res, err := stakingtypes.NewQueryClient(c.clientCtx).
DelegatorUnbondingDelegations(context.Background(), &stakingtypes.QueryDelegatorUnbondingDelegationsRequest{
DelegatorAddr: address,
})
if err != nil {
return nil, err
}
if res == nil {
return nil, fmt.Errorf("not able to fetch unbonding delegations: got response nil")
}
return res.UnbondingResponses, err
}
// GetUnbondedValidators returns bonded validators list
func (c *ChainClient) GetUnbondedValidators() (stakingtypes.Validators, error) {
res, err := stakingtypes.NewQueryClient(c.clientCtx).
Validators(context.Background(), &stakingtypes.QueryValidatorsRequest{
Status: stakingtypes.BondStatusUnbonded,
})
if err != nil {
return nil, err
}
if res == nil {
return nil, fmt.Errorf("not able to fetch validators: got response nil")
}
return res.Validators, err
}
// GetDelegations returns delegation balance of delegator address
func (c *ChainClient) GetDelegations(address string) (stakingtypes.DelegationResponses, error) {
res, err := stakingtypes.NewQueryClient(c.clientCtx).
DelegatorDelegations(context.Background(), &stakingtypes.QueryDelegatorDelegationsRequest{
DelegatorAddr: address,
})
if err != nil {
return nil, err
}
if res == nil {
return nil, fmt.Errorf("not able to fetch delegator delegations: got response nil")
}
return res.DelegationResponses, err
}
func (c *ChainClient) GetAccountInfo(address string) (authtypes.AccountI, error) {
res, err := authtypes.NewQueryClient(c.clientCtx).
Account(context.Background(), &authtypes.QueryAccountRequest{
Address: address,
})
if err != nil {
return nil, err
}
if res == nil {
return nil, fmt.Errorf("not to fetch account numbers : got response nil")
}
var accountI authtypes.AccountI
err = c.GetContext().InterfaceRegistry.UnpackAny(res.Account, &accountI)
if err != nil {
return nil, err
}
return accountI, err
}