@@ -3,7 +3,6 @@ pragma experimental ABIEncoderV2;
3
3
4
4
import "@openzeppelin/contracts/math/SafeMath.sol " ;
5
5
6
-
7
6
/**
8
7
* @title A collection of data structures and functions to manage the Stake state
9
8
* Used for low-level state changes, require() conditions should be evaluated
@@ -20,16 +19,16 @@ library Stakes {
20
19
}
21
20
22
21
struct Indexer {
23
- uint256 tokensIndexer ; // Tokens on the indexer stake (staked by the indexer)
24
- uint256 tokensAllocated; // Tokens used in SubgraphDeployment allocations
22
+ uint256 tokensStaked ; // Tokens on the indexer stake (staked by the indexer)
23
+ uint256 tokensAllocated; // Tokens used in allocations
25
24
uint256 tokensLocked; // Tokens locked for withdrawal subject to thawing period
26
25
uint256 tokensLockedUntil; // Time when locked tokens can be withdrawn
27
26
// SubgraphDeployment stake allocation tracking : subgraphDeploymentID => Allocation
28
27
mapping (bytes32 => Allocation) allocations;
29
28
}
30
29
31
30
/**
32
- * @dev Allocate tokens from the available stack to a SubgraphDeployment
31
+ * @dev Allocate tokens from the main stack to a SubgraphDeployment.
33
32
* @param stake Stake data
34
33
* @param _subgraphDeploymentID SubgraphDeployment where to allocate tokens
35
34
* @param _tokens Amount of tokens to allocate
@@ -46,7 +45,7 @@ library Stakes {
46
45
}
47
46
48
47
/**
49
- * @dev Unallocate tokens from a SubgraphDeployment
48
+ * @dev Unallocate tokens from a SubgraphDeployment.
50
49
* @param stake Stake data
51
50
* @param _subgraphDeploymentID SubgraphDeployment from where to unallocate tokens
52
51
* @param _tokens Amount of tokens to unallocate
@@ -63,25 +62,25 @@ library Stakes {
63
62
}
64
63
65
64
/**
66
- * @dev Deposit tokens to the indexer stake
65
+ * @dev Deposit tokens to the indexer stake.
67
66
* @param stake Stake data
68
67
* @param _tokens Amount of tokens to deposit
69
68
*/
70
69
function deposit (Stakes.Indexer storage stake , uint256 _tokens ) internal {
71
- stake.tokensIndexer = stake.tokensIndexer .add (_tokens);
70
+ stake.tokensStaked = stake.tokensStaked .add (_tokens);
72
71
}
73
72
74
73
/**
75
- * @dev Release tokens from the indexer stake
74
+ * @dev Release tokens from the indexer stake.
76
75
* @param stake Stake data
77
76
* @param _tokens Amount of tokens to release
78
77
*/
79
78
function release (Stakes.Indexer storage stake , uint256 _tokens ) internal {
80
- stake.tokensIndexer = stake.tokensIndexer .sub (_tokens);
79
+ stake.tokensStaked = stake.tokensStaked .sub (_tokens);
81
80
}
82
81
83
82
/**
84
- * @dev Lock tokens until a thawing period expires
83
+ * @dev Lock tokens until a thawing period expires,
85
84
* @param stake Stake data
86
85
* @param _tokens Amount of tokens to unstake
87
86
* @param _thawingPeriod Period in blocks that need to pass before withdrawal
@@ -103,7 +102,7 @@ library Stakes {
103
102
}
104
103
105
104
/**
106
- * @dev Unlock tokens
105
+ * @dev Unlock tokens.
107
106
* @param stake Stake data
108
107
* @param _tokens Amount of tokens to unkock
109
108
*/
@@ -115,7 +114,7 @@ library Stakes {
115
114
}
116
115
117
116
/**
118
- * @dev Take all tokens out from the locked stack for withdrawal
117
+ * @dev Take all tokens out from the locked stack for withdrawal.
119
118
* @param stake Stake data
120
119
* @return Amount of tokens being withdrawn
121
120
*/
@@ -135,14 +134,15 @@ library Stakes {
135
134
}
136
135
137
136
/**
138
- * @dev Get the locking period of the tokens to unstake, if already unstaked before calculate the weighted average
137
+ * @dev Get the locking period of the tokens to unstake.
138
+ * If already unstaked before calculate the weighted average.
139
139
* @param stake Stake data
140
140
* @param _tokens Amount of tokens to unstake
141
141
* @param _thawingPeriod Period in blocks that need to pass before withdrawal
142
142
* @return True if staked
143
143
*/
144
144
function getLockingPeriod (
145
- Stakes.Indexer storage stake ,
145
+ Stakes.Indexer memory stake ,
146
146
uint256 _tokens ,
147
147
uint256 _thawingPeriod
148
148
) internal view returns (uint256 ) {
@@ -157,16 +157,16 @@ library Stakes {
157
157
}
158
158
159
159
/**
160
- * @dev Return true if there are tokens staked by the Indexer
160
+ * @dev Return true if there are tokens staked by the Indexer.
161
161
* @param stake Stake data
162
162
* @return True if staked
163
163
*/
164
- function hasTokens (Stakes.Indexer storage stake ) internal view returns (bool ) {
165
- return stake.tokensIndexer > 0 ;
164
+ function hasTokens (Stakes.Indexer memory stake ) internal view returns (bool ) {
165
+ return stake.tokensStaked > 0 ;
166
166
}
167
167
168
168
/**
169
- * @dev Return true if the indexer has allocated stake on the SubgraphDeployment
169
+ * @dev Return true if the indexer has allocated stake on the SubgraphDeployment.
170
170
* @param stake Stake data
171
171
* @param _subgraphDeploymentID SubgraphDeployment for the allocation
172
172
* @return True if allocated
@@ -180,77 +180,67 @@ library Stakes {
180
180
}
181
181
182
182
/**
183
- * @dev Total tokens staked from indexer
183
+ * @dev Return the amount of tokens used in allocations and locked for withdrawal.
184
184
* @param stake Stake data
185
185
* @return Token amount
186
186
*/
187
- function tokensStaked (Stakes.Indexer storage stake ) internal view returns (uint256 ) {
188
- return stake.tokensIndexer ;
187
+ function tokensUsed (Stakes.Indexer memory stake ) internal view returns (uint256 ) {
188
+ return stake.tokensAllocated. add (stake.tokensLocked) ;
189
189
}
190
190
191
191
/**
192
- * @dev Tokens available for use in allocations
193
- * @dev tokensIndexer - tokensAllocated - tokensLocked
192
+ * @dev Tokens free balance on the indexer stake.
193
+ * tokensStaked - tokensAllocated - tokensLocked
194
194
* @param stake Stake data
195
195
* @return Token amount
196
196
*/
197
- function tokensAvailable (Stakes.Indexer storage stake ) internal view returns (uint256 ) {
198
- uint256 _tokensStaked = stake.tokensStaked ();
199
- uint256 tokensUsed = stake.tokensAllocated.add (stake.tokensLocked);
200
- // Stake is over allocated: return 0 to avoid stake to be used until the overallocation
201
- // is restored by staking more tokens or unallocating tokens
202
- if (tokensUsed > _tokensStaked) {
197
+ function tokensAvailable (Stakes.Indexer memory stake ) internal view returns (uint256 ) {
198
+ uint256 tokensUsed = stake.tokensUsed ();
199
+ // Indexer stake is over allocated: return 0 to avoid stake to be used until
200
+ // the overallocation is restored by staking more tokens or unallocating tokens
201
+ if (tokensUsed > stake.tokensStaked) {
203
202
return 0 ;
204
203
}
205
- return _tokensStaked.sub (tokensUsed);
206
- }
207
-
208
- /**
209
- * @dev Tokens used for slashing whenever necessary
210
- * @param stake Stake data
211
- * @return Token amount
212
- */
213
- function tokensSlashable (Stakes.Indexer storage stake ) internal view returns (uint256 ) {
214
- return stake.tokensIndexer;
204
+ return stake.tokensStaked.sub (tokensUsed);
215
205
}
216
206
217
207
/**
218
- * @dev Tokens available for withdrawal after thawing period
208
+ * @dev Tokens available for withdrawal after thawing period.
219
209
* @param stake Stake data
220
210
* @return Token amount
221
211
*/
222
- function tokensWithdrawable (Stakes.Indexer storage stake ) internal view returns (uint256 ) {
212
+ function tokensWithdrawable (Stakes.Indexer memory stake ) internal view returns (uint256 ) {
223
213
// No tokens to withdraw before locking period
224
214
if (stake.tokensLockedUntil == 0 || block .number < stake.tokensLockedUntil) {
225
215
return 0 ;
226
216
}
227
217
// Cannot withdraw more than currently staked
228
218
// This condition can happen if while tokens are locked for withdrawal a slash condition happens
229
219
// In that case the total staked tokens could be below the amount to be withdrawn
230
- if (stake.tokensLocked > stake.tokensIndexer ) {
231
- return stake.tokensIndexer ;
220
+ if (stake.tokensLocked > stake.tokensStaked ) {
221
+ return stake.tokensStaked ;
232
222
}
233
223
return stake.tokensLocked;
234
224
}
235
225
236
226
/**
237
- * @dev Return if channel for an allocation is active
227
+ * @dev Return if channel for an allocation is active.
238
228
* @param alloc Allocation data
239
229
* @return True if channel related to allocation is active
240
230
*/
241
- function hasChannel (Stakes.Allocation storage alloc ) internal view returns (bool ) {
231
+ function hasChannel (Stakes.Allocation memory alloc ) internal view returns (bool ) {
242
232
return alloc.channelID != address (0 );
243
233
}
244
234
245
235
/**
246
- * @dev Get the effective stake allocation considering epochs from allocation to settlement
236
+ * @dev Get the effective stake allocation considering epochs from allocation to settlement.
247
237
* @param alloc Allocation data
248
238
* @param _numEpochs Number of epochs that passed from allocation to settlement
249
239
* @param _maxEpochs Number of epochs used as a maximum to cap effective allocation
250
240
* @return Effective allocated tokens accross epochs
251
241
*/
252
242
function getTokensEffectiveAllocation (
253
- Stakes.Allocation storage alloc ,
243
+ Stakes.Allocation memory alloc ,
254
244
uint256 _numEpochs ,
255
245
uint256 _maxEpochs
256
246
) internal view returns (uint256 ) {
0 commit comments