Skip to content

Commit 3a41da7

Browse files
committedJun 26, 2023
Use a separate field for delegator minimum stake requirements
1 parent 450fdf5 commit 3a41da7

File tree

10 files changed

+147
-54
lines changed

10 files changed

+147
-54
lines changed
 

‎contracts/FlowIDTableStaking.cdc

+28-13
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ pub contract FlowIDTableStaking {
6868
pub event NewDelegatorCutPercentage(newCutPercentage: UFix64)
6969
pub event NewWeeklyPayout(newPayout: UFix64)
7070
pub event NewStakingMinimums(newMinimums: {UInt8: UFix64})
71+
pub event NewDelegatorStakingMinimum(newMinimum: UFix64)
7172

7273
/// Holds the identity table for all the nodes in the network.
7374
/// Includes nodes that aren't actively participating
@@ -78,7 +79,6 @@ pub contract FlowIDTableStaking {
7879
/// The minimum amount of tokens that each staker type has to stake
7980
/// in order to be considered valid
8081
/// Keys:
81-
/// 0 - Delegators
8282
/// 1 - Collector Nodes
8383
/// 2 - Consensus Nodes
8484
/// 3 - Execution Nodes
@@ -853,17 +853,25 @@ pub contract FlowIDTableStaking {
853853
/// at the end of the staking auction, and pay rewards to nodes at the end of an epoch
854854
pub resource Admin: EpochOperations {
855855

856-
/// Sets a new set of minimum staking requirements for all the nodes and delegators
857-
/// Delegator minimum is at index 0, other nodes' indexes are their role numbers
856+
/// Sets a new set of minimum staking requirements for all the nodes
857+
/// Nodes' indexes are their role numbers
858858
pub fun setMinimumStakeRequirements(_ newRequirements: {UInt8: UFix64}) {
859859
pre {
860-
newRequirements.keys.length == 6:
861-
"There must be six entries for minimum stake requirements (one for delegators and 5 for nodes)"
860+
newRequirements.keys.length == 5:
861+
"There must be six entries for node minimum stake requirements"
862862
}
863863
FlowIDTableStaking.minimumStakeRequired = newRequirements
864864
emit NewStakingMinimums(newMinimums: newRequirements)
865865
}
866866

867+
/// Sets a new set of minimum staking requirements for all the delegators
868+
pub fun setDelegatorMinimumStakeRequirement(_ newRequirement: UFix64) {
869+
FlowIDTableStaking.account.load<UFix64>(from: /storage/delegatorStakingMinimum)
870+
FlowIDTableStaking.account.save(newRequirement, to: /storage/delegatorStakingMinimum)
871+
872+
emit NewDelegatorStakingMinimum(newMinimum: newRequirement)
873+
}
874+
867875
/// Changes the total weekly payout to a new value
868876
pub fun setEpochTokenPayout(_ newPayout: UFix64) {
869877
if newPayout != FlowIDTableStaking.epochTokenPayout {
@@ -1441,9 +1449,9 @@ pub contract FlowIDTableStaking {
14411449
let delRecord = nodeRecord.borrowDelegatorRecord(delegator)
14421450

14431451
// If the delegator's committed tokens for the next epoch
1444-
// Is less than the delegator minimum, unstake all their tokens
1452+
// is less than the delegator minimum, unstake all their tokens
14451453
let actualCommittedForNextEpoch = delRecord.tokensCommitted.balance + delRecord.tokensStaked.balance - delRecord.tokensRequestedToUnstake
1446-
if !FlowIDTableStaking.isGreaterThanMinimumForRole(numTokens: actualCommittedForNextEpoch, role: UInt8(0)) {
1454+
if actualCommittedForNextEpoch < FlowIDTableStaking.getDelegatorMinimumStakeRequirement() {
14471455
delRecord.tokensUnstaked.deposit(from: <-delRecord.tokensCommitted.withdraw(amount: delRecord.tokensCommitted.balance))
14481456
delRecord.tokensRequestedToUnstake = delRecord.tokensStaked.balance
14491457
}
@@ -1548,9 +1556,9 @@ pub contract FlowIDTableStaking {
15481556
message: "Cannot register a delegator for an access node"
15491557
)
15501558

1551-
let minimum = self.minimumStakeRequired[UInt8(0)]!
1559+
let minimum = self.getDelegatorMinimumStakeRequirement()
15521560
assert(
1553-
self.isGreaterThanMinimumForRole(numTokens: tokensCommitted.balance, role: 0),
1561+
tokensCommitted.balance >= minimum,
15541562
message: "Tokens committed for delegator registration is not above the minimum (".concat(minimum.toString()).concat(")")
15551563
)
15561564

@@ -1810,10 +1818,10 @@ pub contract FlowIDTableStaking {
18101818
}
18111819

18121820
/// Checks if the amount of tokens is greater than the minimum staking requirement
1813-
/// for the specified staker role (including role == 0 for delegators)
1821+
/// for the specified node role
18141822
pub fun isGreaterThanMinimumForRole(numTokens: UFix64, role: UInt8): Bool {
18151823
let minimumStake = self.minimumStakeRequired[role]
1816-
?? panic("Incorrect role provided for minimum stake. Must be 0, 1, 2, 3, 4, or 5")
1824+
?? panic("Incorrect role provided for minimum stake. Must be 1, 2, 3, 4, or 5")
18171825

18181826
return numTokens >= minimumStake
18191827
}
@@ -1851,11 +1859,17 @@ pub contract FlowIDTableStaking {
18511859
?? panic("could not get non-operational node list")
18521860
}
18531861

1854-
/// Gets the minimum stake requirements for all the staker types
1862+
/// Gets the minimum stake requirements for all the node types
18551863
pub fun getMinimumStakeRequirements(): {UInt8: UFix64} {
18561864
return self.minimumStakeRequired
18571865
}
18581866

1867+
/// Gets the minimum stake requirement for delegators
1868+
pub fun getDelegatorMinimumStakeRequirement(): UFix64 {
1869+
return self.account.copy<UFix64>(from: /storage/delegatorStakingMinimum)
1870+
?? 0.0
1871+
}
1872+
18591873
/// Gets a dictionary that indicates the current number of tokens staked
18601874
/// by all the nodes of each type
18611875
pub fun getTotalTokensStakedByNodeType(): {UInt8: UFix64} {
@@ -1906,7 +1920,8 @@ pub contract FlowIDTableStaking {
19061920
self.StakingAdminStoragePath = /storage/flowStakingAdmin
19071921
self.DelegatorStoragePath = /storage/flowStakingDelegator
19081922

1909-
self.minimumStakeRequired = {UInt8(0): 50.0, UInt8(1): 250000.0, UInt8(2): 500000.0, UInt8(3): 1250000.0, UInt8(4): 135000.0, UInt8(5): 100.0}
1923+
self.minimumStakeRequired = {UInt8(1): 250000.0, UInt8(2): 500000.0, UInt8(3): 1250000.0, UInt8(4): 135000.0, UInt8(5): 100.0}
1924+
self.account.save(50.0 as UFix64, to: /storage/delegatorStakingMinimum)
19101925
self.totalTokensStakedByNodeType = {UInt8(1): 0.0, UInt8(2): 0.0, UInt8(3): 0.0, UInt8(4): 0.0, UInt8(5): 0.0}
19111926
self.epochTokenPayout = epochTokenPayout
19121927
self.nodeDelegatingRewardCut = rewardCut

‎lib/go/contracts/internal/assets/assets.go

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎lib/go/templates/idtable_staking_templates.go

+15
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const (
1818
moveTokensFilename = "idTableStaking/admin/move_tokens.cdc"
1919
endEpochFilename = "idTableStaking/admin/end_epoch.cdc"
2020
changeMinimumsFilename = "idTableStaking/admin/change_minimums.cdc"
21+
changeDelegatorMinimumsFilename = "idTableStaking/admin/change_del_minimums.cdc"
2122
changeCutFilename = "idTableStaking/admin/change_cut.cdc"
2223
changePayoutFilename = "idTableStaking/admin/change_payout.cdc"
2324
endEpochChangePayoutFilename = "idTableStaking/admin/end_epoch_change_payout.cdc"
@@ -71,6 +72,7 @@ const (
7172
getNonOperationalListFilename = "idTableStaking/scripts/get_non_operational.cdc"
7273
getApprovedNodesFileName = "idTableStaking/scripts/get_approved_nodes.cdc"
7374
stakeRequirementsFilename = "idTableStaking/scripts/get_stake_requirements.cdc"
75+
delegatorStakeRequirementsFilename = "idTableStaking/scripts/get_del_stake_requirements.cdc"
7476
totalStakedByTypeFilename = "idTableStaking/scripts/get_total_staked_by_type.cdc"
7577
totalStakedFilename = "idTableStaking/scripts/get_total_staked.cdc"
7678
rewardRatioFilename = "idTableStaking/scripts/get_node_type_ratio.cdc"
@@ -170,6 +172,12 @@ func GenerateChangeMinimumsScript(env Environment) []byte {
170172
return []byte(ReplaceAddresses(code, env))
171173
}
172174

175+
func GenerateChangeDelegatorMinimumsScript(env Environment) []byte {
176+
code := assets.MustAssetString(changeDelegatorMinimumsFilename)
177+
178+
return []byte(ReplaceAddresses(code, env))
179+
}
180+
173181
// GenerateChangeCutScript creates a script that changes the cut percentage
174182
func GenerateChangeCutScript(env Environment) []byte {
175183
code := assets.MustAssetString(changeCutFilename)
@@ -351,6 +359,13 @@ func GenerateGetStakeRequirementsScript(env Environment) []byte {
351359
return []byte(ReplaceAddresses(code, env))
352360
}
353361

362+
// GenerateGetDelegatorStakeRequirementScript returns the stake requirement for delegators
363+
func GenerateGetDelegatorStakeRequirementScript(env Environment) []byte {
364+
code := assets.MustAssetString(delegatorStakeRequirementsFilename)
365+
366+
return []byte(ReplaceAddresses(code, env))
367+
}
368+
354369
// GenerateGetTotalTokensStakedByTypeScript returns the total tokens staked for a node type
355370
func GenerateGetTotalTokensStakedByTypeScript(env Environment) []byte {
356371
code := assets.MustAssetString(totalStakedByTypeFilename)

0 commit comments

Comments
 (0)
Please sign in to comment.