-
Notifications
You must be signed in to change notification settings - Fork 671
/
wallet.go
509 lines (464 loc) · 14.5 KB
/
wallet.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
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package p
import (
"errors"
"time"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/vms/components/avax"
"github.com/ava-labs/avalanchego/vms/platformvm"
"github.com/ava-labs/avalanchego/vms/platformvm/signer"
"github.com/ava-labs/avalanchego/vms/platformvm/status"
"github.com/ava-labs/avalanchego/vms/platformvm/txs"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"
"github.com/ava-labs/avalanchego/wallet/subnet/primary/common"
)
var (
errNotCommitted = errors.New("not committed")
_ Wallet = (*wallet)(nil)
)
type Wallet interface {
Context
// Builder returns the builder that will be used to create the transactions.
Builder() Builder
// Signer returns the signer that will be used to sign the transactions.
Signer() Signer
// IssueBaseTx creates, signs, and issues 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.
IssueBaseTx(
outputs []*avax.TransferableOutput,
options ...common.Option,
) (ids.ID, error)
// IssueAddValidatorTx creates, signs, and issues 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].
IssueAddValidatorTx(
vdr *txs.Validator,
rewardsOwner *secp256k1fx.OutputOwners,
shares uint32,
options ...common.Option,
) (ids.ID, error)
// IssueAddSubnetValidatorTx creates, signs, and issues 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.
IssueAddSubnetValidatorTx(
vdr *txs.SubnetValidator,
options ...common.Option,
) (ids.ID, error)
// IssueAddSubnetValidatorTx creates, signs, and issues a transaction that
// removes a validator of a subnet.
//
// - [nodeID] is the validator being removed from [subnetID].
IssueRemoveSubnetValidatorTx(
nodeID ids.NodeID,
subnetID ids.ID,
options ...common.Option,
) (ids.ID, error)
// IssueAddDelegatorTx creates, signs, and issues 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.
IssueAddDelegatorTx(
vdr *txs.Validator,
rewardsOwner *secp256k1fx.OutputOwners,
options ...common.Option,
) (ids.ID, error)
// IssueCreateChainTx creates, signs, and issues 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.
IssueCreateChainTx(
subnetID ids.ID,
genesis []byte,
vmID ids.ID,
fxIDs []ids.ID,
chainName string,
options ...common.Option,
) (ids.ID, error)
// IssueCreateSubnetTx creates, signs, and issues a new subnet with the
// specified owner.
//
// - [owner] specifies who has the ability to create new chains and add new
// validators to the subnet.
IssueCreateSubnetTx(
owner *secp256k1fx.OutputOwners,
options ...common.Option,
) (ids.ID, error)
// IssueImportTx creates, signs, and issues 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.
IssueImportTx(
chainID ids.ID,
to *secp256k1fx.OutputOwners,
options ...common.Option,
) (ids.ID, error)
// IssueExportTx creates, signs, and issues 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].
IssueExportTx(
chainID ids.ID,
outputs []*avax.TransferableOutput,
options ...common.Option,
) (ids.ID, error)
// IssueTransformSubnetTx 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.
IssueTransformSubnetTx(
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,
) (ids.ID, error)
// IssueAddPermissionlessValidatorTx creates, signs, and issues 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].
IssueAddPermissionlessValidatorTx(
vdr *txs.SubnetValidator,
signer signer.Signer,
assetID ids.ID,
validationRewardsOwner *secp256k1fx.OutputOwners,
delegationRewardsOwner *secp256k1fx.OutputOwners,
shares uint32,
options ...common.Option,
) (ids.ID, error)
// IssueAddPermissionlessDelegatorTx creates, signs, and issues 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.
IssueAddPermissionlessDelegatorTx(
vdr *txs.SubnetValidator,
assetID ids.ID,
rewardsOwner *secp256k1fx.OutputOwners,
options ...common.Option,
) (ids.ID, error)
// IssueUnsignedTx signs and issues the unsigned tx.
IssueUnsignedTx(
utx txs.UnsignedTx,
options ...common.Option,
) (ids.ID, error)
// IssueTx issues the signed tx.
IssueTx(
tx *txs.Tx,
options ...common.Option,
) (ids.ID, error)
}
func NewWallet(
builder Builder,
signer Signer,
client platformvm.Client,
backend Backend,
) Wallet {
return &wallet{
Backend: backend,
builder: builder,
signer: signer,
client: client,
}
}
type wallet struct {
Backend
builder Builder
signer Signer
client platformvm.Client
}
func (w *wallet) Builder() Builder {
return w.builder
}
func (w *wallet) Signer() Signer {
return w.signer
}
func (w *wallet) IssueBaseTx(
outputs []*avax.TransferableOutput,
options ...common.Option,
) (ids.ID, error) {
utx, err := w.builder.NewBaseTx(outputs, options...)
if err != nil {
return ids.Empty, err
}
return w.IssueUnsignedTx(utx, options...)
}
func (w *wallet) IssueAddValidatorTx(
vdr *txs.Validator,
rewardsOwner *secp256k1fx.OutputOwners,
shares uint32,
options ...common.Option,
) (ids.ID, error) {
utx, err := w.builder.NewAddValidatorTx(vdr, rewardsOwner, shares, options...)
if err != nil {
return ids.Empty, err
}
return w.IssueUnsignedTx(utx, options...)
}
func (w *wallet) IssueAddSubnetValidatorTx(
vdr *txs.SubnetValidator,
options ...common.Option,
) (ids.ID, error) {
utx, err := w.builder.NewAddSubnetValidatorTx(vdr, options...)
if err != nil {
return ids.Empty, err
}
return w.IssueUnsignedTx(utx, options...)
}
func (w *wallet) IssueRemoveSubnetValidatorTx(
nodeID ids.NodeID,
subnetID ids.ID,
options ...common.Option,
) (ids.ID, error) {
utx, err := w.builder.NewRemoveSubnetValidatorTx(nodeID, subnetID, options...)
if err != nil {
return ids.Empty, err
}
return w.IssueUnsignedTx(utx, options...)
}
func (w *wallet) IssueAddDelegatorTx(
vdr *txs.Validator,
rewardsOwner *secp256k1fx.OutputOwners,
options ...common.Option,
) (ids.ID, error) {
utx, err := w.builder.NewAddDelegatorTx(vdr, rewardsOwner, options...)
if err != nil {
return ids.Empty, err
}
return w.IssueUnsignedTx(utx, options...)
}
func (w *wallet) IssueCreateChainTx(
subnetID ids.ID,
genesis []byte,
vmID ids.ID,
fxIDs []ids.ID,
chainName string,
options ...common.Option,
) (ids.ID, error) {
utx, err := w.builder.NewCreateChainTx(subnetID, genesis, vmID, fxIDs, chainName, options...)
if err != nil {
return ids.Empty, err
}
return w.IssueUnsignedTx(utx, options...)
}
func (w *wallet) IssueCreateSubnetTx(
owner *secp256k1fx.OutputOwners,
options ...common.Option,
) (ids.ID, error) {
utx, err := w.builder.NewCreateSubnetTx(owner, options...)
if err != nil {
return ids.Empty, err
}
return w.IssueUnsignedTx(utx, options...)
}
func (w *wallet) IssueImportTx(
sourceChainID ids.ID,
to *secp256k1fx.OutputOwners,
options ...common.Option,
) (ids.ID, error) {
utx, err := w.builder.NewImportTx(sourceChainID, to, options...)
if err != nil {
return ids.Empty, err
}
return w.IssueUnsignedTx(utx, options...)
}
func (w *wallet) IssueExportTx(
chainID ids.ID,
outputs []*avax.TransferableOutput,
options ...common.Option,
) (ids.ID, error) {
utx, err := w.builder.NewExportTx(chainID, outputs, options...)
if err != nil {
return ids.Empty, err
}
return w.IssueUnsignedTx(utx, options...)
}
func (w *wallet) IssueTransformSubnetTx(
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,
) (ids.ID, error) {
utx, err := w.builder.NewTransformSubnetTx(
subnetID,
assetID,
initialSupply,
maxSupply,
minConsumptionRate,
maxConsumptionRate,
minValidatorStake,
maxValidatorStake,
minStakeDuration,
maxStakeDuration,
minDelegationFee,
minDelegatorStake,
maxValidatorWeightFactor,
uptimeRequirement,
options...,
)
if err != nil {
return ids.Empty, err
}
return w.IssueUnsignedTx(utx, options...)
}
func (w *wallet) IssueAddPermissionlessValidatorTx(
vdr *txs.SubnetValidator,
signer signer.Signer,
assetID ids.ID,
validationRewardsOwner *secp256k1fx.OutputOwners,
delegationRewardsOwner *secp256k1fx.OutputOwners,
shares uint32,
options ...common.Option,
) (ids.ID, error) {
utx, err := w.builder.NewAddPermissionlessValidatorTx(
vdr,
signer,
assetID,
validationRewardsOwner,
delegationRewardsOwner,
shares,
options...,
)
if err != nil {
return ids.Empty, err
}
return w.IssueUnsignedTx(utx, options...)
}
func (w *wallet) IssueAddPermissionlessDelegatorTx(
vdr *txs.SubnetValidator,
assetID ids.ID,
rewardsOwner *secp256k1fx.OutputOwners,
options ...common.Option,
) (ids.ID, error) {
utx, err := w.builder.NewAddPermissionlessDelegatorTx(
vdr,
assetID,
rewardsOwner,
options...,
)
if err != nil {
return ids.Empty, err
}
return w.IssueUnsignedTx(utx, options...)
}
func (w *wallet) IssueUnsignedTx(
utx txs.UnsignedTx,
options ...common.Option,
) (ids.ID, error) {
ops := common.NewOptions(options)
ctx := ops.Context()
tx, err := w.signer.SignUnsigned(ctx, utx)
if err != nil {
return ids.Empty, err
}
return w.IssueTx(tx, options...)
}
func (w *wallet) IssueTx(
tx *txs.Tx,
options ...common.Option,
) (ids.ID, error) {
ops := common.NewOptions(options)
ctx := ops.Context()
txID, err := w.client.IssueTx(ctx, tx.Bytes())
if err != nil {
return ids.Empty, err
}
if ops.AssumeDecided() {
return txID, w.Backend.AcceptTx(ctx, tx)
}
txStatus, err := w.client.AwaitTxDecided(ctx, txID, ops.PollFrequency())
if err != nil {
return txID, err
}
if err := w.Backend.AcceptTx(ctx, tx); err != nil {
return txID, err
}
if txStatus.Status != status.Committed {
return txID, errNotCommitted
}
return txID, nil
}