Skip to content

Commit de95d0c

Browse files
committedJun 26, 2023
staking del minimum for locked token register delegator
1 parent 3a41da7 commit de95d0c

File tree

8 files changed

+121
-17
lines changed

8 files changed

+121
-17
lines changed
 

‎contracts/LockedTokens.cdc

+7-2
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,11 @@ pub contract LockedTokens {
220220

221221
let vaultRef = self.vault.borrow()!
222222

223+
assert(
224+
vaultRef.balance >= FlowIDTableStaking.getDelegatorMinimumStakeRequirement(),
225+
message: "Must have the delegation minimum FLOW requirement in the locked vault to register a node"
226+
)
227+
223228
let tokens <- vaultRef.withdraw(amount: amount)
224229

225230
let delegator <- self.nodeDelegator <- FlowIDTableStaking.registerNewDelegator(nodeID: nodeID, tokensCommitted: <-tokens)
@@ -347,9 +352,9 @@ pub contract LockedTokens {
347352

348353
/// The user calls this function if they want to register as a node operator
349354
/// They have to provide the node ID for the node they want to delegate to
350-
pub fun createNodeDelegator(nodeID: String, amount: UFix64) {
355+
pub fun createNodeDelegator(nodeID: String) {
351356

352-
self.borrowTokenManager().registerDelegator(nodeID: nodeID, amount: amount)
357+
self.borrowTokenManager().registerDelegator(nodeID: nodeID, amount: FlowIDTableStaking.getDelegatorMinimumStakeRequirement())
353358

354359
// create a new delegator proxy that can be accessed in transactions
355360
self.nodeDelegatorProxy = LockedNodeDelegatorProxy(tokenManager: self.tokenManager)

‎contracts/testContracts/TestFlowIDTableStaking.cdc

+6
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,12 @@ pub contract FlowIDTableStaking {
264264
return <-create NodeDelegator(id: 1, nodeID: nodeID)
265265
}
266266

267+
/// Gets the minimum stake requirement for delegators
268+
pub fun getDelegatorMinimumStakeRequirement(): UFix64 {
269+
return self.account.copy<UFix64>(from: /storage/delegatorStakingMinimum)
270+
?? 0.0
271+
}
272+
267273
init(_ epochTokenPayout: UFix64, _ rewardCut: UFix64, _ candidateLimits: {UInt8: UInt64}) {
268274
}
269275
}

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

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

‎lib/go/templates/internal/assets/assets.go

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

‎lib/go/templates/manifest.mainnet.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@
289289
{
290290
"id": "TH.17",
291291
"name": "Register Delegator",
292-
"source": "import FlowToken from 0x1654653399040a61\nimport LockedTokens from 0x8d0e87b65159ae63\n\ntransaction(id: String, amount: UFix64) {\n\n let holderRef: \u0026LockedTokens.TokenHolder\n\n let vaultRef: \u0026FlowToken.Vault\n\n prepare(account: AuthAccount) {\n self.holderRef = account.borrow\u003c\u0026LockedTokens.TokenHolder\u003e(from: LockedTokens.TokenHolderStoragePath) \n ?? panic(\"TokenHolder is not saved at specified path\")\n\n self.vaultRef = account.borrow\u003c\u0026FlowToken.Vault\u003e(from: /storage/flowTokenVault)\n ?? panic(\"Could not borrow flow token vault reference\")\n }\n\n execute {\n let lockedBalance = self.holderRef.getLockedAccountBalance()\n\n if amount \u003c= lockedBalance {\n\n self.holderRef.createNodeDelegator(nodeID: id, amount: amount)\n\n } else if ((amount - lockedBalance) \u003c= self.vaultRef.balance) {\n\n self.holderRef.deposit(from: \u003c-self.vaultRef.withdraw(amount: amount - lockedBalance))\n\n self.holderRef.createNodeDelegator(nodeID: id, amount: amount)\n\n } else {\n panic(\"Not enough tokens to stake!\")\n }\n }\n}\n",
292+
"source": "import FlowToken from 0x1654653399040a61\nimport LockedTokens from 0x8d0e87b65159ae63\nimport FlowIDTableStaking from 0x8624b52f9ddcd04a\n\ntransaction(id: String, amount: UFix64) {\n\n let holderRef: \u0026LockedTokens.TokenHolder\n\n let vaultRef: \u0026FlowToken.Vault\n\n prepare(account: AuthAccount) {\n self.holderRef = account.borrow\u003c\u0026LockedTokens.TokenHolder\u003e(from: LockedTokens.TokenHolderStoragePath) \n ?? panic(\"TokenHolder is not saved at specified path\")\n\n self.vaultRef = account.borrow\u003c\u0026FlowToken.Vault\u003e(from: /storage/flowTokenVault)\n ?? panic(\"Could not borrow flow token vault reference\")\n }\n\n execute {\n let lockedBalance = self.holderRef.getLockedAccountBalance()\n\n if amount \u003c= lockedBalance {\n\n self.holderRef.createNodeDelegator(nodeID: id)\n\n let stakerProxy = self.holderRef.borrowDelegator()\n\n stakerProxy.delegateNewTokens(amount: amount - FlowIDTableStaking.getDelegatorMinimumStakeRequirement())\n\n } else if ((amount - lockedBalance) \u003c= self.vaultRef.balance) {\n\n self.holderRef.deposit(from: \u003c-self.vaultRef.withdraw(amount: amount - lockedBalance))\n\n self.holderRef.createNodeDelegator(nodeID: id)\n\n let stakerProxy = self.holderRef.borrowDelegator()\n\n stakerProxy.delegateNewTokens(amount: amount - FlowIDTableStaking.getDelegatorMinimumStakeRequirement())\n\n } else {\n panic(\"Not enough tokens to stake!\")\n }\n }\n}\n",
293293
"arguments": [
294294
{
295295
"type": "String",
@@ -315,7 +315,7 @@
315315
}
316316
],
317317
"network": "mainnet",
318-
"hash": "38b3863f83ae9920c3bedf6db9cbfb67c2c6f73fab7fa96c116511419d4cec3c"
318+
"hash": "35bd380710b471bc5efb41b2f1e8538aec2985f3e2bf196d5b2846a7d2db89ec"
319319
},
320320
{
321321
"id": "TH.19",

‎lib/go/templates/manifest.testnet.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@
289289
{
290290
"id": "TH.17",
291291
"name": "Register Delegator",
292-
"source": "import FlowToken from 0x7e60df042a9c0868\nimport LockedTokens from 0x95e019a17d0e23d7\n\ntransaction(id: String, amount: UFix64) {\n\n let holderRef: \u0026LockedTokens.TokenHolder\n\n let vaultRef: \u0026FlowToken.Vault\n\n prepare(account: AuthAccount) {\n self.holderRef = account.borrow\u003c\u0026LockedTokens.TokenHolder\u003e(from: LockedTokens.TokenHolderStoragePath) \n ?? panic(\"TokenHolder is not saved at specified path\")\n\n self.vaultRef = account.borrow\u003c\u0026FlowToken.Vault\u003e(from: /storage/flowTokenVault)\n ?? panic(\"Could not borrow flow token vault reference\")\n }\n\n execute {\n let lockedBalance = self.holderRef.getLockedAccountBalance()\n\n if amount \u003c= lockedBalance {\n\n self.holderRef.createNodeDelegator(nodeID: id, amount: amount)\n\n } else if ((amount - lockedBalance) \u003c= self.vaultRef.balance) {\n\n self.holderRef.deposit(from: \u003c-self.vaultRef.withdraw(amount: amount - lockedBalance))\n\n self.holderRef.createNodeDelegator(nodeID: id, amount: amount)\n\n } else {\n panic(\"Not enough tokens to stake!\")\n }\n }\n}\n",
292+
"source": "import FlowToken from 0x7e60df042a9c0868\nimport LockedTokens from 0x95e019a17d0e23d7\nimport FlowIDTableStaking from 0x9eca2b38b18b5dfe\n\ntransaction(id: String, amount: UFix64) {\n\n let holderRef: \u0026LockedTokens.TokenHolder\n\n let vaultRef: \u0026FlowToken.Vault\n\n prepare(account: AuthAccount) {\n self.holderRef = account.borrow\u003c\u0026LockedTokens.TokenHolder\u003e(from: LockedTokens.TokenHolderStoragePath) \n ?? panic(\"TokenHolder is not saved at specified path\")\n\n self.vaultRef = account.borrow\u003c\u0026FlowToken.Vault\u003e(from: /storage/flowTokenVault)\n ?? panic(\"Could not borrow flow token vault reference\")\n }\n\n execute {\n let lockedBalance = self.holderRef.getLockedAccountBalance()\n\n if amount \u003c= lockedBalance {\n\n self.holderRef.createNodeDelegator(nodeID: id)\n\n let stakerProxy = self.holderRef.borrowDelegator()\n\n stakerProxy.delegateNewTokens(amount: amount - FlowIDTableStaking.getDelegatorMinimumStakeRequirement())\n\n } else if ((amount - lockedBalance) \u003c= self.vaultRef.balance) {\n\n self.holderRef.deposit(from: \u003c-self.vaultRef.withdraw(amount: amount - lockedBalance))\n\n self.holderRef.createNodeDelegator(nodeID: id)\n\n let stakerProxy = self.holderRef.borrowDelegator()\n\n stakerProxy.delegateNewTokens(amount: amount - FlowIDTableStaking.getDelegatorMinimumStakeRequirement())\n\n } else {\n panic(\"Not enough tokens to stake!\")\n }\n }\n}\n",
293293
"arguments": [
294294
{
295295
"type": "String",
@@ -315,7 +315,7 @@
315315
}
316316
],
317317
"network": "testnet",
318-
"hash": "b11b8e3cdadeb0e505f801766c13f066bbcee1bcf39335d65f65d64f5533ca58"
318+
"hash": "d2ca185d8cb711dc89186efd550b24d93f0ddec17703229df731c55fbfa40582"
319319
},
320320
{
321321
"id": "TH.19",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import FlowIDTableStaking from 0xIDENTITYTABLEADDRESS
2+
3+
// This script finds all of a node's delegators who are staked above zero
4+
// but below the minimum of 50 FLOW and returns information about them
5+
6+
pub struct DelegatorBelowMinInfo {
7+
8+
pub var totalStaked: UFix64
9+
pub var totalBelowMinimumStaked: UFix64
10+
11+
pub var numDelegators: Int
12+
pub var numDelegatorsBelowMin: Int
13+
14+
pub var delegatorInfoBelowMin: [FlowIDTableStaking.DelegatorInfo]
15+
16+
init(numDelegators: Int) {
17+
self.totalStaked = 0.0
18+
self.totalBelowMinimumStaked = 0.0
19+
self.numDelegators = numDelegators
20+
self.numDelegatorsBelowMin = 0
21+
self.delegatorInfoBelowMin = []
22+
}
23+
24+
pub fun addTotalStaked(_ stake: UFix64) {
25+
self.totalStaked = self.totalStaked + stake
26+
}
27+
28+
pub fun addBelowMinStaked(_ stake: UFix64) {
29+
self.totalBelowMinimumStaked = self.totalBelowMinimumStaked + stake
30+
}
31+
32+
pub fun addDelegatorBelowMin() {
33+
self.numDelegatorsBelowMin = self.numDelegatorsBelowMin + 1
34+
}
35+
36+
pub fun addDelegatorInfo(_ info: FlowIDTableStaking.DelegatorInfo) {
37+
self.delegatorInfoBelowMin.append(info)
38+
}
39+
}
40+
41+
pub fun main(nodeID: String): DelegatorBelowMinInfo {
42+
let nodeInfo = FlowIDTableStaking.NodeInfo(nodeID: nodeID)
43+
44+
let delegators = nodeInfo.delegators
45+
46+
let belowMinimum = DelegatorBelowMinInfo(numDelegators: delegators.length)
47+
48+
for delegatorID in delegators {
49+
let delInfo = FlowIDTableStaking.DelegatorInfo(nodeID: nodeID, delegatorID: delegatorID)
50+
51+
belowMinimum.addTotalStaked(delInfo.tokensStaked)
52+
53+
if delInfo.tokensStaked < 50.0 && delInfo.tokensStaked > 0.0 {
54+
belowMinimum.addDelegatorInfo(delInfo)
55+
belowMinimum.addDelegatorBelowMin()
56+
belowMinimum.addBelowMinStaked(delInfo.tokensStaked)
57+
}
58+
}
59+
60+
return belowMinimum
61+
}

‎transactions/lockedTokens/delegator/register_delegator.cdc

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import FlowToken from 0xFLOWTOKENADDRESS
22
import LockedTokens from 0xLOCKEDTOKENADDRESS
3+
import FlowIDTableStaking from 0xIDENTITYTABLEADDRESS
34

45
transaction(id: String, amount: UFix64) {
56

@@ -20,13 +21,21 @@ transaction(id: String, amount: UFix64) {
2021

2122
if amount <= lockedBalance {
2223

23-
self.holderRef.createNodeDelegator(nodeID: id, amount: amount)
24+
self.holderRef.createNodeDelegator(nodeID: id)
25+
26+
let stakerProxy = self.holderRef.borrowDelegator()
27+
28+
stakerProxy.delegateNewTokens(amount: amount - FlowIDTableStaking.getDelegatorMinimumStakeRequirement())
2429

2530
} else if ((amount - lockedBalance) <= self.vaultRef.balance) {
2631

2732
self.holderRef.deposit(from: <-self.vaultRef.withdraw(amount: amount - lockedBalance))
2833

29-
self.holderRef.createNodeDelegator(nodeID: id, amount: amount)
34+
self.holderRef.createNodeDelegator(nodeID: id)
35+
36+
let stakerProxy = self.holderRef.borrowDelegator()
37+
38+
stakerProxy.delegateNewTokens(amount: amount - FlowIDTableStaking.getDelegatorMinimumStakeRequirement())
3039

3140
} else {
3241
panic("Not enough tokens to stake!")

0 commit comments

Comments
 (0)
Please sign in to comment.