-
Notifications
You must be signed in to change notification settings - Fork 664
/
client.go
799 lines (746 loc) · 26 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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package platformvm
import (
"context"
"time"
"github.com/ava-labs/avalanchego/api"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/crypto"
"github.com/ava-labs/avalanchego/utils/formatting"
"github.com/ava-labs/avalanchego/utils/formatting/address"
"github.com/ava-labs/avalanchego/utils/json"
"github.com/ava-labs/avalanchego/utils/rpc"
"github.com/ava-labs/avalanchego/vms/platformvm/status"
)
var _ Client = &client{}
// Client interface for interacting with the P Chain endpoint
type Client interface {
// GetHeight returns the current block height of the P Chain
GetHeight(ctx context.Context, options ...rpc.Option) (uint64, error)
// ExportKey returns the private key corresponding to [address] from [user]'s account
ExportKey(ctx context.Context, user api.UserPass, address ids.ShortID, options ...rpc.Option) (*crypto.PrivateKeySECP256K1R, error)
// ImportKey imports the specified [privateKey] to [user]'s keystore
ImportKey(ctx context.Context, user api.UserPass, privateKey *crypto.PrivateKeySECP256K1R, options ...rpc.Option) (ids.ShortID, error)
// GetBalance returns the balance of [addrs] on the P Chain
GetBalance(ctx context.Context, addrs []ids.ShortID, options ...rpc.Option) (*GetBalanceResponse, error)
// CreateAddress creates a new address for [user]
CreateAddress(ctx context.Context, user api.UserPass, options ...rpc.Option) (ids.ShortID, error)
// ListAddresses returns an array of platform addresses controlled by [user]
ListAddresses(ctx context.Context, user api.UserPass, options ...rpc.Option) ([]ids.ShortID, error)
// GetUTXOs returns the byte representation of the UTXOs controlled by [addrs]
GetUTXOs(
ctx context.Context,
addrs []ids.ShortID,
limit uint32,
startAddress ids.ShortID,
startUTXOID ids.ID,
options ...rpc.Option,
) ([][]byte, ids.ShortID, ids.ID, error)
// GetAtomicUTXOs returns the byte representation of the atomic UTXOs controlled by [addrs]
// from [sourceChain]
GetAtomicUTXOs(
ctx context.Context,
addrs []ids.ShortID,
sourceChain string,
limit uint32,
startAddress ids.ShortID,
startUTXOID ids.ID,
options ...rpc.Option,
) ([][]byte, ids.ShortID, ids.ID, error)
// GetSubnets returns information about the specified subnets
GetSubnets(context.Context, []ids.ID, ...rpc.Option) ([]ClientSubnet, error)
// GetStakingAssetID returns the assetID of the asset used for staking on
// subnet corresponding to [subnetID]
GetStakingAssetID(context.Context, ids.ID, ...rpc.Option) (ids.ID, error)
// GetCurrentValidators returns the list of current validators for subnet with ID [subnetID]
GetCurrentValidators(ctx context.Context, subnetID ids.ID, nodeIDs []ids.NodeID, options ...rpc.Option) ([]ClientPrimaryValidator, error)
// GetPendingValidators returns the list of pending validators for subnet with ID [subnetID]
GetPendingValidators(ctx context.Context, subnetID ids.ID, nodeIDs []ids.NodeID, options ...rpc.Option) ([]interface{}, []interface{}, error)
// GetCurrentSupply returns an upper bound on the supply of AVAX in the system
GetCurrentSupply(ctx context.Context, options ...rpc.Option) (uint64, error)
// SampleValidators returns the nodeIDs of a sample of [sampleSize] validators from the current validator set for subnet with ID [subnetID]
SampleValidators(ctx context.Context, subnetID ids.ID, sampleSize uint16, options ...rpc.Option) ([]ids.NodeID, error)
// AddValidator issues a transaction to add a validator to the primary network
// and returns the txID
AddValidator(
ctx context.Context,
user api.UserPass,
from []ids.ShortID,
changeAddr ids.ShortID,
rewardAddress ids.ShortID,
nodeID ids.NodeID,
stakeAmount,
startTime,
endTime uint64,
delegationFeeRate float32,
options ...rpc.Option,
) (ids.ID, error)
// AddDelegator issues a transaction to add a delegator to the primary network
// and returns the txID
AddDelegator(
ctx context.Context,
user api.UserPass,
from []ids.ShortID,
changeAddr ids.ShortID,
rewardAddress ids.ShortID,
nodeID ids.NodeID,
stakeAmount,
startTime,
endTime uint64,
options ...rpc.Option,
) (ids.ID, error)
// AddSubnetValidator issues a transaction to add validator [nodeID] to subnet
// with ID [subnetID] and returns the txID
AddSubnetValidator(
ctx context.Context,
user api.UserPass,
from []ids.ShortID,
changeAddr ids.ShortID,
subnetID ids.ID,
nodeID ids.NodeID,
stakeAmount,
startTime,
endTime uint64,
options ...rpc.Option,
) (ids.ID, error)
// CreateSubnet issues a transaction to create [subnet] and returns the txID
CreateSubnet(
ctx context.Context,
user api.UserPass,
from []ids.ShortID,
changeAddr ids.ShortID,
controlKeys []ids.ShortID,
threshold uint32,
options ...rpc.Option,
) (ids.ID, error)
// ExportAVAX issues an ExportTx transaction and returns the txID
ExportAVAX(
ctx context.Context,
user api.UserPass,
from []ids.ShortID,
changeAddr ids.ShortID,
to ids.ShortID,
toChainIDAlias string,
amount uint64,
options ...rpc.Option,
) (ids.ID, error)
// ImportAVAX issues an ImportTx transaction and returns the txID
ImportAVAX(
ctx context.Context,
user api.UserPass,
from []ids.ShortID,
changeAddr ids.ShortID,
to ids.ShortID,
sourceChain string,
options ...rpc.Option,
) (ids.ID, error)
// CreateBlockchain issues a CreateBlockchain transaction and returns the txID
CreateBlockchain(
ctx context.Context,
user api.UserPass,
from []ids.ShortID,
changeAddr ids.ShortID,
subnetID ids.ID,
vmID string,
fxIDs []string,
name string,
genesisData []byte,
options ...rpc.Option,
) (ids.ID, error)
// GetBlockchainStatus returns the current status of blockchain with ID: [blockchainID]
GetBlockchainStatus(ctx context.Context, blockchainID string, options ...rpc.Option) (status.BlockchainStatus, error)
// ValidatedBy returns the ID of the Subnet that validates [blockchainID]
ValidatedBy(ctx context.Context, blockchainID ids.ID, options ...rpc.Option) (ids.ID, error)
// Validates returns the list of blockchains that are validated by the subnet with ID [subnetID]
Validates(ctx context.Context, subnetID ids.ID, options ...rpc.Option) ([]ids.ID, error)
// GetBlockchains returns the list of blockchains on the platform
GetBlockchains(ctx context.Context, options ...rpc.Option) ([]APIBlockchain, error)
// IssueTx issues the transaction and returns its txID
IssueTx(ctx context.Context, tx []byte, options ...rpc.Option) (ids.ID, error)
// GetTx returns the byte representation of the transaction corresponding to [txID]
GetTx(ctx context.Context, txID ids.ID, options ...rpc.Option) ([]byte, error)
// GetTxStatus returns the status of the transaction corresponding to [txID]
GetTxStatus(ctx context.Context, txID ids.ID, options ...rpc.Option) (*GetTxStatusResponse, error)
// AwaitTxDecided polls [GetTxStatus] until a status is returned that
// implies the tx may be decided.
AwaitTxDecided(
ctx context.Context,
txID ids.ID,
freq time.Duration,
options ...rpc.Option,
) (*GetTxStatusResponse, error)
// GetStake returns the amount of nAVAX that [addrs] have cumulatively
// staked on the Primary Network.
GetStake(ctx context.Context, addrs []ids.ShortID, options ...rpc.Option) (uint64, [][]byte, error)
// GetMinStake returns the minimum staking amount in nAVAX for validators
// and delegators respectively
GetMinStake(ctx context.Context, options ...rpc.Option) (uint64, uint64, error)
// GetTotalStake returns the total amount (in nAVAX) staked on the network
GetTotalStake(ctx context.Context, subnetID ids.ID, options ...rpc.Option) (uint64, error)
// GetMaxStakeAmount returns the maximum amount of nAVAX staking to the named
// node during the time period.
GetMaxStakeAmount(
ctx context.Context,
subnetID ids.ID,
nodeID ids.NodeID,
startTime uint64,
endTime uint64,
options ...rpc.Option,
) (uint64, error)
// GetRewardUTXOs returns the reward UTXOs for a transaction
GetRewardUTXOs(context.Context, *api.GetTxArgs, ...rpc.Option) ([][]byte, error)
// GetTimestamp returns the current chain timestamp
GetTimestamp(ctx context.Context, options ...rpc.Option) (time.Time, error)
// GetValidatorsAt returns the weights of the validator set of a provided subnet
// at the specified height.
GetValidatorsAt(ctx context.Context, subnetID ids.ID, height uint64, options ...rpc.Option) (map[ids.NodeID]uint64, error)
// GetBlock returns the block with the given id.
GetBlock(ctx context.Context, blockID ids.ID, options ...rpc.Option) ([]byte, error)
}
// Client implementation for interacting with the P Chain endpoint
type client struct {
requester rpc.EndpointRequester
}
// NewClient returns a Client for interacting with the P Chain endpoint
func NewClient(uri string) Client {
return &client{requester: rpc.NewEndpointRequester(
uri+"/ext/P",
"platform",
)}
}
func (c *client) GetHeight(ctx context.Context, options ...rpc.Option) (uint64, error) {
res := &GetHeightResponse{}
err := c.requester.SendRequest(ctx, "getHeight", struct{}{}, res, options...)
return uint64(res.Height), err
}
func (c *client) ExportKey(ctx context.Context, user api.UserPass, address ids.ShortID, options ...rpc.Option) (*crypto.PrivateKeySECP256K1R, error) {
res := &ExportKeyReply{}
err := c.requester.SendRequest(ctx, "exportKey", &ExportKeyArgs{
UserPass: user,
Address: address.String(),
}, res, options...)
return res.PrivateKey, err
}
func (c *client) ImportKey(ctx context.Context, user api.UserPass, privateKey *crypto.PrivateKeySECP256K1R, options ...rpc.Option) (ids.ShortID, error) {
res := &api.JSONAddress{}
err := c.requester.SendRequest(ctx, "importKey", &ImportKeyArgs{
UserPass: user,
PrivateKey: privateKey,
}, res, options...)
if err != nil {
return ids.ShortID{}, err
}
return address.ParseToID(res.Address)
}
func (c *client) GetBalance(ctx context.Context, addrs []ids.ShortID, options ...rpc.Option) (*GetBalanceResponse, error) {
res := &GetBalanceResponse{}
err := c.requester.SendRequest(ctx, "getBalance", &GetBalanceRequest{
Addresses: ids.ShortIDsToStrings(addrs),
}, res, options...)
return res, err
}
func (c *client) CreateAddress(ctx context.Context, user api.UserPass, options ...rpc.Option) (ids.ShortID, error) {
res := &api.JSONAddress{}
err := c.requester.SendRequest(ctx, "createAddress", &user, res, options...)
if err != nil {
return ids.ShortID{}, err
}
return address.ParseToID(res.Address)
}
func (c *client) ListAddresses(ctx context.Context, user api.UserPass, options ...rpc.Option) ([]ids.ShortID, error) {
res := &api.JSONAddresses{}
err := c.requester.SendRequest(ctx, "listAddresses", &user, res, options...)
if err != nil {
return nil, err
}
return address.ParseToIDs(res.Addresses)
}
func (c *client) GetUTXOs(
ctx context.Context,
addrs []ids.ShortID,
limit uint32,
startAddress ids.ShortID,
startUTXOID ids.ID,
options ...rpc.Option,
) ([][]byte, ids.ShortID, ids.ID, error) {
return c.GetAtomicUTXOs(ctx, addrs, "", limit, startAddress, startUTXOID, options...)
}
func (c *client) GetAtomicUTXOs(
ctx context.Context,
addrs []ids.ShortID,
sourceChain string,
limit uint32,
startAddress ids.ShortID,
startUTXOID ids.ID,
options ...rpc.Option,
) ([][]byte, ids.ShortID, ids.ID, error) {
res := &api.GetUTXOsReply{}
err := c.requester.SendRequest(ctx, "getUTXOs", &api.GetUTXOsArgs{
Addresses: ids.ShortIDsToStrings(addrs),
SourceChain: sourceChain,
Limit: json.Uint32(limit),
StartIndex: api.Index{
Address: startAddress.String(),
UTXO: startUTXOID.String(),
},
Encoding: formatting.Hex,
}, res, options...)
if err != nil {
return nil, ids.ShortID{}, ids.Empty, 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, ids.ShortID{}, ids.Empty, err
}
utxos[i] = utxoBytes
}
endAddr, err := address.ParseToID(res.EndIndex.Address)
if err != nil {
return nil, ids.ShortID{}, ids.Empty, err
}
endUTXOID, err := ids.FromString(res.EndIndex.UTXO)
return utxos, endAddr, endUTXOID, err
}
// ClientSubnet is a representation of a subnet used in client methods
type ClientSubnet struct {
// ID of the subnet
ID ids.ID
// Each element of [ControlKeys] the address of a public key.
// A transaction to add a validator to this subnet requires
// signatures from [Threshold] of these keys to be valid.
ControlKeys []ids.ShortID
Threshold uint32
}
func (c *client) GetSubnets(ctx context.Context, ids []ids.ID, options ...rpc.Option) ([]ClientSubnet, error) {
res := &GetSubnetsResponse{}
err := c.requester.SendRequest(ctx, "getSubnets", &GetSubnetsArgs{
IDs: ids,
}, res, options...)
if err != nil {
return nil, err
}
subnets := make([]ClientSubnet, len(res.Subnets))
for i, apiSubnet := range res.Subnets {
controlKeys, err := address.ParseToIDs(apiSubnet.ControlKeys)
if err != nil {
return nil, err
}
subnets[i] = ClientSubnet{
ID: apiSubnet.ID,
ControlKeys: controlKeys,
Threshold: uint32(apiSubnet.Threshold),
}
}
return subnets, nil
}
func (c *client) GetStakingAssetID(ctx context.Context, subnetID ids.ID, options ...rpc.Option) (ids.ID, error) {
res := &GetStakingAssetIDResponse{}
err := c.requester.SendRequest(ctx, "getStakingAssetID", &GetStakingAssetIDArgs{
SubnetID: subnetID,
}, res, options...)
return res.AssetID, err
}
func (c *client) GetCurrentValidators(
ctx context.Context,
subnetID ids.ID,
nodeIDs []ids.NodeID,
options ...rpc.Option,
) ([]ClientPrimaryValidator, error) {
res := &GetCurrentValidatorsReply{}
err := c.requester.SendRequest(ctx, "getCurrentValidators", &GetCurrentValidatorsArgs{
SubnetID: subnetID,
NodeIDs: nodeIDs,
}, res, options...)
if err != nil {
return nil, err
}
return getClientPrimaryValidators(res.Validators)
}
func (c *client) GetPendingValidators(
ctx context.Context,
subnetID ids.ID,
nodeIDs []ids.NodeID,
options ...rpc.Option,
) ([]interface{}, []interface{}, error) {
res := &GetPendingValidatorsReply{}
err := c.requester.SendRequest(ctx, "getPendingValidators", &GetPendingValidatorsArgs{
SubnetID: subnetID,
NodeIDs: nodeIDs,
}, res, options...)
return res.Validators, res.Delegators, err
}
func (c *client) GetCurrentSupply(ctx context.Context, options ...rpc.Option) (uint64, error) {
res := &GetCurrentSupplyReply{}
err := c.requester.SendRequest(ctx, "getCurrentSupply", struct{}{}, res, options...)
return uint64(res.Supply), err
}
func (c *client) SampleValidators(ctx context.Context, subnetID ids.ID, sampleSize uint16, options ...rpc.Option) ([]ids.NodeID, error) {
res := &SampleValidatorsReply{}
err := c.requester.SendRequest(ctx, "sampleValidators", &SampleValidatorsArgs{
SubnetID: subnetID,
Size: json.Uint16(sampleSize),
}, res, options...)
return res.Validators, err
}
func (c *client) AddValidator(
ctx context.Context,
user api.UserPass,
from []ids.ShortID,
changeAddr ids.ShortID,
rewardAddress ids.ShortID,
nodeID ids.NodeID,
stakeAmount,
startTime,
endTime uint64,
delegationFeeRate float32,
options ...rpc.Option,
) (ids.ID, error) {
res := &api.JSONTxID{}
jsonStakeAmount := json.Uint64(stakeAmount)
err := c.requester.SendRequest(ctx, "addValidator", &AddValidatorArgs{
JSONSpendHeader: api.JSONSpendHeader{
UserPass: user,
JSONFromAddrs: api.JSONFromAddrs{From: ids.ShortIDsToStrings(from)},
JSONChangeAddr: api.JSONChangeAddr{ChangeAddr: changeAddr.String()},
},
APIStaker: APIStaker{
NodeID: nodeID,
StakeAmount: &jsonStakeAmount,
StartTime: json.Uint64(startTime),
EndTime: json.Uint64(endTime),
},
RewardAddress: rewardAddress.String(),
DelegationFeeRate: json.Float32(delegationFeeRate),
}, res, options...)
return res.TxID, err
}
func (c *client) AddDelegator(
ctx context.Context,
user api.UserPass,
from []ids.ShortID,
changeAddr ids.ShortID,
rewardAddress ids.ShortID,
nodeID ids.NodeID,
stakeAmount,
startTime,
endTime uint64,
options ...rpc.Option,
) (ids.ID, error) {
res := &api.JSONTxID{}
jsonStakeAmount := json.Uint64(stakeAmount)
err := c.requester.SendRequest(ctx, "addDelegator", &AddDelegatorArgs{
JSONSpendHeader: api.JSONSpendHeader{
UserPass: user,
JSONFromAddrs: api.JSONFromAddrs{From: ids.ShortIDsToStrings(from)},
JSONChangeAddr: api.JSONChangeAddr{ChangeAddr: changeAddr.String()},
}, APIStaker: APIStaker{
NodeID: nodeID,
StakeAmount: &jsonStakeAmount,
StartTime: json.Uint64(startTime),
EndTime: json.Uint64(endTime),
},
RewardAddress: rewardAddress.String(),
}, res, options...)
return res.TxID, err
}
func (c *client) AddSubnetValidator(
ctx context.Context,
user api.UserPass,
from []ids.ShortID,
changeAddr ids.ShortID,
subnetID ids.ID,
nodeID ids.NodeID,
stakeAmount,
startTime,
endTime uint64,
options ...rpc.Option,
) (ids.ID, error) {
res := &api.JSONTxID{}
jsonStakeAmount := json.Uint64(stakeAmount)
err := c.requester.SendRequest(ctx, "addSubnetValidator", &AddSubnetValidatorArgs{
JSONSpendHeader: api.JSONSpendHeader{
UserPass: user,
JSONFromAddrs: api.JSONFromAddrs{From: ids.ShortIDsToStrings(from)},
JSONChangeAddr: api.JSONChangeAddr{ChangeAddr: changeAddr.String()},
},
APIStaker: APIStaker{
NodeID: nodeID,
StakeAmount: &jsonStakeAmount,
StartTime: json.Uint64(startTime),
EndTime: json.Uint64(endTime),
},
SubnetID: subnetID.String(),
}, res, options...)
return res.TxID, err
}
func (c *client) CreateSubnet(
ctx context.Context,
user api.UserPass,
from []ids.ShortID,
changeAddr ids.ShortID,
controlKeys []ids.ShortID,
threshold uint32,
options ...rpc.Option,
) (ids.ID, error) {
res := &api.JSONTxID{}
err := c.requester.SendRequest(ctx, "createSubnet", &CreateSubnetArgs{
JSONSpendHeader: api.JSONSpendHeader{
UserPass: user,
JSONFromAddrs: api.JSONFromAddrs{From: ids.ShortIDsToStrings(from)},
JSONChangeAddr: api.JSONChangeAddr{ChangeAddr: changeAddr.String()},
},
APISubnet: APISubnet{
ControlKeys: ids.ShortIDsToStrings(controlKeys),
Threshold: json.Uint32(threshold),
},
}, res, options...)
return res.TxID, err
}
func (c *client) ExportAVAX(
ctx context.Context,
user api.UserPass,
from []ids.ShortID,
changeAddr ids.ShortID,
to ids.ShortID,
targetChain string,
amount uint64,
options ...rpc.Option,
) (ids.ID, error) {
res := &api.JSONTxID{}
err := c.requester.SendRequest(ctx, "exportAVAX", &ExportAVAXArgs{
JSONSpendHeader: api.JSONSpendHeader{
UserPass: user,
JSONFromAddrs: api.JSONFromAddrs{From: ids.ShortIDsToStrings(from)},
JSONChangeAddr: api.JSONChangeAddr{ChangeAddr: changeAddr.String()},
},
TargetChain: targetChain,
To: to.String(),
Amount: json.Uint64(amount),
}, res, options...)
return res.TxID, err
}
func (c *client) ImportAVAX(
ctx context.Context,
user api.UserPass,
from []ids.ShortID,
changeAddr ids.ShortID,
to ids.ShortID,
sourceChain string,
options ...rpc.Option,
) (ids.ID, error) {
res := &api.JSONTxID{}
err := c.requester.SendRequest(ctx, "importAVAX", &ImportAVAXArgs{
JSONSpendHeader: api.JSONSpendHeader{
UserPass: user,
JSONFromAddrs: api.JSONFromAddrs{From: ids.ShortIDsToStrings(from)},
JSONChangeAddr: api.JSONChangeAddr{ChangeAddr: changeAddr.String()},
},
To: to.String(),
SourceChain: sourceChain,
}, res, options...)
return res.TxID, err
}
func (c *client) CreateBlockchain(
ctx context.Context,
user api.UserPass,
from []ids.ShortID,
changeAddr ids.ShortID,
subnetID ids.ID,
vmID string,
fxIDs []string,
name string,
genesisData []byte,
options ...rpc.Option,
) (ids.ID, error) {
genesisDataStr, err := formatting.EncodeWithChecksum(formatting.Hex, genesisData)
if err != nil {
return ids.ID{}, err
}
res := &api.JSONTxID{}
err = c.requester.SendRequest(ctx, "createBlockchain", &CreateBlockchainArgs{
JSONSpendHeader: api.JSONSpendHeader{
UserPass: user,
JSONFromAddrs: api.JSONFromAddrs{From: ids.ShortIDsToStrings(from)},
JSONChangeAddr: api.JSONChangeAddr{ChangeAddr: changeAddr.String()},
},
SubnetID: subnetID,
VMID: vmID,
FxIDs: fxIDs,
Name: name,
GenesisData: genesisDataStr,
Encoding: formatting.Hex,
}, res, options...)
return res.TxID, err
}
func (c *client) GetBlockchainStatus(ctx context.Context, blockchainID string, options ...rpc.Option) (status.BlockchainStatus, error) {
res := &GetBlockchainStatusReply{}
err := c.requester.SendRequest(ctx, "getBlockchainStatus", &GetBlockchainStatusArgs{
BlockchainID: blockchainID,
}, res, options...)
return res.Status, err
}
func (c *client) ValidatedBy(ctx context.Context, blockchainID ids.ID, options ...rpc.Option) (ids.ID, error) {
res := &ValidatedByResponse{}
err := c.requester.SendRequest(ctx, "validatedBy", &ValidatedByArgs{
BlockchainID: blockchainID,
}, res, options...)
return res.SubnetID, err
}
func (c *client) Validates(ctx context.Context, subnetID ids.ID, options ...rpc.Option) ([]ids.ID, error) {
res := &ValidatesResponse{}
err := c.requester.SendRequest(ctx, "validates", &ValidatesArgs{
SubnetID: subnetID,
}, res, options...)
return res.BlockchainIDs, err
}
func (c *client) GetBlockchains(ctx context.Context, options ...rpc.Option) ([]APIBlockchain, error) {
res := &GetBlockchainsResponse{}
err := c.requester.SendRequest(ctx, "getBlockchains", struct{}{}, res, options...)
return res.Blockchains, err
}
func (c *client) IssueTx(ctx context.Context, txBytes []byte, options ...rpc.Option) (ids.ID, error) {
txStr, err := formatting.EncodeWithChecksum(formatting.Hex, txBytes)
if err != nil {
return ids.ID{}, err
}
res := &api.JSONTxID{}
err = c.requester.SendRequest(ctx, "issueTx", &api.FormattedTx{
Tx: txStr,
Encoding: formatting.Hex,
}, res, options...)
return res.TxID, err
}
func (c *client) GetTx(ctx context.Context, txID ids.ID, options ...rpc.Option) ([]byte, error) {
res := &api.FormattedTx{}
err := c.requester.SendRequest(ctx, "getTx", &api.GetTxArgs{
TxID: txID,
Encoding: formatting.Hex,
}, res, options...)
if err != nil {
return nil, err
}
return formatting.Decode(res.Encoding, res.Tx)
}
func (c *client) GetTxStatus(ctx context.Context, txID ids.ID, options ...rpc.Option) (*GetTxStatusResponse, error) {
res := new(GetTxStatusResponse)
err := c.requester.SendRequest(
ctx,
"getTxStatus",
&GetTxStatusArgs{
TxID: txID,
},
res,
options...,
)
return res, err
}
func (c *client) AwaitTxDecided(ctx context.Context, txID ids.ID, freq time.Duration, options ...rpc.Option) (*GetTxStatusResponse, error) {
ticker := time.NewTicker(freq)
defer ticker.Stop()
for {
res, err := c.GetTxStatus(ctx, txID, options...)
if err == nil {
switch res.Status {
case status.Committed, status.Aborted, status.Dropped:
return res, nil
}
}
select {
case <-ticker.C:
case <-ctx.Done():
return nil, ctx.Err()
}
}
}
func (c *client) GetStake(ctx context.Context, addrs []ids.ShortID, options ...rpc.Option) (uint64, [][]byte, error) {
res := new(GetStakeReply)
err := c.requester.SendRequest(ctx, "getStake", &GetStakeArgs{
JSONAddresses: api.JSONAddresses{
Addresses: ids.ShortIDsToStrings(addrs),
},
Encoding: formatting.Hex,
}, res, options...)
if err != nil {
return 0, nil, err
}
outputs := make([][]byte, len(res.Outputs))
for i, outputStr := range res.Outputs {
output, err := formatting.Decode(res.Encoding, outputStr)
if err != nil {
return 0, nil, err
}
outputs[i] = output
}
return uint64(res.Staked), outputs, err
}
func (c *client) GetMinStake(ctx context.Context, options ...rpc.Option) (uint64, uint64, error) {
res := new(GetMinStakeReply)
err := c.requester.SendRequest(ctx, "getMinStake", struct{}{}, res, options...)
return uint64(res.MinValidatorStake), uint64(res.MinDelegatorStake), err
}
func (c *client) GetTotalStake(ctx context.Context, subnetID ids.ID, options ...rpc.Option) (uint64, error) {
res := new(GetTotalStakeReply)
err := c.requester.SendRequest(ctx, "getTotalStake", &GetTotalStakeArgs{
SubnetID: subnetID,
}, res, options...)
var amount json.Uint64
if subnetID == constants.PrimaryNetworkID {
amount = res.Stake
} else {
amount = res.Weight
}
return uint64(amount), err
}
func (c *client) GetMaxStakeAmount(ctx context.Context, subnetID ids.ID, nodeID ids.NodeID, startTime, endTime uint64, options ...rpc.Option) (uint64, error) {
res := new(GetMaxStakeAmountReply)
err := c.requester.SendRequest(ctx, "getMaxStakeAmount", &GetMaxStakeAmountArgs{
SubnetID: subnetID,
NodeID: nodeID,
StartTime: json.Uint64(startTime),
EndTime: json.Uint64(endTime),
}, res, options...)
return uint64(res.Amount), err
}
func (c *client) GetRewardUTXOs(ctx context.Context, args *api.GetTxArgs, options ...rpc.Option) ([][]byte, error) {
res := &GetRewardUTXOsReply{}
err := c.requester.SendRequest(ctx, "getRewardUTXOs", args, res, options...)
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
}
func (c *client) GetTimestamp(ctx context.Context, options ...rpc.Option) (time.Time, error) {
res := &GetTimestampReply{}
err := c.requester.SendRequest(ctx, "getTimestamp", struct{}{}, res, options...)
return res.Timestamp, err
}
func (c *client) GetValidatorsAt(ctx context.Context, subnetID ids.ID, height uint64, options ...rpc.Option) (map[ids.NodeID]uint64, error) {
res := &GetValidatorsAtReply{}
err := c.requester.SendRequest(ctx, "getValidatorsAt", &GetValidatorsAtArgs{
SubnetID: subnetID,
Height: json.Uint64(height),
}, res, options...)
return res.Validators, err
}
func (c *client) GetBlock(ctx context.Context, blockID ids.ID, options ...rpc.Option) ([]byte, error) {
response := &api.FormattedBlock{}
if err := c.requester.SendRequest(ctx, "getBlock", &api.GetBlockArgs{
BlockID: blockID,
Encoding: formatting.Hex,
}, response, options...); err != nil {
return nil, err
}
return formatting.Decode(response.Encoding, response.Block)
}