-
Notifications
You must be signed in to change notification settings - Fork 665
/
client.go
479 lines (438 loc) · 14.5 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package platformvm
import (
"time"
"github.com/ava-labs/avalanchego/api"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/formatting"
cjson "github.com/ava-labs/avalanchego/utils/json"
"github.com/ava-labs/avalanchego/utils/rpc"
)
// Client ...
type Client struct {
requester rpc.EndpointRequester
}
// NewClient returns a Client for interacting with the P Chain endpoint
func NewClient(uri string, requestTimeout time.Duration) *Client {
return &Client{
requester: rpc.NewEndpointRequester(uri, "/ext/P", "platform", requestTimeout),
}
}
// GetHeight returns the current block height of the P Chain
func (c *Client) GetHeight() (uint64, error) {
res := &GetHeightResponse{}
err := c.requester.SendRequest("getHeight", struct{}{}, res)
return uint64(res.Height), err
}
// ExportKey returns the private key corresponding to [address] from [user]'s account
func (c *Client) ExportKey(user api.UserPass, address string) (string, error) {
res := &ExportKeyReply{}
err := c.requester.SendRequest("exportKey", &ExportKeyArgs{
UserPass: user,
Address: address,
}, res)
return res.PrivateKey, err
}
// ImportKey imports the specified [privateKey] to [user]'s keystore
func (c *Client) ImportKey(user api.UserPass, privateKey string) (string, error) {
res := &api.JSONAddress{}
err := c.requester.SendRequest("importKey", &ImportKeyArgs{
UserPass: user,
PrivateKey: privateKey,
}, res)
return res.Address, err
}
// GetBalance returns the balance of [address] on the P Chain
func (c *Client) GetBalance(address string) (*GetBalanceResponse, error) {
res := &GetBalanceResponse{}
err := c.requester.SendRequest("getBalance", &api.JSONAddress{
Address: address,
}, res)
return res, err
}
// CreateAddress creates a new address for [user]
func (c *Client) CreateAddress(user api.UserPass) (string, error) {
res := &api.JSONAddress{}
err := c.requester.SendRequest("createAddress", &user, res)
return res.Address, err
}
// ListAddresses returns an array of platform addresses controlled by [user]
func (c *Client) ListAddresses(user api.UserPass) ([]string, error) {
res := &api.JSONAddresses{}
err := c.requester.SendRequest("listAddresses", &user, res)
return res.Addresses, err
}
// GetUTXOs returns the byte representation of the UTXOs controlled by [addrs]
func (c *Client) GetUTXOs(addrs []string, limit uint32, startAddress, startUTXOID string) ([][]byte, api.Index, error) {
return c.GetAtomicUTXOs(addrs, "", limit, startAddress, startUTXOID)
}
// GetAtomicUTXOs returns the byte representation of the atomic UTXOs controlled by [addresses]
// from [sourceChain]
func (c *Client) GetAtomicUTXOs(addrs []string, sourceChain string, limit uint32, startAddress, startUTXOID string) ([][]byte, api.Index, error) {
res := &api.GetUTXOsReply{}
err := c.requester.SendRequest("getUTXOs", &api.GetUTXOsArgs{
Addresses: addrs,
SourceChain: sourceChain,
Limit: cjson.Uint32(limit),
StartIndex: api.Index{
Address: startAddress,
UTXO: startUTXOID,
},
Encoding: formatting.Hex,
}, res)
if err != nil {
return nil, api.Index{}, err
}
utxos := make([][]byte, len(res.UTXOs))
for i, utxo := range res.UTXOs {
utxoBytes, err := formatting.Decode(res.Encoding, utxo)
if err != nil {
return nil, api.Index{}, err
}
utxos[i] = utxoBytes
}
return utxos, res.EndIndex, nil
}
// GetSubnets returns information about the specified subnets
func (c *Client) GetSubnets(ids []ids.ID) ([]APISubnet, error) {
res := &GetSubnetsResponse{}
err := c.requester.SendRequest("getSubnets", &GetSubnetsArgs{
IDs: ids,
}, res)
return res.Subnets, err
}
// GetStakingAssetID returns the assetID of the asset used for staking on
// subnet corresponding to [subnetID]
func (c *Client) GetStakingAssetID(subnetID ids.ID) (ids.ID, error) {
res := &GetStakingAssetIDResponse{}
err := c.requester.SendRequest("getStakingAssetID", &GetStakingAssetIDArgs{
SubnetID: subnetID,
}, res)
return res.AssetID, err
}
// GetCurrentValidators returns the list of current validators for subnet with ID [subnetID]
func (c *Client) GetCurrentValidators(subnetID ids.ID) ([]interface{}, error) {
res := &GetCurrentValidatorsReply{}
err := c.requester.SendRequest("getCurrentValidators", &GetCurrentValidatorsArgs{
SubnetID: subnetID,
}, res)
return res.Validators, err
}
// GetPendingValidators returns the list of pending validators for subnet with ID [subnetID]
func (c *Client) GetPendingValidators(subnetID ids.ID) ([]interface{}, []interface{}, error) {
res := &GetPendingValidatorsReply{}
err := c.requester.SendRequest("getPendingValidators", &GetPendingValidatorsArgs{
SubnetID: subnetID,
}, res)
return res.Validators, res.Delegators, err
}
// GetCurrentSupply returns an upper bound on the supply of AVAX in the system
func (c *Client) GetCurrentSupply() (uint64, error) {
res := &GetCurrentSupplyReply{}
err := c.requester.SendRequest("getCurrentSupply", struct{}{}, res)
return uint64(res.Supply), err
}
// SampleValidators returns the nodeIDs of a sample of [sampleSize] validators from the current validator set for subnet with ID [subnetID]
func (c *Client) SampleValidators(subnetID ids.ID, sampleSize uint16) ([]string, error) {
res := &SampleValidatorsReply{}
err := c.requester.SendRequest("sampleValidators", &SampleValidatorsArgs{
SubnetID: subnetID,
Size: cjson.Uint16(sampleSize),
}, res)
return res.Validators, err
}
// AddValidator issues a transaction to add a validator to the primary network and returns the txID
func (c *Client) AddValidator(
user api.UserPass,
from []string,
changeAddr string,
rewardAddress,
nodeID string,
stakeAmount,
startTime,
endTime uint64,
delegationFeeRate float32,
) (ids.ID, error) {
res := &api.JSONTxID{}
jsonStakeAmount := cjson.Uint64(stakeAmount)
err := c.requester.SendRequest("addValidator", &AddValidatorArgs{
JSONSpendHeader: api.JSONSpendHeader{
UserPass: user,
},
APIStaker: APIStaker{
NodeID: nodeID,
StakeAmount: &jsonStakeAmount,
StartTime: cjson.Uint64(startTime),
EndTime: cjson.Uint64(endTime),
},
RewardAddress: rewardAddress,
DelegationFeeRate: cjson.Float32(delegationFeeRate),
}, res)
return res.TxID, err
}
// AddDelegator issues a transaction to add a delegator to the primary network and returns the txID
func (c *Client) AddDelegator(
user api.UserPass,
from []string,
changeAddr string,
rewardAddress,
nodeID string,
stakeAmount,
startTime,
endTime uint64,
) (ids.ID, error) {
res := &api.JSONTxID{}
jsonStakeAmount := cjson.Uint64(stakeAmount)
err := c.requester.SendRequest("addDelegator", &AddDelegatorArgs{
JSONSpendHeader: api.JSONSpendHeader{
UserPass: user,
JSONFromAddrs: api.JSONFromAddrs{From: from},
JSONChangeAddr: api.JSONChangeAddr{ChangeAddr: changeAddr},
}, APIStaker: APIStaker{
NodeID: nodeID,
StakeAmount: &jsonStakeAmount,
StartTime: cjson.Uint64(startTime),
EndTime: cjson.Uint64(endTime),
},
RewardAddress: rewardAddress,
}, res)
return res.TxID, err
}
// AddSubnetValidator issues a transaction to add validator [nodeID] to subnet with ID [subnetID] and returns the txID
func (c *Client) AddSubnetValidator(
user api.UserPass,
from []string,
changeAddr string,
subnetID,
nodeID string,
stakeAmount,
startTime,
endTime uint64,
) (ids.ID, error) {
res := &api.JSONTxID{}
jsonStakeAmount := cjson.Uint64(stakeAmount)
err := c.requester.SendRequest("addSubnetValidator", &AddSubnetValidatorArgs{
JSONSpendHeader: api.JSONSpendHeader{
UserPass: user,
JSONFromAddrs: api.JSONFromAddrs{From: from},
JSONChangeAddr: api.JSONChangeAddr{ChangeAddr: changeAddr},
},
APIStaker: APIStaker{
NodeID: nodeID,
StakeAmount: &jsonStakeAmount,
StartTime: cjson.Uint64(startTime),
EndTime: cjson.Uint64(endTime),
},
SubnetID: subnetID,
}, res)
return res.TxID, err
}
// CreateSubnet issues a transaction to create [subnet] and returns the txID
func (c *Client) CreateSubnet(
user api.UserPass,
from []string,
changeAddr string,
controlKeys []string,
threshold uint32,
) (ids.ID, error) {
res := &api.JSONTxID{}
err := c.requester.SendRequest("createSubnet", &CreateSubnetArgs{
JSONSpendHeader: api.JSONSpendHeader{
UserPass: user,
JSONFromAddrs: api.JSONFromAddrs{From: from},
JSONChangeAddr: api.JSONChangeAddr{ChangeAddr: changeAddr},
},
APISubnet: APISubnet{
ControlKeys: controlKeys,
Threshold: cjson.Uint32(threshold),
},
}, res)
return res.TxID, err
}
// ExportAVAX issues an ExportAVAX transaction and returns the txID
func (c *Client) ExportAVAX(
user api.UserPass,
from []string,
changeAddr string,
to string,
amount uint64,
) (ids.ID, error) {
res := &api.JSONTxID{}
err := c.requester.SendRequest("exportAVAX", &ExportAVAXArgs{
JSONSpendHeader: api.JSONSpendHeader{
UserPass: user,
JSONFromAddrs: api.JSONFromAddrs{From: from},
JSONChangeAddr: api.JSONChangeAddr{ChangeAddr: changeAddr},
},
To: to,
Amount: cjson.Uint64(amount),
}, res)
return res.TxID, err
}
// ImportAVAX issues an ImportAVAX transaction and returns the txID
func (c *Client) ImportAVAX(
user api.UserPass,
from []string,
changeAddr,
to,
sourceChain string,
) (ids.ID, error) {
res := &api.JSONTxID{}
err := c.requester.SendRequest("importAVAX", &ImportAVAXArgs{
JSONSpendHeader: api.JSONSpendHeader{
UserPass: user,
JSONFromAddrs: api.JSONFromAddrs{From: from},
JSONChangeAddr: api.JSONChangeAddr{ChangeAddr: changeAddr},
},
To: to,
SourceChain: sourceChain,
}, res)
return res.TxID, err
}
// CreateBlockchain issues a CreateBlockchain transaction and returns the txID
func (c *Client) CreateBlockchain(
user api.UserPass,
from []string,
changeAddr string,
subnetID ids.ID,
vmID string,
fxIDs []string,
name string,
genesisData []byte,
) (ids.ID, error) {
genesisDataStr, err := formatting.Encode(formatting.Hex, genesisData)
if err != nil {
return ids.ID{}, err
}
res := &api.JSONTxID{}
err = c.requester.SendRequest("createBlockchain", &CreateBlockchainArgs{
JSONSpendHeader: api.JSONSpendHeader{
UserPass: user,
JSONFromAddrs: api.JSONFromAddrs{From: from},
JSONChangeAddr: api.JSONChangeAddr{ChangeAddr: changeAddr},
},
SubnetID: subnetID,
VMID: vmID,
FxIDs: fxIDs,
Name: name,
GenesisData: genesisDataStr,
Encoding: formatting.Hex,
}, res)
return res.TxID, err
}
// GetBlockchainStatus returns the current status of blockchain with ID: [blockchainID]
func (c *Client) GetBlockchainStatus(blockchainID string) (Status, error) {
res := &GetBlockchainStatusReply{}
err := c.requester.SendRequest("getBlockchainStatus", &GetBlockchainStatusArgs{
BlockchainID: blockchainID,
}, res)
return res.Status, err
}
// ValidatedBy returns the ID of the Subnet that validates [blockchainID]
func (c *Client) ValidatedBy(blockchainID ids.ID) (ids.ID, error) {
res := &ValidatedByResponse{}
err := c.requester.SendRequest("validatedBy", &ValidatedByArgs{
BlockchainID: blockchainID,
}, res)
return res.SubnetID, err
}
// Validates returns the list of blockchains that are validated by the subnet with ID [subnetID]
func (c *Client) Validates(subnetID ids.ID) ([]ids.ID, error) {
res := &ValidatesResponse{}
err := c.requester.SendRequest("validates", &ValidatesArgs{
SubnetID: subnetID,
}, res)
return res.BlockchainIDs, err
}
// GetBlockchains returns the list of blockchains on the platform
func (c *Client) GetBlockchains() ([]APIBlockchain, error) {
res := &GetBlockchainsResponse{}
err := c.requester.SendRequest("getBlockchains", struct{}{}, res)
return res.Blockchains, err
}
// IssueTx issues the transaction and returns its transaction ID
func (c *Client) IssueTx(txBytes []byte) (ids.ID, error) {
txStr, err := formatting.Encode(formatting.Hex, txBytes)
if err != nil {
return ids.ID{}, err
}
res := &api.JSONTxID{}
err = c.requester.SendRequest("issueTx", &api.FormattedTx{
Tx: txStr,
Encoding: formatting.Hex,
}, res)
return res.TxID, err
}
// GetTx returns the byte representation of the transaction corresponding to [txID]
func (c *Client) GetTx(txID ids.ID) ([]byte, error) {
res := &api.FormattedTx{}
err := c.requester.SendRequest("getTx", &api.GetTxArgs{
TxID: txID,
Encoding: formatting.Hex,
}, res)
if err != nil {
return nil, err
}
return formatting.Decode(res.Encoding, res.Tx)
}
// GetTxStatus returns the status of the transaction corresponding to [txID]
func (c *Client) GetTxStatus(txID ids.ID, includeReason bool) (*GetTxStatusResponse, error) {
res := new(GetTxStatusResponse)
err := c.requester.SendRequest("getTxStatus", &GetTxStatusArgs{
TxID: txID,
IncludeReason: includeReason,
}, res)
return res, err
}
// GetStake returns the amount of nAVAX that [addresses] have cumulatively
// staked on the Primary Network.
func (c *Client) GetStake(addrs []string) (uint64, error) {
res := new(GetStakeReply)
err := c.requester.SendRequest("getStake", &api.JSONAddresses{
Addresses: addrs,
}, res)
return uint64(res.Staked), err
}
// GetMinStake returns the minimum staking amount in nAVAX for validators
// and delegators respectively
func (c *Client) GetMinStake() (uint64, uint64, error) {
res := new(GetMinStakeReply)
err := c.requester.SendRequest("getMinStake", struct{}{}, res)
return uint64(res.MinValidatorStake), uint64(res.MinDelegatorStake), err
}
// GetTotalStake returns the total amount (in nAVAX) staked on the network
func (c *Client) GetTotalStake() (uint64, error) {
res := new(GetTotalStakeReply)
err := c.requester.SendRequest("getTotalStake", struct{}{}, res)
return uint64(res.Stake), err
}
// GetMaxStakeAmount returns the maximum amount of nAVAX staking to the named
// node during the time period.
func (c *Client) GetMaxStakeAmount(subnetID ids.ID, nodeID string, startTime, endTime uint64) (uint64, error) {
res := new(GetMaxStakeAmountReply)
err := c.requester.SendRequest("getMaxStakeAmount", &GetMaxStakeAmountArgs{
SubnetID: subnetID,
NodeID: nodeID,
StartTime: cjson.Uint64(startTime),
EndTime: cjson.Uint64(endTime),
}, res)
return uint64(res.Amount), err
}
// GetRewardUTXOs returns the reward UTXOs for a transaction
func (c *Client) GetRewardUTXOs(args *api.GetTxArgs) ([][]byte, error) {
res := &GetRewardUTXOsReply{}
err := c.requester.SendRequest("getRewardUTXOs", args, res)
if err != nil {
return nil, err
}
utxos := make([][]byte, len(res.UTXOs))
for i, utxoStr := range res.UTXOs {
utxoBytes, err := formatting.Decode(res.Encoding, utxoStr)
if err != nil {
return nil, err
}
utxos[i] = utxoBytes
}
return utxos, err
}