-
Notifications
You must be signed in to change notification settings - Fork 642
/
builder.go
1120 lines (1026 loc) · 32.8 KB
/
builder.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
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package p
import (
"errors"
"fmt"
"time"
stdcontext "context"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/math"
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/avalanchego/vms/components/avax"
"github.com/ava-labs/avalanchego/vms/platformvm/signer"
"github.com/ava-labs/avalanchego/vms/platformvm/stakeable"
"github.com/ava-labs/avalanchego/vms/platformvm/txs"
"github.com/ava-labs/avalanchego/vms/platformvm/validator"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"
"github.com/ava-labs/avalanchego/wallet/subnet/primary/common"
)
var (
errNoChangeAddress = errors.New("no possible change address")
errWrongTxType = errors.New("wrong tx type")
errUnknownOwnerType = errors.New("unknown owner type")
errInsufficientAuthorization = errors.New("insufficient authorization")
errInsufficientFunds = errors.New("insufficient funds")
_ Builder = (*builder)(nil)
)
// Builder provides a convenient interface for building unsigned P-chain
// transactions.
type Builder interface {
// GetBalance calculates the amount of each asset that this builder has
// control over.
GetBalance(
options ...common.Option,
) (map[ids.ID]uint64, error)
// GetImportableBalance calculates the amount of each asset that this
// builder could import from the provided chain.
//
// - [chainID] specifies the chain the funds are from.
GetImportableBalance(
chainID ids.ID,
options ...common.Option,
) (map[ids.ID]uint64, error)
// NewBaseTx creates a new simple value transfer. Because the P-chain
// doesn't intend for balance transfers to occur, this method is expensive
// and abuses the creation of subnets.
//
// - [outputs] specifies all the recipients and amounts that should be sent
// from this transaction.
NewBaseTx(
outputs []*avax.TransferableOutput,
options ...common.Option,
) (*txs.CreateSubnetTx, error)
// NewAddValidatorTx creates a new validator of the primary network.
//
// - [vdr] specifies all the details of the validation period such as the
// startTime, endTime, stake weight, and nodeID.
// - [rewardsOwner] specifies the owner of all the rewards this validator
// may accrue during its validation period.
// - [shares] specifies the fraction (out of 1,000,000) that this validator
// will take from delegation rewards. If 1,000,000 is provided, 100% of
// the delegation reward will be sent to the validator's [rewardsOwner].
NewAddValidatorTx(
vdr *validator.Validator,
rewardsOwner *secp256k1fx.OutputOwners,
shares uint32,
options ...common.Option,
) (*txs.AddValidatorTx, error)
// NewAddSubnetValidatorTx creates a new validator of a subnet.
//
// - [vdr] specifies all the details of the validation period such as the
// startTime, endTime, sampling weight, nodeID, and subnetID.
NewAddSubnetValidatorTx(
vdr *validator.SubnetValidator,
options ...common.Option,
) (*txs.AddSubnetValidatorTx, error)
// NewRemoveSubnetValidatorTx removes [nodeID] from the validator
// set [subnetID].
NewRemoveSubnetValidatorTx(
nodeID ids.NodeID,
subnetID ids.ID,
options ...common.Option,
) (*txs.RemoveSubnetValidatorTx, error)
// NewAddDelegatorTx creates a new delegator to a validator on the primary
// network.
//
// - [vdr] specifies all the details of the delegation period such as the
// startTime, endTime, stake weight, and validator's nodeID.
// - [rewardsOwner] specifies the owner of all the rewards this delegator
// may accrue at the end of its delegation period.
NewAddDelegatorTx(
vdr *validator.Validator,
rewardsOwner *secp256k1fx.OutputOwners,
options ...common.Option,
) (*txs.AddDelegatorTx, error)
// NewCreateChainTx creates a new chain in the named subnet.
//
// - [subnetID] specifies the subnet to launch the chain in.
// - [genesis] specifies the initial state of the new chain.
// - [vmID] specifies the vm that the new chain will run.
// - [fxIDs] specifies all the feature extensions that the vm should be
// running with.
// - [chainName] specifies a human readable name for the chain.
NewCreateChainTx(
subnetID ids.ID,
genesis []byte,
vmID ids.ID,
fxIDs []ids.ID,
chainName string,
options ...common.Option,
) (*txs.CreateChainTx, error)
// NewCreateSubnetTx creates a new subnet with the specified owner.
//
// - [owner] specifies who has the ability to create new chains and add new
// validators to the subnet.
NewCreateSubnetTx(
owner *secp256k1fx.OutputOwners,
options ...common.Option,
) (*txs.CreateSubnetTx, error)
// NewImportTx creates an import transaction that attempts to consume all
// the available UTXOs and import the funds to [to].
//
// - [chainID] specifies the chain to be importing funds from.
// - [to] specifies where to send the imported funds to.
NewImportTx(
chainID ids.ID,
to *secp256k1fx.OutputOwners,
options ...common.Option,
) (*txs.ImportTx, error)
// NewExportTx creates an export transaction that attempts to send all the
// provided [outputs] to the requested [chainID].
//
// - [chainID] specifies the chain to be exporting the funds to.
// - [outputs] specifies the outputs to send to the [chainID].
NewExportTx(
chainID ids.ID,
outputs []*avax.TransferableOutput,
options ...common.Option,
) (*txs.ExportTx, error)
// NewTransformSubnetTx creates a transform subnet transaction that attempts
// to convert the provided [subnetID] from a permissioned subnet to a
// permissionless subnet. This transaction will convert
// [maxSupply] - [initialSupply] of [assetID] to staking rewards.
//
// - [subnetID] specifies the subnet to transform.
// - [assetID] specifies the asset to use to reward stakers on the subnet.
// - [initialSupply] is the amount of [assetID] that will be in circulation
// after this transaction is accepted.
// - [maxSupply] is the maximum total amount of [assetID] that should ever
// exist.
// - [minConsumptionRate] is the rate that a staker will receive rewards
// if they stake with a duration of 0.
// - [maxConsumptionRate] is the maximum rate that staking rewards should be
// consumed from the reward pool per year.
// - [minValidatorStake] is the minimum amount of funds required to become a
// validator.
// - [maxValidatorStake] is the maximum amount of funds a single validator
// can be allocated, including delegated funds.
// - [minStakeDuration] is the minimum number of seconds a staker can stake
// for.
// - [maxStakeDuration] is the maximum number of seconds a staker can stake
// for.
// - [minValidatorStake] is the minimum amount of funds required to become a
// delegator.
// - [maxValidatorWeightFactor] is the factor which calculates the maximum
// amount of delegation a validator can receive. A value of 1 effectively
// disables delegation.
// - [uptimeRequirement] is the minimum percentage a validator must be
// online and responsive to receive a reward.
NewTransformSubnetTx(
subnetID ids.ID,
assetID ids.ID,
initialSupply uint64,
maxSupply uint64,
minConsumptionRate uint64,
maxConsumptionRate uint64,
minValidatorStake uint64,
maxValidatorStake uint64,
minStakeDuration time.Duration,
maxStakeDuration time.Duration,
minDelegationFee uint32,
minDelegatorStake uint64,
maxValidatorWeightFactor byte,
uptimeRequirement uint32,
options ...common.Option,
) (*txs.TransformSubnetTx, error)
// NewAddPermissionlessValidatorTx creates a new validator of the specified
// subnet.
//
// - [vdr] specifies all the details of the validation period such as the
// subnetID, startTime, endTime, stake weight, and nodeID.
// - [signer] if the subnetID is the primary network, this is the BLS key
// for this validator. Otherwise, this value should be the empty signer.
// - [assetID] specifies the asset to stake.
// - [validationRewardsOwner] specifies the owner of all the rewards this
// validator earns for its validation period.
// - [delegationRewardsOwner] specifies the owner of all the rewards this
// validator earns for delegations during its validation period.
// - [shares] specifies the fraction (out of 1,000,000) that this validator
// will take from delegation rewards. If 1,000,000 is provided, 100% of
// the delegation reward will be sent to the validator's [rewardsOwner].
NewAddPermissionlessValidatorTx(
vdr *validator.SubnetValidator,
signer signer.Signer,
assetID ids.ID,
validationRewardsOwner *secp256k1fx.OutputOwners,
delegationRewardsOwner *secp256k1fx.OutputOwners,
shares uint32,
options ...common.Option,
) (*txs.AddPermissionlessValidatorTx, error)
// NewAddPermissionlessDelegatorTx creates a new delegator of the specified
// subnet on the specified nodeID.
//
// - [vdr] specifies all the details of the delegation period such as the
// subnetID, startTime, endTime, stake weight, and nodeID.
// - [assetID] specifies the asset to stake.
// - [rewardsOwner] specifies the owner of all the rewards this delegator
// earns during its delegation period.
NewAddPermissionlessDelegatorTx(
vdr *validator.SubnetValidator,
assetID ids.ID,
rewardsOwner *secp256k1fx.OutputOwners,
options ...common.Option,
) (*txs.AddPermissionlessDelegatorTx, error)
}
// BuilderBackend specifies the required information needed to build unsigned
// P-chain transactions.
type BuilderBackend interface {
Context
UTXOs(ctx stdcontext.Context, sourceChainID ids.ID) ([]*avax.UTXO, error)
GetTx(ctx stdcontext.Context, txID ids.ID) (*txs.Tx, error)
}
type builder struct {
addrs set.Set[ids.ShortID]
backend BuilderBackend
}
// NewBuilder returns a new transaction builder.
//
// - [addrs] is the set of addresses that the builder assumes can be used when
// signing the transactions in the future.
// - [backend] provides the required access to the chain's context and state
// to build out the transactions.
func NewBuilder(addrs set.Set[ids.ShortID], backend BuilderBackend) Builder {
return &builder{
addrs: addrs,
backend: backend,
}
}
func (b *builder) GetBalance(
options ...common.Option,
) (map[ids.ID]uint64, error) {
ops := common.NewOptions(options)
return b.getBalance(constants.PlatformChainID, ops)
}
func (b *builder) GetImportableBalance(
chainID ids.ID,
options ...common.Option,
) (map[ids.ID]uint64, error) {
ops := common.NewOptions(options)
return b.getBalance(chainID, ops)
}
func (b *builder) NewBaseTx(
outputs []*avax.TransferableOutput,
options ...common.Option,
) (*txs.CreateSubnetTx, error) {
toBurn := map[ids.ID]uint64{
b.backend.AVAXAssetID(): b.backend.CreateSubnetTxFee(),
}
for _, out := range outputs {
assetID := out.AssetID()
amountToBurn, err := math.Add64(toBurn[assetID], out.Out.Amount())
if err != nil {
return nil, err
}
toBurn[assetID] = amountToBurn
}
toStake := map[ids.ID]uint64{}
ops := common.NewOptions(options)
inputs, changeOutputs, _, err := b.spend(toBurn, toStake, ops)
if err != nil {
return nil, err
}
outputs = append(outputs, changeOutputs...)
avax.SortTransferableOutputs(outputs, txs.Codec) // sort the outputs
return &txs.CreateSubnetTx{
BaseTx: txs.BaseTx{BaseTx: avax.BaseTx{
NetworkID: b.backend.NetworkID(),
BlockchainID: constants.PlatformChainID,
Ins: inputs,
Outs: outputs,
Memo: ops.Memo(),
}},
Owner: &secp256k1fx.OutputOwners{},
}, nil
}
func (b *builder) NewAddValidatorTx(
vdr *validator.Validator,
rewardsOwner *secp256k1fx.OutputOwners,
shares uint32,
options ...common.Option,
) (*txs.AddValidatorTx, error) {
avaxAssetID := b.backend.AVAXAssetID()
toBurn := map[ids.ID]uint64{
avaxAssetID: b.backend.AddPrimaryNetworkValidatorFee(),
}
toStake := map[ids.ID]uint64{
avaxAssetID: vdr.Wght,
}
ops := common.NewOptions(options)
inputs, baseOutputs, stakeOutputs, err := b.spend(toBurn, toStake, ops)
if err != nil {
return nil, err
}
utils.Sort(rewardsOwner.Addrs)
return &txs.AddValidatorTx{
BaseTx: txs.BaseTx{BaseTx: avax.BaseTx{
NetworkID: b.backend.NetworkID(),
BlockchainID: constants.PlatformChainID,
Ins: inputs,
Outs: baseOutputs,
Memo: ops.Memo(),
}},
Validator: *vdr,
StakeOuts: stakeOutputs,
RewardsOwner: rewardsOwner,
DelegationShares: shares,
}, nil
}
func (b *builder) NewAddSubnetValidatorTx(
vdr *validator.SubnetValidator,
options ...common.Option,
) (*txs.AddSubnetValidatorTx, error) {
toBurn := map[ids.ID]uint64{
b.backend.AVAXAssetID(): b.backend.AddSubnetValidatorFee(),
}
toStake := map[ids.ID]uint64{}
ops := common.NewOptions(options)
inputs, outputs, _, err := b.spend(toBurn, toStake, ops)
if err != nil {
return nil, err
}
subnetAuth, err := b.authorizeSubnet(vdr.Subnet, ops)
if err != nil {
return nil, err
}
return &txs.AddSubnetValidatorTx{
BaseTx: txs.BaseTx{BaseTx: avax.BaseTx{
NetworkID: b.backend.NetworkID(),
BlockchainID: constants.PlatformChainID,
Ins: inputs,
Outs: outputs,
Memo: ops.Memo(),
}},
Validator: *vdr,
SubnetAuth: subnetAuth,
}, nil
}
func (b *builder) NewRemoveSubnetValidatorTx(
nodeID ids.NodeID,
subnetID ids.ID,
options ...common.Option,
) (*txs.RemoveSubnetValidatorTx, error) {
toBurn := map[ids.ID]uint64{
b.backend.AVAXAssetID(): b.backend.BaseTxFee(),
}
toStake := map[ids.ID]uint64{}
ops := common.NewOptions(options)
inputs, outputs, _, err := b.spend(toBurn, toStake, ops)
if err != nil {
return nil, err
}
subnetAuth, err := b.authorizeSubnet(subnetID, ops)
if err != nil {
return nil, err
}
return &txs.RemoveSubnetValidatorTx{
BaseTx: txs.BaseTx{BaseTx: avax.BaseTx{
NetworkID: b.backend.NetworkID(),
BlockchainID: constants.PlatformChainID,
Ins: inputs,
Outs: outputs,
Memo: ops.Memo(),
}},
Subnet: subnetID,
NodeID: nodeID,
SubnetAuth: subnetAuth,
}, nil
}
func (b *builder) NewAddDelegatorTx(
vdr *validator.Validator,
rewardsOwner *secp256k1fx.OutputOwners,
options ...common.Option,
) (*txs.AddDelegatorTx, error) {
avaxAssetID := b.backend.AVAXAssetID()
toBurn := map[ids.ID]uint64{
avaxAssetID: b.backend.AddPrimaryNetworkDelegatorFee(),
}
toStake := map[ids.ID]uint64{
b.backend.AVAXAssetID(): vdr.Wght,
}
ops := common.NewOptions(options)
inputs, baseOutputs, stakeOutputs, err := b.spend(toBurn, toStake, ops)
if err != nil {
return nil, err
}
utils.Sort(rewardsOwner.Addrs)
return &txs.AddDelegatorTx{
BaseTx: txs.BaseTx{BaseTx: avax.BaseTx{
NetworkID: b.backend.NetworkID(),
BlockchainID: constants.PlatformChainID,
Ins: inputs,
Outs: baseOutputs,
Memo: ops.Memo(),
}},
Validator: *vdr,
StakeOuts: stakeOutputs,
DelegationRewardsOwner: rewardsOwner,
}, nil
}
func (b *builder) NewCreateChainTx(
subnetID ids.ID,
genesis []byte,
vmID ids.ID,
fxIDs []ids.ID,
chainName string,
options ...common.Option,
) (*txs.CreateChainTx, error) {
toBurn := map[ids.ID]uint64{
b.backend.AVAXAssetID(): b.backend.CreateBlockchainTxFee(),
}
toStake := map[ids.ID]uint64{}
ops := common.NewOptions(options)
inputs, outputs, _, err := b.spend(toBurn, toStake, ops)
if err != nil {
return nil, err
}
subnetAuth, err := b.authorizeSubnet(subnetID, ops)
if err != nil {
return nil, err
}
utils.Sort(fxIDs)
return &txs.CreateChainTx{
BaseTx: txs.BaseTx{BaseTx: avax.BaseTx{
NetworkID: b.backend.NetworkID(),
BlockchainID: constants.PlatformChainID,
Ins: inputs,
Outs: outputs,
Memo: ops.Memo(),
}},
SubnetID: subnetID,
ChainName: chainName,
VMID: vmID,
FxIDs: fxIDs,
GenesisData: genesis,
SubnetAuth: subnetAuth,
}, nil
}
func (b *builder) NewCreateSubnetTx(
owner *secp256k1fx.OutputOwners,
options ...common.Option,
) (*txs.CreateSubnetTx, error) {
toBurn := map[ids.ID]uint64{
b.backend.AVAXAssetID(): b.backend.CreateSubnetTxFee(),
}
toStake := map[ids.ID]uint64{}
ops := common.NewOptions(options)
inputs, outputs, _, err := b.spend(toBurn, toStake, ops)
if err != nil {
return nil, err
}
utils.Sort(owner.Addrs)
return &txs.CreateSubnetTx{
BaseTx: txs.BaseTx{BaseTx: avax.BaseTx{
NetworkID: b.backend.NetworkID(),
BlockchainID: constants.PlatformChainID,
Ins: inputs,
Outs: outputs,
Memo: ops.Memo(),
}},
Owner: owner,
}, nil
}
func (b *builder) NewImportTx(
sourceChainID ids.ID,
to *secp256k1fx.OutputOwners,
options ...common.Option,
) (*txs.ImportTx, error) {
ops := common.NewOptions(options)
utxos, err := b.backend.UTXOs(ops.Context(), sourceChainID)
if err != nil {
return nil, err
}
var (
addrs = ops.Addresses(b.addrs)
minIssuanceTime = ops.MinIssuanceTime()
avaxAssetID = b.backend.AVAXAssetID()
txFee = b.backend.BaseTxFee()
importedInputs = make([]*avax.TransferableInput, 0, len(utxos))
importedAmounts = make(map[ids.ID]uint64)
)
// Iterate over the unlocked UTXOs
for _, utxo := range utxos {
out, ok := utxo.Out.(*secp256k1fx.TransferOutput)
if !ok {
continue
}
inputSigIndices, ok := common.MatchOwners(&out.OutputOwners, addrs, minIssuanceTime)
if !ok {
// We couldn't spend this UTXO, so we skip to the next one
continue
}
importedInputs = append(importedInputs, &avax.TransferableInput{
UTXOID: utxo.UTXOID,
Asset: utxo.Asset,
In: &secp256k1fx.TransferInput{
Amt: out.Amt,
Input: secp256k1fx.Input{
SigIndices: inputSigIndices,
},
},
})
assetID := utxo.AssetID()
newImportedAmount, err := math.Add64(importedAmounts[assetID], out.Amt)
if err != nil {
return nil, err
}
importedAmounts[assetID] = newImportedAmount
}
utils.Sort(importedInputs) // sort imported inputs
if len(importedInputs) == 0 {
return nil, fmt.Errorf(
"%w: no UTXOs available to import",
errInsufficientFunds,
)
}
var (
inputs []*avax.TransferableInput
outputs = make([]*avax.TransferableOutput, 0, len(importedAmounts))
importedAVAX = importedAmounts[avaxAssetID]
)
if importedAVAX > txFee {
importedAmounts[avaxAssetID] -= txFee
} else {
if importedAVAX < txFee { // imported amount goes toward paying tx fee
toBurn := map[ids.ID]uint64{
avaxAssetID: txFee - importedAVAX,
}
toStake := map[ids.ID]uint64{}
var err error
inputs, outputs, _, err = b.spend(toBurn, toStake, ops)
if err != nil {
return nil, fmt.Errorf("couldn't generate tx inputs/outputs: %w", err)
}
}
delete(importedAmounts, avaxAssetID)
}
for assetID, amount := range importedAmounts {
outputs = append(outputs, &avax.TransferableOutput{
Asset: avax.Asset{ID: assetID},
Out: &secp256k1fx.TransferOutput{
Amt: amount,
OutputOwners: *to,
},
})
}
avax.SortTransferableOutputs(outputs, txs.Codec) // sort imported outputs
return &txs.ImportTx{
BaseTx: txs.BaseTx{BaseTx: avax.BaseTx{
NetworkID: b.backend.NetworkID(),
BlockchainID: constants.PlatformChainID,
Ins: inputs,
Outs: outputs,
Memo: ops.Memo(),
}},
SourceChain: sourceChainID,
ImportedInputs: importedInputs,
}, nil
}
func (b *builder) NewExportTx(
chainID ids.ID,
outputs []*avax.TransferableOutput,
options ...common.Option,
) (*txs.ExportTx, error) {
toBurn := map[ids.ID]uint64{
b.backend.AVAXAssetID(): b.backend.BaseTxFee(),
}
for _, out := range outputs {
assetID := out.AssetID()
amountToBurn, err := math.Add64(toBurn[assetID], out.Out.Amount())
if err != nil {
return nil, err
}
toBurn[assetID] = amountToBurn
}
toStake := map[ids.ID]uint64{}
ops := common.NewOptions(options)
inputs, changeOutputs, _, err := b.spend(toBurn, toStake, ops)
if err != nil {
return nil, err
}
avax.SortTransferableOutputs(outputs, txs.Codec) // sort exported outputs
return &txs.ExportTx{
BaseTx: txs.BaseTx{BaseTx: avax.BaseTx{
NetworkID: b.backend.NetworkID(),
BlockchainID: constants.PlatformChainID,
Ins: inputs,
Outs: changeOutputs,
Memo: ops.Memo(),
}},
DestinationChain: chainID,
ExportedOutputs: outputs,
}, nil
}
func (b *builder) NewTransformSubnetTx(
subnetID ids.ID,
assetID ids.ID,
initialSupply uint64,
maxSupply uint64,
minConsumptionRate uint64,
maxConsumptionRate uint64,
minValidatorStake uint64,
maxValidatorStake uint64,
minStakeDuration time.Duration,
maxStakeDuration time.Duration,
minDelegationFee uint32,
minDelegatorStake uint64,
maxValidatorWeightFactor byte,
uptimeRequirement uint32,
options ...common.Option,
) (*txs.TransformSubnetTx, error) {
toBurn := map[ids.ID]uint64{
b.backend.AVAXAssetID(): b.backend.TransformSubnetTxFee(),
assetID: maxSupply - initialSupply,
}
toStake := map[ids.ID]uint64{}
ops := common.NewOptions(options)
inputs, outputs, _, err := b.spend(toBurn, toStake, ops)
if err != nil {
return nil, err
}
subnetAuth, err := b.authorizeSubnet(subnetID, ops)
if err != nil {
return nil, err
}
return &txs.TransformSubnetTx{
BaseTx: txs.BaseTx{BaseTx: avax.BaseTx{
NetworkID: b.backend.NetworkID(),
BlockchainID: constants.PlatformChainID,
Ins: inputs,
Outs: outputs,
Memo: ops.Memo(),
}},
Subnet: subnetID,
AssetID: assetID,
InitialSupply: initialSupply,
MaximumSupply: maxSupply,
MinConsumptionRate: minConsumptionRate,
MaxConsumptionRate: maxConsumptionRate,
MinValidatorStake: minValidatorStake,
MaxValidatorStake: maxValidatorStake,
MinStakeDuration: uint32(minStakeDuration / time.Second),
MaxStakeDuration: uint32(maxStakeDuration / time.Second),
MinDelegationFee: minDelegationFee,
MinDelegatorStake: minDelegatorStake,
MaxValidatorWeightFactor: maxValidatorWeightFactor,
UptimeRequirement: uptimeRequirement,
SubnetAuth: subnetAuth,
}, nil
}
func (b *builder) NewAddPermissionlessValidatorTx(
vdr *validator.SubnetValidator,
signer signer.Signer,
assetID ids.ID,
validationRewardsOwner *secp256k1fx.OutputOwners,
delegationRewardsOwner *secp256k1fx.OutputOwners,
shares uint32,
options ...common.Option,
) (*txs.AddPermissionlessValidatorTx, error) {
avaxAssetID := b.backend.AVAXAssetID()
toBurn := map[ids.ID]uint64{}
if vdr.Subnet == constants.PrimaryNetworkID {
toBurn[avaxAssetID] = b.backend.AddPrimaryNetworkValidatorFee()
} else {
toBurn[avaxAssetID] = b.backend.AddSubnetValidatorFee()
}
toStake := map[ids.ID]uint64{
assetID: vdr.Wght,
}
ops := common.NewOptions(options)
inputs, baseOutputs, stakeOutputs, err := b.spend(toBurn, toStake, ops)
if err != nil {
return nil, err
}
utils.Sort(validationRewardsOwner.Addrs)
utils.Sort(delegationRewardsOwner.Addrs)
return &txs.AddPermissionlessValidatorTx{
BaseTx: txs.BaseTx{BaseTx: avax.BaseTx{
NetworkID: b.backend.NetworkID(),
BlockchainID: constants.PlatformChainID,
Ins: inputs,
Outs: baseOutputs,
Memo: ops.Memo(),
}},
Validator: vdr.Validator,
Subnet: vdr.Subnet,
Signer: signer,
StakeOuts: stakeOutputs,
ValidatorRewardsOwner: validationRewardsOwner,
DelegatorRewardsOwner: delegationRewardsOwner,
DelegationShares: shares,
}, nil
}
func (b *builder) NewAddPermissionlessDelegatorTx(
vdr *validator.SubnetValidator,
assetID ids.ID,
rewardsOwner *secp256k1fx.OutputOwners,
options ...common.Option,
) (*txs.AddPermissionlessDelegatorTx, error) {
avaxAssetID := b.backend.AVAXAssetID()
toBurn := map[ids.ID]uint64{}
if vdr.Subnet == constants.PrimaryNetworkID {
toBurn[avaxAssetID] = b.backend.AddPrimaryNetworkDelegatorFee()
} else {
toBurn[avaxAssetID] = b.backend.AddSubnetDelegatorFee()
}
toStake := map[ids.ID]uint64{
assetID: vdr.Wght,
}
ops := common.NewOptions(options)
inputs, baseOutputs, stakeOutputs, err := b.spend(toBurn, toStake, ops)
if err != nil {
return nil, err
}
utils.Sort(rewardsOwner.Addrs)
return &txs.AddPermissionlessDelegatorTx{
BaseTx: txs.BaseTx{BaseTx: avax.BaseTx{
NetworkID: b.backend.NetworkID(),
BlockchainID: constants.PlatformChainID,
Ins: inputs,
Outs: baseOutputs,
Memo: ops.Memo(),
}},
Validator: vdr.Validator,
Subnet: vdr.Subnet,
StakeOuts: stakeOutputs,
DelegationRewardsOwner: rewardsOwner,
}, nil
}
func (b *builder) getBalance(
chainID ids.ID,
options *common.Options,
) (
balance map[ids.ID]uint64,
err error,
) {
utxos, err := b.backend.UTXOs(options.Context(), chainID)
if err != nil {
return nil, err
}
addrs := options.Addresses(b.addrs)
minIssuanceTime := options.MinIssuanceTime()
balance = make(map[ids.ID]uint64)
// Iterate over the UTXOs
for _, utxo := range utxos {
outIntf := utxo.Out
if lockedOut, ok := outIntf.(*stakeable.LockOut); ok {
if !options.AllowStakeableLocked() && lockedOut.Locktime > minIssuanceTime {
// This output is currently locked, so this output can't be
// burned.
continue
}
outIntf = lockedOut.TransferableOut
}
out, ok := outIntf.(*secp256k1fx.TransferOutput)
if !ok {
return nil, errUnknownOutputType
}
_, ok = common.MatchOwners(&out.OutputOwners, addrs, minIssuanceTime)
if !ok {
// We couldn't spend this UTXO, so we skip to the next one
continue
}
assetID := utxo.AssetID()
balance[assetID], err = math.Add64(balance[assetID], out.Amt)
if err != nil {
return nil, err
}
}
return balance, nil
}
// spend takes in the requested burn amounts and the requested stake amounts.
//
// - [amountsToBurn] maps assetID to the amount of the asset to spend without
// producing an output. This is typically used for fees. However, it can
// also be used to consume some of an asset that will be produced in
// separate outputs, such as ExportedOutputs. Only unlocked UTXOs are able
// to be burned here.
// - [amountsToStake] maps assetID to the amount of the asset to spend and
// place into the staked outputs. First locked UTXOs are attempted to be
// used for these funds, and then unlocked UTXOs will be attempted to be
// used. There is no preferential ordering on the unlock times.
func (b *builder) spend(
amountsToBurn map[ids.ID]uint64,
amountsToStake map[ids.ID]uint64,
options *common.Options,
) (
inputs []*avax.TransferableInput,
changeOutputs []*avax.TransferableOutput,
stakeOutputs []*avax.TransferableOutput,
err error,
) {
utxos, err := b.backend.UTXOs(options.Context(), constants.PlatformChainID)
if err != nil {
return nil, nil, nil, err
}
addrs := options.Addresses(b.addrs)
minIssuanceTime := options.MinIssuanceTime()
addr, ok := addrs.Peek()
if !ok {
return nil, nil, nil, errNoChangeAddress
}
changeOwner := options.ChangeOwner(&secp256k1fx.OutputOwners{
Threshold: 1,
Addrs: []ids.ShortID{addr},
})
// Iterate over the locked UTXOs
for _, utxo := range utxos {
assetID := utxo.AssetID()
remainingAmountToStake := amountsToStake[assetID]
// If we have staked enough of the asset, then we have no need burn
// more.
if remainingAmountToStake == 0 {
continue
}
outIntf := utxo.Out
lockedOut, ok := outIntf.(*stakeable.LockOut)
if !ok {
// This output isn't locked, so it will be handled during the next
// iteration of the UTXO set
continue
}
if minIssuanceTime >= lockedOut.Locktime {
// This output isn't locked, so it will be handled during the next
// iteration of the UTXO set
continue
}
out, ok := lockedOut.TransferableOut.(*secp256k1fx.TransferOutput)
if !ok {
return nil, nil, nil, errUnknownOutputType
}
inputSigIndices, ok := common.MatchOwners(&out.OutputOwners, addrs, minIssuanceTime)
if !ok {
// We couldn't spend this UTXO, so we skip to the next one
continue
}
inputs = append(inputs, &avax.TransferableInput{
UTXOID: utxo.UTXOID,
Asset: utxo.Asset,
In: &stakeable.LockIn{
Locktime: lockedOut.Locktime,
TransferableIn: &secp256k1fx.TransferInput{
Amt: out.Amt,
Input: secp256k1fx.Input{
SigIndices: inputSigIndices,
},
},
},
})
// Stake any value that should be staked
amountToStake := math.Min(
remainingAmountToStake, // Amount we still need to stake
out.Amt, // Amount available to stake
)
// Add the output to the staked outputs
stakeOutputs = append(stakeOutputs, &avax.TransferableOutput{
Asset: utxo.Asset,
Out: &stakeable.LockOut{
Locktime: lockedOut.Locktime,
TransferableOut: &secp256k1fx.TransferOutput{
Amt: amountToStake,
OutputOwners: out.OutputOwners,
},
},
})
amountsToStake[assetID] -= amountToStake
if remainingAmount := out.Amt - amountToStake; remainingAmount > 0 {
// This input had extra value, so some of it must be returned
changeOutputs = append(changeOutputs, &avax.TransferableOutput{
Asset: utxo.Asset,
Out: &stakeable.LockOut{
Locktime: lockedOut.Locktime,
TransferableOut: &secp256k1fx.TransferOutput{
Amt: remainingAmount,
OutputOwners: out.OutputOwners,
},
},
})
}
}
// Iterate over the unlocked UTXOs
for _, utxo := range utxos {
assetID := utxo.AssetID()
remainingAmountToStake := amountsToStake[assetID]
remainingAmountToBurn := amountsToBurn[assetID]
// If we have consumed enough of the asset, then we have no need burn
// more.
if remainingAmountToStake == 0 && remainingAmountToBurn == 0 {
continue
}
outIntf := utxo.Out
if lockedOut, ok := outIntf.(*stakeable.LockOut); ok {
if lockedOut.Locktime > minIssuanceTime {
// This output is currently locked, so this output can't be
// burned.