-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathFlowStakingCollection.cdc
1277 lines (1032 loc) · 63 KB
/
FlowStakingCollection.cdc
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
/*
FlowStakingCollection
This contract defines a collection for staking and delegating objects
which allows users to stake and delegate for as many nodes as they want in a single account.
It is compatible with the locked token account setup.
See the onflow/flow-core-contracts README for more high level information about the staking collection.
*/
import "FungibleToken"
import "FlowToken"
import "FlowIDTableStaking"
import "LockedTokens"
import "FlowStorageFees"
import "FlowClusterQC"
import "FlowDKG"
import "FlowEpoch"
import "Burner"
access(all) contract FlowStakingCollection {
/// Account paths
access(all) let StakingCollectionStoragePath: StoragePath
access(all) let StakingCollectionPrivatePath: PrivatePath
access(all) let StakingCollectionPublicPath: PublicPath
/// Events
access(all) event NodeAddedToStakingCollection(nodeID: String, role: UInt8, amountCommitted: UFix64, address: Address?)
access(all) event DelegatorAddedToStakingCollection(nodeID: String, delegatorID: UInt32, amountCommitted: UFix64, address: Address?)
access(all) event NodeRemovedFromStakingCollection(nodeID: String, role: UInt8, address: Address?)
access(all) event DelegatorRemovedFromStakingCollection(nodeID: String, delegatorID: UInt32, address: Address?)
access(all) event MachineAccountCreated(nodeID: String, role: UInt8, address: Address)
/// Struct that stores delegator ID info
access(all) struct DelegatorIDs {
access(all) let delegatorNodeID: String
access(all) let delegatorID: UInt32
view init(nodeID: String, delegatorID: UInt32) {
self.delegatorNodeID = nodeID
self.delegatorID = delegatorID
}
}
/// Contains information about a node's machine Account
/// which is a secondary account that is only meant to hold
/// the QC or DKG object and FLOW to automatically pay for transaction fees
/// related to QC or DKG operations.
access(all) struct MachineAccountInfo {
access(all) let nodeID: String
access(all) let role: UInt8
// Capability to the FLOW Vault to allow the owner
// to withdraw or deposit to their machine account if needed
access(contract) let machineAccountVaultProvider: Capability<auth(FungibleToken.Withdraw) &FlowToken.Vault>
init(nodeID: String, role: UInt8, machineAccountVaultProvider: Capability<auth(FungibleToken.Withdraw) &FlowToken.Vault>) {
pre {
machineAccountVaultProvider.check():
"FlowStakingCollection.MachineAccountInfo.init: Invalid Flow Token Vault Provider."
}
self.nodeID = nodeID
self.role = role
self.machineAccountVaultProvider = machineAccountVaultProvider
}
// Gets the address of the machine account
access(all) view fun getAddress(): Address {
return self.machineAccountVaultProvider.borrow()!.owner!.address
}
}
/// Public interface that users can publish for their staking collection
/// so that others can query their staking info
access(all) resource interface StakingCollectionPublic {
access(all) var lockedTokensUsed: UFix64
access(all) var unlockedTokensUsed: UFix64
access(all) fun addNodeObject(_ node: @FlowIDTableStaking.NodeStaker, machineAccountInfo: MachineAccountInfo?)
access(all) fun addDelegatorObject(_ delegator: @FlowIDTableStaking.NodeDelegator)
access(all) view fun doesStakeExist(nodeID: String, delegatorID: UInt32?): Bool
access(all) fun getNodeIDs(): [String]
access(all) fun getDelegatorIDs(): [DelegatorIDs]
access(all) fun getAllNodeInfo(): [FlowIDTableStaking.NodeInfo]
access(all) fun getAllDelegatorInfo(): [FlowIDTableStaking.DelegatorInfo]
access(all) fun getMachineAccounts(): {String: MachineAccountInfo}
}
access(all) entitlement CollectionOwner
/// The resource that stakers store in their accounts to store
/// all their staking objects and capability to the locked account object
/// Keeps track of how many locked and unlocked tokens are staked
/// so it knows which tokens to give to the user when they deposit and withdraw
/// different types of tokens
///
/// WARNING: If you destroy a staking collection with the `destroy` command,
/// you will lose access to all your nodes and delegators that the staking collection
/// manages. If you want to destroy it, you must either transfer your node to a different account
/// unstake all your tokens and withdraw
/// your unstaked tokens and rewards first before destroying.
/// Then use the `destroyStakingCollection` method to destroy it
access(all) resource StakingCollection: StakingCollectionPublic, Burner.Burnable {
/// unlocked vault
access(self) var unlockedVault: Capability<auth(FungibleToken.Withdraw) &FlowToken.Vault>
/// locked vault
/// will be nil if the account has no corresponding locked account
access(self) var lockedVault: Capability<auth(FungibleToken.Withdraw) &FlowToken.Vault>?
/// Stores staking objects for nodes and delegators
/// Can only use one delegator per node ID
/// need to be private for now because they could be using locked tokens
access(self) var nodeStakers: @{String: FlowIDTableStaking.NodeStaker}
access(self) var nodeDelegators: @{String: FlowIDTableStaking.NodeDelegator}
/// Capabilty to the TokenHolder object in the unlocked account
/// Accounts without a locked account will not store this, it will be nil
access(self) var tokenHolder: Capability<auth(FungibleToken.Withdraw, LockedTokens.TokenOperations) &LockedTokens.TokenHolder>?
/// Tracks how many locked and unlocked tokens the staker is using for all their nodes and/or delegators
/// When committing new tokens, locked tokens are used first, followed by unlocked tokens
/// When withdrawing tokens, unlocked tokens are withdrawn first, followed by locked tokens
access(all) var lockedTokensUsed: UFix64
access(all) var unlockedTokensUsed: UFix64
/// Tracks the machine accounts associated with nodes
access(self) var machineAccounts: {String: MachineAccountInfo}
init(
unlockedVault: Capability<auth(FungibleToken.Withdraw) &FlowToken.Vault>,
tokenHolder: Capability<auth(FungibleToken.Withdraw, LockedTokens.TokenOperations) &LockedTokens.TokenHolder>?
) {
pre {
unlockedVault.check():
"FlowStakingCollection.StakingCollection.init: Cannot Initialize a Staking Collection! "
.concat("The provided FlowToken Vault capability with withdraw entitlements is invalid.")
}
self.unlockedVault = unlockedVault
self.nodeStakers <- {}
self.nodeDelegators <- {}
self.lockedTokensUsed = 0.0
self.unlockedTokensUsed = 0.0
// If the account has a locked account, initialize its token holder
// and locked vault capability
if let tokenHolderObj = tokenHolder {
self.tokenHolder = tokenHolder
// borrow the main token manager object from the locked account
// to get access to the locked vault capability
let lockedTokenManager = tokenHolderObj.borrow()!.borrowTokenManager()
self.lockedVault = lockedTokenManager.vault
} else {
self.tokenHolder = nil
self.lockedVault = nil
}
self.machineAccounts = {}
}
/// Gets a standard error message to show when the requested staker
/// is not controlled by the staking collection
///
/// @param nodeID: The ID of the requested node
/// @param delegatorID: The ID of the requested delegator
///
/// @return String: The full error message to print
access(all) view fun getStakerDoesntExistInCollectionError(funcName: String, nodeID: String, delegatorID: UInt32?): String {
// Construct the function name for the beginning of the error
let errorBeginning = "FlowStakingCollection.StakingCollection.".concat(funcName).concat(": ")
// The error message is different if it is a delegator vs a node
if let delegator = delegatorID {
return errorBeginning.concat("The specified delegator with node ID ")
.concat(nodeID).concat(" and delegatorID ").concat(delegator.toString())
.concat(" does not exist in the owner's collection. ")
.concat("Make sure that the IDs you entered correspond to a delegator that is controlled by this staking collection.")
} else {
return errorBeginning.concat("The specified node with ID ")
.concat(nodeID).concat(" does not exist in the owner's collection. ")
.concat("Make sure that the ID you entered corresponds to a node that is controlled by this staking collection.")
}
}
/// Called when the collection is destroyed via `Burner.burn()`
access(contract) fun burnCallback() {
let nodeIDs = self.getNodeIDs()
let delegatorIDs = self.getDelegatorIDs()
for nodeID in nodeIDs {
self.closeStake(nodeID: nodeID, delegatorID: nil)
}
for delegatorID in delegatorIDs {
self.closeStake(nodeID: delegatorID.delegatorNodeID, delegatorID: delegatorID.delegatorID)
}
}
/// Called when committing tokens for staking. Gets tokens from either or both vaults
/// Uses locked tokens first, then unlocked if any more are still needed
access(self) fun getTokens(amount: UFix64): @{FungibleToken.Vault} {
let unlockedVault = self.unlockedVault.borrow()!
let unlockedBalance = unlockedVault.balance - FlowStorageFees.minimumStorageReservation
// If there is a locked account, use the locked vault first
if self.lockedVault != nil {
let lockedVault = self.lockedVault!.borrow()!
let lockedBalance = lockedVault.balance - FlowStorageFees.minimumStorageReservation
assert(
amount <= lockedBalance + unlockedBalance,
message: "FlowStakingCollection.StakingCollection.getTokens: Cannot get tokens to stake! "
.concat("The amount of FLOW requested to use, ")
.concat(amount.toString()).concat(", is more than the sum of ")
.concat("locked and unlocked FLOW, ").concat((lockedBalance+unlockedBalance).toString())
.concat(", in the owner's accounts.")
)
// If all the tokens can be removed from locked, withdraw and return them
if (amount <= lockedBalance) {
self.lockedTokensUsed = self.lockedTokensUsed + amount
return <-lockedVault.withdraw(amount: amount)
// If not all can be removed from locked, remove what can be, then remove the rest from unlocked
} else {
// update locked tokens used record by adding the rest of the locked balance
self.lockedTokensUsed = self.lockedTokensUsed + lockedBalance
let numUnlockedTokensToUse = amount - lockedBalance
// Update the unlocked tokens used record by adding the amount requested
// minus whatever was used from the locked tokens
self.unlockedTokensUsed = self.unlockedTokensUsed + numUnlockedTokensToUse
let tokens <- FlowToken.createEmptyVault(vaultType: Type<@FlowToken.Vault>())
// Get the actual tokens from each vault
let lockedPortion <- lockedVault.withdraw(amount: lockedBalance)
let unlockedPortion <- unlockedVault.withdraw(amount: numUnlockedTokensToUse)
// Deposit them into the same vault
tokens.deposit(from: <-lockedPortion)
tokens.deposit(from: <-unlockedPortion)
return <-tokens
}
} else {
// Since there is no locked account, all tokens have to come from the normal unlocked balance
assert(
amount <= unlockedBalance,
message: "FlowStakingCollection.StakingCollection.getTokens: Cannot get tokens to stake! "
.concat("The amount of FLOW requested to use, ")
.concat(amount.toString()).concat(", is more than the amount of FLOW, ")
.concat((unlockedBalance).toString())
.concat(", in the owner's account.")
)
self.unlockedTokensUsed = self.unlockedTokensUsed + amount
return <-unlockedVault.withdraw(amount: amount)
}
}
/// Deposits tokens back to a vault after being withdrawn from a Stake or Delegation.
/// Deposits to unlocked tokens first, if possible, followed by locked tokens
access(self) fun depositTokens(from: @{FungibleToken.Vault}) {
pre {
// This error should never be triggered in production becasue the tokens used fields
// should be properly managed by all the other functions
from.balance <= self.unlockedTokensUsed + self.lockedTokensUsed:
"FlowStakingCollection.StakingCollection.depositTokens: "
.concat(" Cannot return more FLOW to the account than is already in use for staking.")
}
let unlockedVault = self.unlockedVault.borrow()!
/// If there is a locked account, get the locked vault holder for depositing
if self.lockedVault != nil {
if (from.balance <= self.unlockedTokensUsed) {
self.unlockedTokensUsed = self.unlockedTokensUsed - from.balance
unlockedVault.deposit(from: <-from)
} else {
// Return unlocked tokens first
unlockedVault.deposit(from: <-from.withdraw(amount: self.unlockedTokensUsed))
self.unlockedTokensUsed = 0.0
self.lockedTokensUsed = self.lockedTokensUsed - from.balance
// followed by returning the difference as locked tokens
self.lockedVault!.borrow()!.deposit(from: <-from)
}
} else {
self.unlockedTokensUsed = self.unlockedTokensUsed - from.balance
// If there is no locked account, get the users vault capability and deposit tokens to it.
unlockedVault.deposit(from: <-from)
}
}
/// Returns true if a Stake or Delegation record exists in the StakingCollection for a given nodeID and optional delegatorID, otherwise false.
access(all) view fun doesStakeExist(nodeID: String, delegatorID: UInt32?): Bool {
var tokenHolderNodeID: String? = nil
var tokenHolderDelegatorNodeID: String? = nil
var tokenHolderDelegatorID: UInt32? = nil
// If there is a locked account, get the staking info from that account
if self.tokenHolder != nil {
if let _tokenHolder = self.tokenHolder!.borrow() {
tokenHolderNodeID = _tokenHolder.getNodeID()
tokenHolderDelegatorNodeID = _tokenHolder.getDelegatorNodeID()
tokenHolderDelegatorID = _tokenHolder.getDelegatorID()
}
}
// If the request is for a delegator, check all possible delegators for possible matches
if let delegatorID = delegatorID {
if (tokenHolderDelegatorNodeID != nil
&& tokenHolderDelegatorID != nil
&& tokenHolderDelegatorNodeID! == nodeID
&& tokenHolderDelegatorID! == delegatorID)
{
return true
}
// Look for a delegator with the specified node ID and delegator ID
return self.borrowDelegator(nodeID: nodeID, delegatorID: delegatorID) != nil
} else {
if (tokenHolderNodeID != nil && tokenHolderNodeID! == nodeID) {
return true
}
return self.borrowNode(nodeID) != nil
}
}
/// Function to add an existing NodeStaker object
access(all) fun addNodeObject(_ node: @FlowIDTableStaking.NodeStaker, machineAccountInfo: MachineAccountInfo?) {
let id = node.id
let stakingInfo = FlowIDTableStaking.NodeInfo(nodeID: id)
let totalStaked = stakingInfo.totalTokensInRecord() - stakingInfo.tokensRewarded
self.unlockedTokensUsed = self.unlockedTokensUsed + totalStaked
emit NodeAddedToStakingCollection(
nodeID: stakingInfo.id,
role: stakingInfo.role,
amountCommitted: stakingInfo.totalCommittedWithoutDelegators(),
address: self.owner?.address
)
self.nodeStakers[id] <-! node
// Set the machine account for the existing node
// can be the same as the old account if needed
self.machineAccounts[id] = machineAccountInfo
}
/// Function to add an existing NodeDelegator object
access(all) fun addDelegatorObject(_ delegator: @FlowIDTableStaking.NodeDelegator) {
let stakingInfo = FlowIDTableStaking.DelegatorInfo(nodeID: delegator.nodeID, delegatorID: delegator.id)
let totalStaked = stakingInfo.totalTokensInRecord() - stakingInfo.tokensRewarded
self.unlockedTokensUsed = self.unlockedTokensUsed + totalStaked
emit DelegatorAddedToStakingCollection(
nodeID: stakingInfo.nodeID,
delegatorID: stakingInfo.id,
amountCommitted: stakingInfo.tokensStaked + stakingInfo.tokensCommitted - stakingInfo.tokensRequestedToUnstake,
address: self.owner?.address
)
self.nodeDelegators[delegator.nodeID] <-! delegator
}
/// Function to remove an existing NodeStaker object.
/// If the user has used any locked tokens, removing NodeStaker objects is not allowed.
/// We do not clear the machine account field for this node here
/// because the operator may want to keep it the same
access(CollectionOwner) fun removeNode(nodeID: String): @FlowIDTableStaking.NodeStaker? {
pre {
self.doesStakeExist(nodeID: nodeID, delegatorID: nil):
self.getStakerDoesntExistInCollectionError(funcName: "removeNode", nodeID: nodeID, delegatorID: nil)
self.lockedTokensUsed == UFix64(0.0):
"FlowStakingCollection.StakingCollection.removeNode: Cannot remove a node from the collection "
.concat("because the collection still manages ").concat(self.lockedTokensUsed.toString())
.concat(" locked tokens. This is to prevent locked tokens ")
.concat("from being unlocked and withdrawn before their allotted unlocking time.")
}
if self.nodeStakers[nodeID] != nil {
let stakingInfo = FlowIDTableStaking.NodeInfo(nodeID: nodeID)
let totalStaked = stakingInfo.totalTokensInRecord() - stakingInfo.tokensRewarded
// Since the NodeStaker object is being removed, the total number of unlocked tokens staked to it is deducted from the counter.
self.unlockedTokensUsed = self.unlockedTokensUsed - totalStaked
// Removes the NodeStaker object from the Staking Collections internal nodeStakers map.
let nodeStaker <- self.nodeStakers[nodeID] <- nil
// Clear the machine account info from the record
self.machineAccounts[nodeID] = nil
emit NodeRemovedFromStakingCollection(nodeID: nodeID, role: stakingInfo.role, address: self.owner?.address)
return <- nodeStaker
} else {
// The function does not allow for removing a NodeStaker stored in the locked account, if one exists.
panic("Cannot remove node stored in locked account.")
}
}
/// Function to remove an existing NodeDelegator object.
/// If the user has used any locked tokens, removing NodeDelegator objects is not allowed.
access(CollectionOwner) fun removeDelegator(nodeID: String, delegatorID: UInt32): @FlowIDTableStaking.NodeDelegator? {
pre {
self.doesStakeExist(nodeID: nodeID, delegatorID: delegatorID):
self.getStakerDoesntExistInCollectionError(funcName: "removeDelegator", nodeID: nodeID, delegatorID: delegatorID)
self.lockedTokensUsed == UFix64(0.0):
"FlowStakingCollection.StakingCollection.removeDelegator: Cannot remove a delegator from the collection "
.concat("because the collection still manages ").concat(self.lockedTokensUsed.toString())
.concat(" locked tokens. This is to prevent locked tokens ")
.concat("from being unlocked and withdrawn before their allotted unlocking time.")
}
if self.nodeDelegators[nodeID] != nil {
let delegatorRef = (&self.nodeDelegators[nodeID] as &FlowIDTableStaking.NodeDelegator?)!
if delegatorRef.id == delegatorID {
let stakingInfo = FlowIDTableStaking.DelegatorInfo(nodeID: nodeID, delegatorID: delegatorID)
let totalStaked = stakingInfo.totalTokensInRecord() - stakingInfo.tokensRewarded
// Since the NodeDelegator object is being removed, the total number of unlocked tokens delegated to it is deducted from the counter.
self.unlockedTokensUsed = self.unlockedTokensUsed - totalStaked
// Removes the NodeDelegator object from the Staking Collections internal nodeDelegators map.
let nodeDelegator <- self.nodeDelegators[nodeID] <- nil
emit DelegatorRemovedFromStakingCollection(
nodeID: nodeID,
delegatorID: delegatorID,
address: self.owner?.address
)
return <- nodeDelegator
} else {
panic("FlowStakingCollection.StakingCollection.removeDelegator: "
.concat("Expected delegatorID ").concat(delegatorID.toString())
.concat(" does not correspond to the Staking Collection's delegator ID ")
.concat(delegatorRef.id.toString()))
}
} else {
// The function does not allow for removing a NodeDelegator stored in the locked account, if one exists.
panic("FlowStakingCollection.StakingCollection.removeDelegator: "
.concat("Cannot remove a delegator with ID ").concat(delegatorID.toString())
.concat(" because it is stored in the locked account."))
}
}
/// Operations to register new staking objects
/// Function to register a new Staking Record to the Staking Collection
access(CollectionOwner) fun registerNode(
id: String,
role: UInt8,
networkingAddress: String,
networkingKey: String,
stakingKey: String,
amount: UFix64,
payer: auth(BorrowValue) &Account
): auth(Storage, Capabilities, Contracts, Keys, Inbox) &Account? {
let tokens <- self.getTokens(amount: amount)
let nodeStaker <- FlowIDTableStaking.addNodeRecord(
id: id,
role: role,
networkingAddress: networkingAddress,
networkingKey: networkingKey,
stakingKey: stakingKey,
tokensCommitted: <-tokens
)
emit NodeAddedToStakingCollection(
nodeID: nodeStaker.id,
role: role,
amountCommitted: amount,
address: self.owner?.address
)
self.nodeStakers[id] <-! nodeStaker
let nodeReference = self.borrowNode(id)
?? panic("FlowStakingCollection.StakingCollection.registerNode: "
.concat("Could not borrow a reference to the newly created node with ID ")
.concat(id).concat("."))
let nodeInfo = FlowIDTableStaking.NodeInfo(nodeID: nodeReference.id)
// Register the machine account for the node
// creates an auth account object and returns it to the caller
if nodeInfo.role == FlowEpoch.NodeRole.Collector.rawValue || nodeInfo.role == FlowEpoch.NodeRole.Consensus.rawValue {
return self.registerMachineAccount(nodeReference: nodeReference, payer: payer)
} else {
return nil
}
}
/// Registers the secondary machine account for a node
/// to store their epoch-related objects
/// Only returns an AuthAccount object if the node is collector or consensus, otherwise returns nil
/// The caller's qc or dkg object is stored in the new account
/// but it is the caller's responsibility to add public keys to it
access(self) fun registerMachineAccount(
nodeReference: &FlowIDTableStaking.NodeStaker,
payer: auth(BorrowValue) &Account
): auth(Storage, Capabilities, Contracts, Keys, Inbox) &Account? {
let nodeInfo = FlowIDTableStaking.NodeInfo(nodeID: nodeReference.id)
// Create the new account
let machineAcct = Account(payer: payer)
// Get the vault capability and create the machineAccountInfo struct
let machineAccountVaultProvider = machineAcct.capabilities.storage
.issue<auth(FungibleToken.Withdraw) &FlowToken.Vault>(/storage/flowTokenVault)!
let machineAccountInfo = MachineAccountInfo(
nodeID: nodeInfo.id,
role: nodeInfo.role,
machineAccountVaultProvider: machineAccountVaultProvider
)
// If they are a collector node, create a QC Voter object and store it in the account
if nodeInfo.role == FlowEpoch.NodeRole.Collector.rawValue {
// Get the voter object and store it
let qcVoter <- FlowEpoch.getClusterQCVoter(nodeStaker: nodeReference)
machineAcct.storage.save(<-qcVoter, to: FlowClusterQC.VoterStoragePath)
// set this node's machine account
self.machineAccounts[nodeInfo.id] = machineAccountInfo
emit MachineAccountCreated(
nodeID: nodeInfo.id,
role: FlowEpoch.NodeRole.Collector.rawValue,
address: machineAccountVaultProvider.borrow()!.owner!.address
)
return machineAcct
// If they are a consensus node, create a DKG Participant object and store it in the account
} else if nodeInfo.role == FlowEpoch.NodeRole.Consensus.rawValue {
// get the participant object and store it
let dkgParticipant <- FlowEpoch.getDKGParticipant(nodeStaker: nodeReference)
machineAcct.storage.save(<-dkgParticipant, to: FlowDKG.ParticipantStoragePath)
// set this node's machine account
self.machineAccounts[nodeInfo.id] = machineAccountInfo
emit MachineAccountCreated(
nodeID: nodeInfo.id,
role: FlowEpoch.NodeRole.Consensus.rawValue,
address: machineAccountVaultProvider.borrow()!.owner!.address
)
return machineAcct
}
return nil
}
/// Allows the owner to set the machine account for one of their nodes
/// This is used if the owner decides to transfer the machine account resource to another account
/// without also transferring the old machine account record,
/// or if they decide they want to use a different machine account for one of their nodes
/// If they want to use a different machine account, it is their responsibility to
/// transfer the qc or dkg object to the new account
access(all) fun addMachineAccountRecord(
nodeID: String,
machineAccount: auth(BorrowValue, StorageCapabilities) &Account
) {
pre {
self.doesStakeExist(nodeID: nodeID, delegatorID: nil):
self.getStakerDoesntExistInCollectionError(funcName: "addMachineAccountRecord", nodeID: nodeID, delegatorID: nil)
}
let nodeInfo = FlowIDTableStaking.NodeInfo(nodeID: nodeID)
// Make sure that the QC or DKG object in the machine account is correct for this node ID
if nodeInfo.role == FlowEpoch.NodeRole.Collector.rawValue {
let qcVoterRef = machineAccount.storage.borrow<&FlowClusterQC.Voter>(from: FlowClusterQC.VoterStoragePath)
?? panic("FlowStakingCollection.StakingCollection.addMachineAccountRecord: "
.concat("Could not access a QC Voter object from the provided machine account with address ").concat(machineAccount.address.toString()))
assert(
nodeID == qcVoterRef.nodeID,
message: "FlowStakingCollection.StakingCollection.addMachineAccountRecord: "
.concat("The QC Voter Object in the machine account with node ID ").concat(qcVoterRef.nodeID)
.concat(" does not match the Staking Collection's specified node ID ").concat(nodeID)
)
} else if nodeInfo.role == FlowEpoch.NodeRole.Consensus.rawValue {
let dkgParticipantRef = machineAccount.storage.borrow<&FlowDKG.Participant>(from: FlowDKG.ParticipantStoragePath)
?? panic("FlowStakingCollection.StakingCollection.addMachineAccountRecord: "
.concat("Could not access a DKG Participant object from the provided machine account with address ").concat(machineAccount.address.toString()))
assert(
nodeID == dkgParticipantRef.nodeID,
message: "FlowStakingCollection.StakingCollection.addMachineAccountRecord: "
.concat("The DKG Participant Object in the machine account with node ID ").concat(dkgParticipantRef.nodeID)
.concat(" does not match the Staking Collection's specified node ID ").concat(nodeID)
)
}
// Make sure that the vault capability is created
let machineAccountVaultProvider = machineAccount.capabilities.storage
.issue<auth(FungibleToken.Withdraw) &FlowToken.Vault>(/storage/flowTokenVault)!
// Create the new Machine account info object and store it
let machineAccountInfo = MachineAccountInfo(
nodeID: nodeID,
role: nodeInfo.role,
machineAccountVaultProvider: machineAccountVaultProvider
)
self.machineAccounts[nodeID] = machineAccountInfo
}
/// If a user has created a node before epochs were enabled, they'll need to use this function
/// to create their machine account with their node
access(CollectionOwner) fun createMachineAccountForExistingNode(nodeID: String, payer: auth(BorrowValue) &Account): auth(Storage, Capabilities, Contracts, Keys, Inbox) &Account? {
pre {
self.doesStakeExist(nodeID: nodeID, delegatorID: nil):
self.getStakerDoesntExistInCollectionError(funcName: "createMachineAccountForExistingNode", nodeID: nodeID, delegatorID: nil)
}
let nodeInfo = FlowIDTableStaking.NodeInfo(nodeID: nodeID)
if let nodeReference = self.borrowNode(nodeID) {
return self.registerMachineAccount(nodeReference: nodeReference, payer: payer)
} else {
if let tokenHolderObj = self.tokenHolder {
// borrow the main token manager object from the locked account
// to get access to the locked vault capability
let lockedTokenManager = tokenHolderObj.borrow()!.borrowTokenManager()
let lockedNodeReference = lockedTokenManager.borrowNode()
?? panic("FlowStakingCollection.StakingCollection.createMachineAccountForExistingNode: "
.concat("Could not borrow a node reference from the locked account."))
return self.registerMachineAccount(nodeReference: lockedNodeReference, payer: payer)
}
}
return nil
}
/// Allows the owner to withdraw any available FLOW from their machine account
access(CollectionOwner) fun withdrawFromMachineAccount(nodeID: String, amount: UFix64) {
pre {
self.doesStakeExist(nodeID: nodeID, delegatorID: nil):
self.getStakerDoesntExistInCollectionError(funcName: "withdrawFromMachineAccount", nodeID: nodeID, delegatorID: nil)
}
if let machineAccountInfo = self.machineAccounts[nodeID] {
let vaultRef = machineAccountInfo.machineAccountVaultProvider.borrow()
?? panic("FlowStakingCollection.StakingCollection.withdrawFromMachineAccount: "
.concat("Could not borrow reference to machine account vault."))
let tokens <- vaultRef.withdraw(amount: amount)
let unlockedVault = self.unlockedVault.borrow()!
unlockedVault.deposit(from: <-tokens)
} else {
panic("FlowStakingCollection.StakingCollection.withdrawFromMachineAccount: "
.concat("Could not find a machine account for the specified node ID ")
.concat(nodeID).concat("."))
}
}
/// Function to register a new Delegator Record to the Staking Collection
access(CollectionOwner) fun registerDelegator(nodeID: String, amount: UFix64) {
let delegatorIDs = self.getDelegatorIDs()
for idInfo in delegatorIDs {
if idInfo.delegatorNodeID == nodeID {
panic("FlowStakingCollection.StakingCollection.registerDelegator: "
.concat("Cannot register a delegator for node ").concat(nodeID)
.concat(" because that node is already being delegated to from this Staking Collection."))
}
}
let tokens <- self.getTokens(amount: amount)
let nodeDelegator <- FlowIDTableStaking.registerNewDelegator(nodeID: nodeID, tokensCommitted: <-tokens)
emit DelegatorAddedToStakingCollection(
nodeID: nodeDelegator.nodeID,
delegatorID: nodeDelegator.id,
amountCommitted: amount,
address: self.owner?.address
)
self.nodeDelegators[nodeDelegator.nodeID] <-! nodeDelegator
}
/// Borrows a reference to a node in the collection
access(self) view fun borrowNode(_ nodeID: String): auth(FlowIDTableStaking.NodeOperator) &FlowIDTableStaking.NodeStaker? {
if self.nodeStakers[nodeID] != nil {
return &self.nodeStakers[nodeID] as auth(FlowIDTableStaking.NodeOperator) &FlowIDTableStaking.NodeStaker?
} else {
return nil
}
}
/// Borrows a reference to a delegator in the collection
access(self) view fun borrowDelegator(nodeID: String, delegatorID: UInt32): auth(FlowIDTableStaking.DelegatorOwner) &FlowIDTableStaking.NodeDelegator? {
if self.nodeDelegators[nodeID] != nil {
let delegatorRef = (&self.nodeDelegators[nodeID] as auth(FlowIDTableStaking.DelegatorOwner) &FlowIDTableStaking.NodeDelegator?)!
if delegatorRef.id == delegatorID { return delegatorRef } else { return nil }
} else {
return nil
}
}
// Staking Operations
// The owner calls the same function whether or not they are staking for a node or delegating.
// If they are staking for a node, they provide their node ID and `nil` as the delegator ID
// If they are staking for a delegator, they provide the node ID for the node they are delegating to
// and their delegator ID to specify that it is for their delegator object
/// Updates the stored networking address for the specified node
access(CollectionOwner) fun updateNetworkingAddress(nodeID: String, newAddress: String) {
pre {
self.doesStakeExist(nodeID: nodeID, delegatorID: nil):
self.getStakerDoesntExistInCollectionError(funcName: "updateNetworkingAddress", nodeID: nodeID, delegatorID: nil)
}
// If the node is stored in the collection, borrow it
if let node = self.borrowNode(nodeID) {
node.updateNetworkingAddress(newAddress)
} else {
// Use the node stored in the locked account
let node = self.tokenHolder!.borrow()!.borrowStaker()
node.updateNetworkingAddress(newAddress)
}
}
/// Function to stake new tokens for an existing Stake or Delegation record in the StakingCollection
access(CollectionOwner) fun stakeNewTokens(nodeID: String, delegatorID: UInt32?, amount: UFix64) {
pre {
self.doesStakeExist(nodeID: nodeID, delegatorID: delegatorID):
self.getStakerDoesntExistInCollectionError(funcName: "stakeNewTokens", nodeID: nodeID, delegatorID: delegatorID)
}
// If staking as a delegator, use the delegate functionality
if let delegatorID = delegatorID {
// If the delegator is stored in the collection, borrow it
if let delegator = self.borrowDelegator(nodeID: nodeID, delegatorID: delegatorID) {
delegator.delegateNewTokens(from: <- self.getTokens(amount: amount))
} else {
let tokenHolder = self.tokenHolder!.borrow()!
// Get any needed unlocked tokens, and deposit them to the locked vault.
let lockedBalance = self.lockedVault!.borrow()!.balance - FlowStorageFees.minimumStorageReservation
if (amount > lockedBalance) {
let numUnlockedTokensToUse = amount - lockedBalance
tokenHolder.deposit(from: <- self.unlockedVault.borrow()!.withdraw(amount: numUnlockedTokensToUse))
}
// Use the delegator stored in the locked account
let delegator = tokenHolder.borrowDelegator()
delegator.delegateNewTokens(amount: amount)
}
// If the node is stored in the collection, borrow it
} else if let node = self.borrowNode(nodeID) {
node.stakeNewTokens(<-self.getTokens(amount: amount))
} else {
// Get any needed unlocked tokens, and deposit them to the locked vault.
let lockedBalance = self.lockedVault!.borrow()!.balance - FlowStorageFees.minimumStorageReservation
if (amount > lockedBalance) {
let numUnlockedTokensToUse = amount - lockedBalance
self.tokenHolder!.borrow()!.deposit(from: <- self.unlockedVault.borrow()!.withdraw(amount: numUnlockedTokensToUse))
}
// Use the staker stored in the locked account
let staker = self.tokenHolder!.borrow()!.borrowStaker()
staker.stakeNewTokens(amount: amount)
}
}
/// Function to stake unstaked tokens for an existing Stake or Delegation record in the StakingCollection
access(CollectionOwner) fun stakeUnstakedTokens(nodeID: String, delegatorID: UInt32?, amount: UFix64) {
pre {
self.doesStakeExist(nodeID: nodeID, delegatorID: delegatorID):
self.getStakerDoesntExistInCollectionError(funcName: "stakeUnstakedTokens", nodeID: nodeID, delegatorID: delegatorID)
}
if let delegatorID = delegatorID {
if let delegator = self.borrowDelegator(nodeID: nodeID, delegatorID: delegatorID) {
delegator.delegateUnstakedTokens(amount: amount)
} else {
let delegator = self.tokenHolder!.borrow()!.borrowDelegator()
delegator.delegateUnstakedTokens(amount: amount)
}
} else if let node = self.borrowNode(nodeID) {
node.stakeUnstakedTokens(amount: amount)
} else {
let staker = self.tokenHolder!.borrow()!.borrowStaker()
staker.stakeUnstakedTokens(amount: amount)
}
}
/// Function to stake rewarded tokens for an existing Stake or Delegation record in the StakingCollection
access(CollectionOwner) fun stakeRewardedTokens(nodeID: String, delegatorID: UInt32?, amount: UFix64) {
pre {
self.doesStakeExist(nodeID: nodeID, delegatorID: delegatorID):
self.getStakerDoesntExistInCollectionError(funcName: "stakeRewardedTokens", nodeID: nodeID, delegatorID: delegatorID)
}
if let delegatorID = delegatorID {
if let delegator = self.borrowDelegator(nodeID: nodeID, delegatorID: delegatorID) {
// We add the amount to the unlocked tokens used because rewards are newly minted tokens
// and aren't immediately reflected in the tokens used fields
self.unlockedTokensUsed = self.unlockedTokensUsed + amount
delegator.delegateRewardedTokens(amount: amount)
} else {
// Staking tokens in the locked account staking objects are not reflected in the tokens used fields,
// so they are not updated here
let delegator = self.tokenHolder!.borrow()!.borrowDelegator()
delegator.delegateRewardedTokens(amount: amount)
}
} else if let node = self.borrowNode(nodeID) {
self.unlockedTokensUsed = self.unlockedTokensUsed + amount
node.stakeRewardedTokens(amount: amount)
} else {
let staker = self.tokenHolder!.borrow()!.borrowStaker()
staker.stakeRewardedTokens(amount: amount)
}
}
/// Function to request tokens to be unstaked for an existing Stake or Delegation record in the StakingCollection
access(CollectionOwner) fun requestUnstaking(nodeID: String, delegatorID: UInt32?, amount: UFix64) {
pre {
self.doesStakeExist(nodeID: nodeID, delegatorID: delegatorID):
self.getStakerDoesntExistInCollectionError(funcName: "requestUnstaking", nodeID: nodeID, delegatorID: delegatorID)
}
if let delegatorID = delegatorID {
if let delegator = self.borrowDelegator(nodeID: nodeID, delegatorID: delegatorID) {
delegator.requestUnstaking(amount: amount)
} else {
let delegator = self.tokenHolder!.borrow()!.borrowDelegator()
delegator.requestUnstaking(amount: amount)
}
} else if let node = self.borrowNode(nodeID) {
node.requestUnstaking(amount: amount)
} else {
let staker = self.tokenHolder!.borrow()!.borrowStaker()
staker.requestUnstaking(amount: amount)
}
}
/// Function to unstake all tokens for an existing node staking record in the StakingCollection
/// Only available for node operators
access(CollectionOwner) fun unstakeAll(nodeID: String) {
pre {
self.doesStakeExist(nodeID: nodeID, delegatorID: nil):
self.getStakerDoesntExistInCollectionError(funcName: "unstakeAll", nodeID: nodeID, delegatorID: nil)
}
if let node = self.borrowNode(nodeID) {
node.unstakeAll()
} else {
let staker = self.tokenHolder!.borrow()!.borrowStaker()
staker.unstakeAll()
}
}
/// Function to withdraw unstaked tokens for an existing Stake or Delegation record in the StakingCollection
access(CollectionOwner) fun withdrawUnstakedTokens(nodeID: String, delegatorID: UInt32?, amount: UFix64) {
pre {
self.doesStakeExist(nodeID: nodeID, delegatorID: delegatorID):
self.getStakerDoesntExistInCollectionError(funcName: "withdrawUnstakedTokens", nodeID: nodeID, delegatorID: delegatorID)
}
if let delegatorID = delegatorID {
if let delegator = self.borrowDelegator(nodeID: nodeID, delegatorID: delegatorID) {
let tokens <- delegator.withdrawUnstakedTokens(amount: amount)
self.depositTokens(from: <-tokens)
} else {
let delegator = self.tokenHolder!.borrow()!.borrowDelegator()
delegator.withdrawUnstakedTokens(amount: amount)
}
} else if let node = self.borrowNode(nodeID) {
let tokens <- node.withdrawUnstakedTokens(amount: amount)
self.depositTokens(from: <-tokens)
} else {
let staker = self.tokenHolder!.borrow()!.borrowStaker()
staker.withdrawUnstakedTokens(amount: amount)
}
}
/// Function to withdraw rewarded tokens for an existing Stake or Delegation record in the StakingCollection
access(CollectionOwner) fun withdrawRewardedTokens(nodeID: String, delegatorID: UInt32?, amount: UFix64) {
pre {
self.doesStakeExist(nodeID: nodeID, delegatorID: delegatorID):
self.getStakerDoesntExistInCollectionError(funcName: "withdrawRewardedTokens", nodeID: nodeID, delegatorID: delegatorID)
}
if let delegatorID = delegatorID {
if let delegator = self.borrowDelegator(nodeID: nodeID, delegatorID: delegatorID) {
// We update the unlocked tokens used field before withdrawing because
// rewards are newly minted and not immediately reflected in the tokens used fields
self.unlockedTokensUsed = self.unlockedTokensUsed + amount
let tokens <- delegator.withdrawRewardedTokens(amount: amount)
self.depositTokens(from: <-tokens)
} else {
let delegator = self.tokenHolder!.borrow()!.borrowDelegator()
delegator.withdrawRewardedTokens(amount: amount)
// move the unlocked rewards from the locked account to the unlocked account
let unlockedRewards <- self.tokenHolder!.borrow()!.withdraw(amount: amount)
self.unlockedVault.borrow()!.deposit(from: <-unlockedRewards)
}
} else if let node = self.borrowNode(nodeID) {
self.unlockedTokensUsed = self.unlockedTokensUsed + amount
let tokens <- node.withdrawRewardedTokens(amount: amount)
self.depositTokens(from: <-tokens)
} else {
let staker = self.tokenHolder!.borrow()!.borrowStaker()
staker.withdrawRewardedTokens(amount: amount)
// move the unlocked rewards from the locked account to the unlocked account
let unlockedRewards <- self.tokenHolder!.borrow()!.withdraw(amount: amount)
self.unlockedVault.borrow()!.deposit(from: <-unlockedRewards)
}
}
// Closers
/// Closes an existing stake or delegation, moving all withdrawable tokens back to the users account and removing the stake
/// or delegator object from the StakingCollection.
access(CollectionOwner) fun closeStake(nodeID: String, delegatorID: UInt32?) {
pre {
self.doesStakeExist(nodeID: nodeID, delegatorID: delegatorID):
self.getStakerDoesntExistInCollectionError(funcName: "closeStake", nodeID: nodeID, delegatorID: delegatorID)
}
if let delegatorID = delegatorID {
let delegatorInfo = FlowIDTableStaking.DelegatorInfo(nodeID: nodeID, delegatorID: delegatorID)
assert(
delegatorInfo.tokensStaked + delegatorInfo.tokensCommitted + delegatorInfo.tokensUnstaking == 0.0,
message: "FlowStakingCollection.StakingCollection.closeStake: "
.concat("Cannot close a delegation until all tokens have been withdrawn, or moved to a withdrawable state.")
)
if delegatorInfo.tokensUnstaked > 0.0 {
self.withdrawUnstakedTokens(nodeID: nodeID, delegatorID: delegatorID, amount: delegatorInfo.tokensUnstaked)
}
if delegatorInfo.tokensRewarded > 0.0 {
self.withdrawRewardedTokens(nodeID: nodeID, delegatorID: delegatorID, amount: delegatorInfo.tokensRewarded)
}
if let delegator = self.borrowDelegator(nodeID: nodeID, delegatorID: delegatorID) {
let delegator <- self.nodeDelegators[nodeID] <- nil
destroy delegator
} else if let tokenHolderCapability = self.tokenHolder {
let tokenManager = tokenHolderCapability.borrow()!.borrowTokenManager()
let delegator <- tokenManager.removeDelegator()
destroy delegator
} else {
panic("FlowStakingCollection.StakingCollection.closeStake: Token Holder capability needed and not found.")
}
emit DelegatorRemovedFromStakingCollection(nodeID: nodeID, delegatorID: delegatorID, address: self.owner?.address)
} else {