-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathTestFlowIDTableStaking.cdc
281 lines (210 loc) · 9.05 KB
/
TestFlowIDTableStaking.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
/*
TestFlowIDTableStaking
This is a test contract to act as an API for
testing the lockbox and staking proxy contracts.
*/
import FungibleToken from "FungibleToken"
import FlowToken from "FlowToken"
// import Burner from "Burner"
access(all) contract FlowIDTableStaking {
/*********** ID Table and Staking Composite Type Definitions *************/
/// Contains information that is specific to a node in Flow
/// only lives in this contract
access(all) resource NodeRecord {
/// The unique ID of the node
/// Set when the node is created
access(all) let id: String
/// The type of node:
/// 1 = collection
/// 2 = consensus
/// 3 = execution
/// 4 = verification
/// 5 = access
access(all) var role: UInt8
/// The address used for networking
access(all) var networkingAddress: String
/// the public key for networking
access(all) var networkingKey: String
/// the public key for staking
access(all) var stakingKey: String
init(
id: String,
role: UInt8, /// role that the node will have for future epochs
networkingAddress: String,
networkingKey: String,
stakingKey: String,
tokensCommitted: @{FungibleToken.Vault}
) {
self.id = id
self.role = role
self.networkingAddress = networkingAddress
self.networkingKey = networkingKey
self.stakingKey = stakingKey
destroy tokensCommitted
}
}
// Struct to create to get read-only info about a node
access(all) struct NodeInfo {
access(all) let id: String
access(all) let role: UInt8
access(all) let networkingAddress: String
access(all) let networkingKey: String
access(all) let stakingKey: String
access(all) let tokensStaked: UFix64
access(all) let totalTokensStaked: UFix64
access(all) let tokensCommitted: UFix64
access(all) let tokensUnstaking: UFix64
access(all) let tokensUnstaked: UFix64
access(all) let tokensRewarded: UFix64
/// list of delegator IDs for this node operator
access(all) let delegators: [UInt32]
access(all) let delegatorIDCounter: UInt32
access(all) let tokensRequestedToUnstake: UFix64
access(all) let initialWeight: UInt64
init(nodeID: String) {
self.id = nodeID
self.role = 2
self.networkingAddress = "address"
self.networkingKey = "key"
self.stakingKey = "key"
self.tokensStaked = 0.0
self.totalTokensStaked = 0.0
self.tokensCommitted = 0.0
self.tokensUnstaking = 0.0
self.tokensUnstaked = 0.0
self.tokensRewarded = 0.0
self.delegators = []
self.delegatorIDCounter = 0
self.tokensRequestedToUnstake = 0.0
self.initialWeight = 0
}
}
access(all) entitlement NodeOperator
/// Resource that the node operator controls for staking
access(all) resource NodeStaker {
/// Unique ID for the node operator
access(all) let id: String
init(id: String) {
self.id = id
}
access(NodeOperator) fun updateNetworkingAddress(_ newAddress: String) {
}
/// Add new tokens to the system to stake during the next epoch
access(NodeOperator) fun stakeNewTokens(_ tokens: @{FungibleToken.Vault}) {
destroy tokens
}
/// Stake tokens that are in the tokensUnstaked bucket
/// but haven't been officially staked
access(NodeOperator) fun stakeUnstakedTokens(amount: UFix64) {
}
/// Stake tokens that are in the tokensRewarded bucket
/// but haven't been officially staked
access(NodeOperator) fun stakeRewardedTokens(amount: UFix64) {
}
/// Request amount tokens to be removed from staking
/// at the end of the next epoch
access(NodeOperator) fun requestUnstaking(amount: UFix64) {
}
/// Requests to unstake all of the node operators staked and committed tokens,
/// as well as all the staked and committed tokens of all of their delegators
access(NodeOperator) fun unstakeAll() {
}
/// Withdraw tokens from the unstaked bucket
access(NodeOperator) fun withdrawUnstakedTokens(amount: UFix64): @{FungibleToken.Vault} {
let flowTokenMinter = FlowIDTableStaking.account.storage.borrow<&FlowToken.Minter>(from: /storage/flowTokenMinter)
?? panic("Could not borrow minter reference")
return <- flowTokenMinter.mintTokens(amount: amount)
}
/// Withdraw tokens from the rewarded bucket
access(NodeOperator) fun withdrawRewardedTokens(amount: UFix64): @{FungibleToken.Vault} {
let flowTokenMinter = FlowIDTableStaking.account.storage.borrow<&FlowToken.Minter>(from: /storage/flowTokenMinter)
?? panic("Could not borrow minter reference")
return <- flowTokenMinter.mintTokens(amount: amount)
}
}
access(all) entitlement DelegatorOwner
access(all) struct DelegatorInfo {
access(all) let id: UInt32
access(all) let nodeID: String
access(all) let tokensCommitted: UFix64
access(all) let tokensStaked: UFix64
access(all) let tokensUnstaking: UFix64
access(all) let tokensRewarded: UFix64
access(all) let tokensUnstaked: UFix64
access(all) let tokensRequestedToUnstake: UFix64
init(nodeID: String, delegatorID: UInt32) {
self.id = delegatorID
self.nodeID = nodeID
self.tokensCommitted = 0.0
self.tokensStaked = 0.0
self.tokensUnstaking = 0.0
self.tokensUnstaked = 0.0
self.tokensRewarded = 0.0
self.tokensRequestedToUnstake = 0.0
}
}
/// Resource object that the delegator stores in their account
/// to perform staking actions
access(all) resource NodeDelegator {
/// Each delegator for a node operator has a unique ID
access(all) let id: UInt32
/// The ID of the node operator that this delegator delegates to
access(all) let nodeID: String
init(id: UInt32, nodeID: String) {
self.id = id
self.nodeID = nodeID
}
/// Delegate new tokens to the node operator
access(DelegatorOwner) fun delegateNewTokens(from: @{FungibleToken.Vault}) {
destroy from
}
/// Delegate tokens from the unstaked bucket to the node operator
access(DelegatorOwner) fun delegateUnstakedTokens(amount: UFix64) {
}
/// Delegate tokens from the rewards bucket to the node operator
access(DelegatorOwner) fun delegateRewardedTokens(amount: UFix64) {
}
/// Request to unstake delegated tokens during the next epoch
access(DelegatorOwner) fun requestUnstaking(amount: UFix64) {
}
/// Withdraw tokens from the unstaked bucket
access(DelegatorOwner) fun withdrawUnstakedTokens(amount: UFix64): @{FungibleToken.Vault} {
let flowTokenMinter = FlowIDTableStaking.account.storage.borrow<&FlowToken.Minter>(from: /storage/flowTokenMinter)
?? panic("Could not borrow minter reference")
return <- flowTokenMinter.mintTokens(amount: amount)
}
/// Withdraw tokens from the rewarded bucket
access(DelegatorOwner) fun withdrawRewardedTokens(amount: UFix64): @{FungibleToken.Vault} {
let flowTokenMinter = FlowIDTableStaking.account.storage.borrow<&FlowToken.Minter>(from: /storage/flowTokenMinter)
?? panic("Could not borrow minter reference")
return <- flowTokenMinter.mintTokens(amount: amount)
}
}
/// Any node can call this function to register a new Node
/// It returns the resource for nodes that they can store in
/// their account storage
access(all) fun addNodeRecord(
id: String,
role: UInt8,
networkingAddress: String,
networkingKey: String,
stakingKey: String,
stakingKeyPoP: String,
tokensCommitted: @{FungibleToken.Vault}
): @NodeStaker {
destroy tokensCommitted
// return a new NodeStaker object that the node operator stores in their account
return <-create NodeStaker(id: id)
}
access(all) fun registerNewDelegator(nodeID: String, tokensCommitted: @{FungibleToken.Vault}): @NodeDelegator {
destroy tokensCommitted
return <-create NodeDelegator(id: 1, nodeID: nodeID)
}
/// Gets the minimum stake requirement for delegators
access(all) fun getDelegatorMinimumStakeRequirement(): UFix64 {
return self.account.storage.copy<UFix64>(from: /storage/delegatorStakingMinimum)
?? 0.0
}
init(_ epochTokenPayout: UFix64, _ rewardCut: UFix64, _ candidateLimits: {UInt8: UInt64}) {
}
}