-
Notifications
You must be signed in to change notification settings - Fork 660
/
builder.go
714 lines (655 loc) · 19 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
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package builder
import (
"errors"
"fmt"
"time"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/utils"
"github.com/ava-labs/avalanchego/utils/crypto/secp256k1"
"github.com/ava-labs/avalanchego/utils/math"
"github.com/ava-labs/avalanchego/utils/timer/mockable"
"github.com/ava-labs/avalanchego/vms/components/avax"
"github.com/ava-labs/avalanchego/vms/platformvm/config"
"github.com/ava-labs/avalanchego/vms/platformvm/fx"
"github.com/ava-labs/avalanchego/vms/platformvm/state"
"github.com/ava-labs/avalanchego/vms/platformvm/txs"
"github.com/ava-labs/avalanchego/vms/platformvm/utxo"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"
)
// Max number of items allowed in a page
const MaxPageSize = 1024
var (
_ Builder = (*builder)(nil)
ErrNoFunds = errors.New("no spendable funds were found")
)
type Builder interface {
AtomicTxBuilder
DecisionTxBuilder
ProposalTxBuilder
}
type AtomicTxBuilder interface {
// chainID: chain to import UTXOs from
// to: address of recipient
// keys: keys to import the funds
// changeAddr: address to send change to, if there is any
NewImportTx(
chainID ids.ID,
to ids.ShortID,
keys []*secp256k1.PrivateKey,
changeAddr ids.ShortID,
) (*txs.Tx, error)
// amount: amount of tokens to export
// chainID: chain to send the UTXOs to
// to: address of recipient
// keys: keys to pay the fee and provide the tokens
// changeAddr: address to send change to, if there is any
NewExportTx(
amount uint64,
chainID ids.ID,
to ids.ShortID,
keys []*secp256k1.PrivateKey,
changeAddr ids.ShortID,
) (*txs.Tx, error)
}
type DecisionTxBuilder interface {
// subnetID: ID of the subnet that validates the new chain
// genesisData: byte repr. of genesis state of the new chain
// vmID: ID of VM this chain runs
// fxIDs: ids of features extensions this chain supports
// chainName: name of the chain
// keys: keys to sign the tx
// changeAddr: address to send change to, if there is any
NewCreateChainTx(
subnetID ids.ID,
genesisData []byte,
vmID ids.ID,
fxIDs []ids.ID,
chainName string,
keys []*secp256k1.PrivateKey,
changeAddr ids.ShortID,
) (*txs.Tx, error)
// threshold: [threshold] of [ownerAddrs] needed to manage this subnet
// ownerAddrs: control addresses for the new subnet
// keys: keys to pay the fee
// changeAddr: address to send change to, if there is any
NewCreateSubnetTx(
threshold uint32,
ownerAddrs []ids.ShortID,
keys []*secp256k1.PrivateKey,
changeAddr ids.ShortID,
) (*txs.Tx, error)
// amount: amount the sender is sending
// owner: recipient of the funds
// keys: keys to sign the tx and pay the amount
// changeAddr: address to send change to, if there is any
NewBaseTx(
amount uint64,
owner secp256k1fx.OutputOwners,
keys []*secp256k1.PrivateKey,
changeAddr ids.ShortID,
) (*txs.Tx, error)
}
type ProposalTxBuilder interface {
// stakeAmount: amount the validator stakes
// startTime: unix time they start validating
// endTime: unix time they stop validating
// nodeID: ID of the node we want to validate with
// rewardAddress: address to send reward to, if applicable
// shares: 10,000 times percentage of reward taken from delegators
// keys: Keys providing the staked tokens
// changeAddr: Address to send change to, if there is any
NewAddValidatorTx(
stakeAmount,
startTime,
endTime uint64,
nodeID ids.NodeID,
rewardAddress ids.ShortID,
shares uint32,
keys []*secp256k1.PrivateKey,
changeAddr ids.ShortID,
) (*txs.Tx, error)
// stakeAmount: amount the delegator stakes
// startTime: unix time they start delegating
// endTime: unix time they stop delegating
// nodeID: ID of the node we are delegating to
// rewardAddress: address to send reward to, if applicable
// keys: keys providing the staked tokens
// changeAddr: address to send change to, if there is any
NewAddDelegatorTx(
stakeAmount,
startTime,
endTime uint64,
nodeID ids.NodeID,
rewardAddress ids.ShortID,
keys []*secp256k1.PrivateKey,
changeAddr ids.ShortID,
) (*txs.Tx, error)
// weight: sampling weight of the new validator
// startTime: unix time they start delegating
// endTime: unix time they top delegating
// nodeID: ID of the node validating
// subnetID: ID of the subnet the validator will validate
// keys: keys to use for adding the validator
// changeAddr: address to send change to, if there is any
NewAddSubnetValidatorTx(
weight,
startTime,
endTime uint64,
nodeID ids.NodeID,
subnetID ids.ID,
keys []*secp256k1.PrivateKey,
changeAddr ids.ShortID,
) (*txs.Tx, error)
// Creates a transaction that removes [nodeID]
// as a validator from [subnetID]
// keys: keys to use for removing the validator
// changeAddr: address to send change to, if there is any
NewRemoveSubnetValidatorTx(
nodeID ids.NodeID,
subnetID ids.ID,
keys []*secp256k1.PrivateKey,
changeAddr ids.ShortID,
) (*txs.Tx, error)
// Creates a transaction that transfers ownership of [subnetID]
// threshold: [threshold] of [ownerAddrs] needed to manage this subnet
// ownerAddrs: control addresses for the new subnet
// keys: keys to use for modifying the subnet
// changeAddr: address to send change to, if there is any
NewTransferSubnetOwnershipTx(
subnetID ids.ID,
threshold uint32,
ownerAddrs []ids.ShortID,
keys []*secp256k1.PrivateKey,
changeAddr ids.ShortID,
) (*txs.Tx, error)
// newAdvanceTimeTx creates a new tx that, if it is accepted and followed by a
// Commit block, will set the chain's timestamp to [timestamp].
NewAdvanceTimeTx(timestamp time.Time) (*txs.Tx, error)
// RewardStakerTx creates a new transaction that proposes to remove the staker
// [validatorID] from the default validator set.
NewRewardValidatorTx(txID ids.ID) (*txs.Tx, error)
}
func New(
ctx *snow.Context,
cfg *config.Config,
clk *mockable.Clock,
fx fx.Fx,
state state.State,
atomicUTXOManager avax.AtomicUTXOManager,
utxoSpender utxo.Spender,
) Builder {
return &builder{
AtomicUTXOManager: atomicUTXOManager,
Spender: utxoSpender,
state: state,
cfg: cfg,
ctx: ctx,
clk: clk,
fx: fx,
}
}
type builder struct {
avax.AtomicUTXOManager
utxo.Spender
state state.State
cfg *config.Config
ctx *snow.Context
clk *mockable.Clock
fx fx.Fx
}
func (b *builder) NewImportTx(
from ids.ID,
to ids.ShortID,
keys []*secp256k1.PrivateKey,
changeAddr ids.ShortID,
) (*txs.Tx, error) {
kc := secp256k1fx.NewKeychain(keys...)
atomicUTXOs, _, _, err := b.GetAtomicUTXOs(from, kc.Addresses(), ids.ShortEmpty, ids.Empty, MaxPageSize)
if err != nil {
return nil, fmt.Errorf("problem retrieving atomic UTXOs: %w", err)
}
importedInputs := []*avax.TransferableInput{}
signers := [][]*secp256k1.PrivateKey{}
importedAmounts := make(map[ids.ID]uint64)
now := b.clk.Unix()
for _, utxo := range atomicUTXOs {
inputIntf, utxoSigners, err := kc.Spend(utxo.Out, now)
if err != nil {
continue
}
input, ok := inputIntf.(avax.TransferableIn)
if !ok {
continue
}
assetID := utxo.AssetID()
importedAmounts[assetID], err = math.Add64(importedAmounts[assetID], input.Amount())
if err != nil {
return nil, err
}
importedInputs = append(importedInputs, &avax.TransferableInput{
UTXOID: utxo.UTXOID,
Asset: utxo.Asset,
In: input,
})
signers = append(signers, utxoSigners)
}
avax.SortTransferableInputsWithSigners(importedInputs, signers)
if len(importedAmounts) == 0 {
return nil, ErrNoFunds // No imported UTXOs were spendable
}
importedAVAX := importedAmounts[b.ctx.AVAXAssetID]
ins := []*avax.TransferableInput{}
outs := []*avax.TransferableOutput{}
switch {
case importedAVAX < b.cfg.TxFee: // imported amount goes toward paying tx fee
var baseSigners [][]*secp256k1.PrivateKey
ins, outs, _, baseSigners, err = b.Spend(b.state, keys, 0, b.cfg.TxFee-importedAVAX, changeAddr)
if err != nil {
return nil, fmt.Errorf("couldn't generate tx inputs/outputs: %w", err)
}
signers = append(baseSigners, signers...)
delete(importedAmounts, b.ctx.AVAXAssetID)
case importedAVAX == b.cfg.TxFee:
delete(importedAmounts, b.ctx.AVAXAssetID)
default:
importedAmounts[b.ctx.AVAXAssetID] -= b.cfg.TxFee
}
for assetID, amount := range importedAmounts {
outs = append(outs, &avax.TransferableOutput{
Asset: avax.Asset{ID: assetID},
Out: &secp256k1fx.TransferOutput{
Amt: amount,
OutputOwners: secp256k1fx.OutputOwners{
Locktime: 0,
Threshold: 1,
Addrs: []ids.ShortID{to},
},
},
})
}
avax.SortTransferableOutputs(outs, txs.Codec) // sort imported outputs
// Create the transaction
utx := &txs.ImportTx{
BaseTx: txs.BaseTx{BaseTx: avax.BaseTx{
NetworkID: b.ctx.NetworkID,
BlockchainID: b.ctx.ChainID,
Outs: outs,
Ins: ins,
}},
SourceChain: from,
ImportedInputs: importedInputs,
}
tx, err := txs.NewSigned(utx, txs.Codec, signers)
if err != nil {
return nil, err
}
return tx, tx.SyntacticVerify(b.ctx)
}
// TODO: should support other assets than AVAX
func (b *builder) NewExportTx(
amount uint64,
chainID ids.ID,
to ids.ShortID,
keys []*secp256k1.PrivateKey,
changeAddr ids.ShortID,
) (*txs.Tx, error) {
toBurn, err := math.Add64(amount, b.cfg.TxFee)
if err != nil {
return nil, fmt.Errorf("amount (%d) + tx fee(%d) overflows", amount, b.cfg.TxFee)
}
ins, outs, _, signers, err := b.Spend(b.state, keys, 0, toBurn, changeAddr)
if err != nil {
return nil, fmt.Errorf("couldn't generate tx inputs/outputs: %w", err)
}
// Create the transaction
utx := &txs.ExportTx{
BaseTx: txs.BaseTx{BaseTx: avax.BaseTx{
NetworkID: b.ctx.NetworkID,
BlockchainID: b.ctx.ChainID,
Ins: ins,
Outs: outs, // Non-exported outputs
}},
DestinationChain: chainID,
ExportedOutputs: []*avax.TransferableOutput{{ // Exported to X-Chain
Asset: avax.Asset{ID: b.ctx.AVAXAssetID},
Out: &secp256k1fx.TransferOutput{
Amt: amount,
OutputOwners: secp256k1fx.OutputOwners{
Locktime: 0,
Threshold: 1,
Addrs: []ids.ShortID{to},
},
},
}},
}
tx, err := txs.NewSigned(utx, txs.Codec, signers)
if err != nil {
return nil, err
}
return tx, tx.SyntacticVerify(b.ctx)
}
func (b *builder) NewCreateChainTx(
subnetID ids.ID,
genesisData []byte,
vmID ids.ID,
fxIDs []ids.ID,
chainName string,
keys []*secp256k1.PrivateKey,
changeAddr ids.ShortID,
) (*txs.Tx, error) {
timestamp := b.state.GetTimestamp()
createBlockchainTxFee := b.cfg.GetCreateBlockchainTxFee(timestamp)
ins, outs, _, signers, err := b.Spend(b.state, keys, 0, createBlockchainTxFee, changeAddr)
if err != nil {
return nil, fmt.Errorf("couldn't generate tx inputs/outputs: %w", err)
}
subnetAuth, subnetSigners, err := b.Authorize(b.state, subnetID, keys)
if err != nil {
return nil, fmt.Errorf("couldn't authorize tx's subnet restrictions: %w", err)
}
signers = append(signers, subnetSigners)
// Sort the provided fxIDs
utils.Sort(fxIDs)
// Create the tx
utx := &txs.CreateChainTx{
BaseTx: txs.BaseTx{BaseTx: avax.BaseTx{
NetworkID: b.ctx.NetworkID,
BlockchainID: b.ctx.ChainID,
Ins: ins,
Outs: outs,
}},
SubnetID: subnetID,
ChainName: chainName,
VMID: vmID,
FxIDs: fxIDs,
GenesisData: genesisData,
SubnetAuth: subnetAuth,
}
tx, err := txs.NewSigned(utx, txs.Codec, signers)
if err != nil {
return nil, err
}
return tx, tx.SyntacticVerify(b.ctx)
}
func (b *builder) NewCreateSubnetTx(
threshold uint32,
ownerAddrs []ids.ShortID,
keys []*secp256k1.PrivateKey,
changeAddr ids.ShortID,
) (*txs.Tx, error) {
timestamp := b.state.GetTimestamp()
createSubnetTxFee := b.cfg.GetCreateSubnetTxFee(timestamp)
ins, outs, _, signers, err := b.Spend(b.state, keys, 0, createSubnetTxFee, changeAddr)
if err != nil {
return nil, fmt.Errorf("couldn't generate tx inputs/outputs: %w", err)
}
// Sort control addresses
utils.Sort(ownerAddrs)
// Create the tx
utx := &txs.CreateSubnetTx{
BaseTx: txs.BaseTx{BaseTx: avax.BaseTx{
NetworkID: b.ctx.NetworkID,
BlockchainID: b.ctx.ChainID,
Ins: ins,
Outs: outs,
}},
Owner: &secp256k1fx.OutputOwners{
Threshold: threshold,
Addrs: ownerAddrs,
},
}
tx, err := txs.NewSigned(utx, txs.Codec, signers)
if err != nil {
return nil, err
}
return tx, tx.SyntacticVerify(b.ctx)
}
func (b *builder) NewAddValidatorTx(
stakeAmount,
startTime,
endTime uint64,
nodeID ids.NodeID,
rewardAddress ids.ShortID,
shares uint32,
keys []*secp256k1.PrivateKey,
changeAddr ids.ShortID,
) (*txs.Tx, error) {
ins, unstakedOuts, stakedOuts, signers, err := b.Spend(b.state, keys, stakeAmount, b.cfg.AddPrimaryNetworkValidatorFee, changeAddr)
if err != nil {
return nil, fmt.Errorf("couldn't generate tx inputs/outputs: %w", err)
}
// Create the tx
utx := &txs.AddValidatorTx{
BaseTx: txs.BaseTx{BaseTx: avax.BaseTx{
NetworkID: b.ctx.NetworkID,
BlockchainID: b.ctx.ChainID,
Ins: ins,
Outs: unstakedOuts,
}},
Validator: txs.Validator{
NodeID: nodeID,
Start: startTime,
End: endTime,
Wght: stakeAmount,
},
StakeOuts: stakedOuts,
RewardsOwner: &secp256k1fx.OutputOwners{
Locktime: 0,
Threshold: 1,
Addrs: []ids.ShortID{rewardAddress},
},
DelegationShares: shares,
}
tx, err := txs.NewSigned(utx, txs.Codec, signers)
if err != nil {
return nil, err
}
return tx, tx.SyntacticVerify(b.ctx)
}
func (b *builder) NewAddDelegatorTx(
stakeAmount,
startTime,
endTime uint64,
nodeID ids.NodeID,
rewardAddress ids.ShortID,
keys []*secp256k1.PrivateKey,
changeAddr ids.ShortID,
) (*txs.Tx, error) {
ins, unlockedOuts, lockedOuts, signers, err := b.Spend(b.state, keys, stakeAmount, b.cfg.AddPrimaryNetworkDelegatorFee, changeAddr)
if err != nil {
return nil, fmt.Errorf("couldn't generate tx inputs/outputs: %w", err)
}
// Create the tx
utx := &txs.AddDelegatorTx{
BaseTx: txs.BaseTx{BaseTx: avax.BaseTx{
NetworkID: b.ctx.NetworkID,
BlockchainID: b.ctx.ChainID,
Ins: ins,
Outs: unlockedOuts,
}},
Validator: txs.Validator{
NodeID: nodeID,
Start: startTime,
End: endTime,
Wght: stakeAmount,
},
StakeOuts: lockedOuts,
DelegationRewardsOwner: &secp256k1fx.OutputOwners{
Locktime: 0,
Threshold: 1,
Addrs: []ids.ShortID{rewardAddress},
},
}
tx, err := txs.NewSigned(utx, txs.Codec, signers)
if err != nil {
return nil, err
}
return tx, tx.SyntacticVerify(b.ctx)
}
func (b *builder) NewAddSubnetValidatorTx(
weight,
startTime,
endTime uint64,
nodeID ids.NodeID,
subnetID ids.ID,
keys []*secp256k1.PrivateKey,
changeAddr ids.ShortID,
) (*txs.Tx, error) {
ins, outs, _, signers, err := b.Spend(b.state, keys, 0, b.cfg.TxFee, changeAddr)
if err != nil {
return nil, fmt.Errorf("couldn't generate tx inputs/outputs: %w", err)
}
subnetAuth, subnetSigners, err := b.Authorize(b.state, subnetID, keys)
if err != nil {
return nil, fmt.Errorf("couldn't authorize tx's subnet restrictions: %w", err)
}
signers = append(signers, subnetSigners)
// Create the tx
utx := &txs.AddSubnetValidatorTx{
BaseTx: txs.BaseTx{BaseTx: avax.BaseTx{
NetworkID: b.ctx.NetworkID,
BlockchainID: b.ctx.ChainID,
Ins: ins,
Outs: outs,
}},
SubnetValidator: txs.SubnetValidator{
Validator: txs.Validator{
NodeID: nodeID,
Start: startTime,
End: endTime,
Wght: weight,
},
Subnet: subnetID,
},
SubnetAuth: subnetAuth,
}
tx, err := txs.NewSigned(utx, txs.Codec, signers)
if err != nil {
return nil, err
}
return tx, tx.SyntacticVerify(b.ctx)
}
func (b *builder) NewRemoveSubnetValidatorTx(
nodeID ids.NodeID,
subnetID ids.ID,
keys []*secp256k1.PrivateKey,
changeAddr ids.ShortID,
) (*txs.Tx, error) {
ins, outs, _, signers, err := b.Spend(b.state, keys, 0, b.cfg.TxFee, changeAddr)
if err != nil {
return nil, fmt.Errorf("couldn't generate tx inputs/outputs: %w", err)
}
subnetAuth, subnetSigners, err := b.Authorize(b.state, subnetID, keys)
if err != nil {
return nil, fmt.Errorf("couldn't authorize tx's subnet restrictions: %w", err)
}
signers = append(signers, subnetSigners)
// Create the tx
utx := &txs.RemoveSubnetValidatorTx{
BaseTx: txs.BaseTx{BaseTx: avax.BaseTx{
NetworkID: b.ctx.NetworkID,
BlockchainID: b.ctx.ChainID,
Ins: ins,
Outs: outs,
}},
Subnet: subnetID,
NodeID: nodeID,
SubnetAuth: subnetAuth,
}
tx, err := txs.NewSigned(utx, txs.Codec, signers)
if err != nil {
return nil, err
}
return tx, tx.SyntacticVerify(b.ctx)
}
func (b *builder) NewAdvanceTimeTx(timestamp time.Time) (*txs.Tx, error) {
utx := &txs.AdvanceTimeTx{Time: uint64(timestamp.Unix())}
tx, err := txs.NewSigned(utx, txs.Codec, nil)
if err != nil {
return nil, err
}
return tx, tx.SyntacticVerify(b.ctx)
}
func (b *builder) NewRewardValidatorTx(txID ids.ID) (*txs.Tx, error) {
utx := &txs.RewardValidatorTx{TxID: txID}
tx, err := txs.NewSigned(utx, txs.Codec, nil)
if err != nil {
return nil, err
}
return tx, tx.SyntacticVerify(b.ctx)
}
func (b *builder) NewTransferSubnetOwnershipTx(
subnetID ids.ID,
threshold uint32,
ownerAddrs []ids.ShortID,
keys []*secp256k1.PrivateKey,
changeAddr ids.ShortID,
) (*txs.Tx, error) {
ins, outs, _, signers, err := b.Spend(b.state, keys, 0, b.cfg.TxFee, changeAddr)
if err != nil {
return nil, fmt.Errorf("couldn't generate tx inputs/outputs: %w", err)
}
subnetAuth, subnetSigners, err := b.Authorize(b.state, subnetID, keys)
if err != nil {
return nil, fmt.Errorf("couldn't authorize tx's subnet restrictions: %w", err)
}
signers = append(signers, subnetSigners)
utx := &txs.TransferSubnetOwnershipTx{
BaseTx: txs.BaseTx{BaseTx: avax.BaseTx{
NetworkID: b.ctx.NetworkID,
BlockchainID: b.ctx.ChainID,
Ins: ins,
Outs: outs,
}},
Subnet: subnetID,
SubnetAuth: subnetAuth,
Owner: &secp256k1fx.OutputOwners{
Threshold: threshold,
Addrs: ownerAddrs,
},
}
tx, err := txs.NewSigned(utx, txs.Codec, signers)
if err != nil {
return nil, err
}
return tx, tx.SyntacticVerify(b.ctx)
}
func (b *builder) NewBaseTx(
amount uint64,
owner secp256k1fx.OutputOwners,
keys []*secp256k1.PrivateKey,
changeAddr ids.ShortID,
) (*txs.Tx, error) {
toBurn, err := math.Add64(amount, b.cfg.TxFee)
if err != nil {
return nil, fmt.Errorf("amount (%d) + tx fee(%d) overflows", amount, b.cfg.TxFee)
}
ins, outs, _, signers, err := b.Spend(b.state, keys, 0, toBurn, changeAddr)
if err != nil {
return nil, fmt.Errorf("couldn't generate tx inputs/outputs: %w", err)
}
outs = append(outs, &avax.TransferableOutput{
Asset: avax.Asset{ID: b.ctx.AVAXAssetID},
Out: &secp256k1fx.TransferOutput{
Amt: amount,
OutputOwners: owner,
},
})
avax.SortTransferableOutputs(outs, txs.Codec)
utx := &txs.BaseTx{
BaseTx: avax.BaseTx{
NetworkID: b.ctx.NetworkID,
BlockchainID: b.ctx.ChainID,
Ins: ins,
Outs: outs,
},
}
tx, err := txs.NewSigned(utx, txs.Codec, signers)
if err != nil {
return nil, err
}
return tx, tx.SyntacticVerify(b.ctx)
}