@@ -952,8 +952,8 @@ contract Staking is StakingV1Storage, GraphUpgradeable, IStaking {
952
952
allocState == AllocationState.Closed)
953
953
? protocolPercentage
954
954
: MAX_PPM;
955
- uint256 protocolFees = _collectTax (graphToken, queryFees, usedProtocolPercentage);
956
- queryFees = queryFees.sub (protocolFees );
955
+ uint256 protocolTax = _collectTax (graphToken, queryFees, usedProtocolPercentage);
956
+ queryFees = queryFees.sub (protocolTax );
957
957
958
958
// -- Collect curation fees --
959
959
// Only if the subgraph deployment is curated
@@ -1128,12 +1128,12 @@ contract Staking is StakingV1Storage, GraphUpgradeable, IStaking {
1128
1128
require (allocState == AllocationState.Active, "!active " );
1129
1129
1130
1130
// Get allocation
1131
- Allocation storage alloc = allocations[_allocationID];
1131
+ Allocation memory alloc = allocations[_allocationID];
1132
1132
1133
1133
// Validate that an allocation cannot be closed before one epoch
1134
- uint256 currentEpoch = epochManager ().currentEpoch ();
1135
- uint256 epochs = alloc.createdAtEpoch < currentEpoch
1136
- ? currentEpoch .sub (alloc.createdAtEpoch)
1134
+ alloc.closedAtEpoch = epochManager ().currentEpoch ();
1135
+ uint256 epochs = alloc.createdAtEpoch < alloc.closedAtEpoch
1136
+ ? alloc.closedAtEpoch .sub (alloc.createdAtEpoch)
1137
1137
: 0 ;
1138
1138
require (epochs > 0 , "<epochs " );
1139
1139
@@ -1146,13 +1146,20 @@ contract Staking is StakingV1Storage, GraphUpgradeable, IStaking {
1146
1146
require (isIndexer, "!auth " );
1147
1147
}
1148
1148
1149
+ // Calculate effective allocation for the amount of epochs it remained allocated
1150
+ alloc.effectiveAllocation = _getEffectiveAllocation (
1151
+ maxAllocationEpochs,
1152
+ alloc.tokens,
1153
+ epochs
1154
+ );
1155
+
1149
1156
// Close the allocation and start counting a period to settle remaining payments from
1150
1157
// state channels.
1151
- alloc .closedAtEpoch = currentEpoch ;
1152
- alloc .effectiveAllocation = _getEffectiveAllocation ( alloc.tokens, epochs) ;
1158
+ allocations[_allocationID] .closedAtEpoch = alloc.closedAtEpoch ;
1159
+ allocations[_allocationID] .effectiveAllocation = alloc.effectiveAllocation ;
1153
1160
1154
1161
// Account collected fees and effective allocation in rebate pool for the epoch
1155
- Rebates.Pool storage rebatePool = rebates[currentEpoch ];
1162
+ Rebates.Pool storage rebatePool = rebates[alloc.closedAtEpoch ];
1156
1163
if (! rebatePool.exists ()) {
1157
1164
rebatePool.init (alphaNumerator, alphaDenominator);
1158
1165
}
@@ -1196,23 +1203,21 @@ contract Staking is StakingV1Storage, GraphUpgradeable, IStaking {
1196
1203
require (allocState == AllocationState.Finalized, "!finalized " );
1197
1204
1198
1205
// Get allocation
1199
- Allocation storage alloc = allocations[_allocationID];
1200
- address indexer = alloc.indexer;
1201
- uint256 closedAtEpoch = alloc.closedAtEpoch;
1206
+ Allocation memory alloc = allocations[_allocationID];
1202
1207
1203
1208
// Only the indexer or operator can decide if to restake
1204
- bool restake = _isAuth (indexer) ? _restake : false ;
1209
+ bool restake = _isAuth (alloc. indexer) ? _restake : false ;
1205
1210
1206
1211
// Process rebate reward
1207
- Rebates.Pool storage rebatePool = rebates[closedAtEpoch];
1212
+ Rebates.Pool storage rebatePool = rebates[alloc. closedAtEpoch];
1208
1213
uint256 tokensToClaim = rebatePool.redeem (alloc.collectedFees, alloc.effectiveAllocation);
1209
1214
1210
- // Calculate delegation rewards and add them to the delegation pool
1211
- uint256 delegationRewards = _collectDelegationQueryRewards (indexer, tokensToClaim);
1215
+ // Add delegation rewards to the delegation pool
1216
+ uint256 delegationRewards = _collectDelegationQueryRewards (alloc. indexer, tokensToClaim);
1212
1217
tokensToClaim = tokensToClaim.sub (delegationRewards);
1213
1218
1214
1219
// Update the allocate state
1215
- alloc .tokens = 0 ; // This avoid collect(), close() and claim() to be called
1220
+ allocations[_allocationID] .tokens = 0 ; // This avoid collect(), close() and claim() to be called
1216
1221
1217
1222
// -- Interactions --
1218
1223
@@ -1221,27 +1226,27 @@ contract Staking is StakingV1Storage, GraphUpgradeable, IStaking {
1221
1226
// When all allocations processed then burn unclaimed fees and prune rebate pool
1222
1227
if (rebatePool.unclaimedAllocationsCount == 0 ) {
1223
1228
_burnTokens (graphToken, rebatePool.unclaimedFees ());
1224
- delete rebates[closedAtEpoch];
1229
+ delete rebates[alloc. closedAtEpoch];
1225
1230
}
1226
1231
1227
1232
// When there are tokens to claim from the rebate pool, transfer or restake
1228
1233
if (tokensToClaim > 0 ) {
1229
1234
// Assign claimed tokens
1230
1235
if (restake) {
1231
1236
// Restake to place fees into the indexer stake
1232
- _stake (indexer, tokensToClaim);
1237
+ _stake (alloc. indexer, tokensToClaim);
1233
1238
} else {
1234
1239
// Transfer funds back to the indexer
1235
- require (graphToken.transfer (indexer, tokensToClaim), "!transfer " );
1240
+ require (graphToken.transfer (alloc. indexer, tokensToClaim), "!transfer " );
1236
1241
}
1237
1242
}
1238
1243
1239
1244
emit RebateClaimed (
1240
- indexer,
1245
+ alloc. indexer,
1241
1246
alloc.subgraphDeploymentID,
1242
1247
_allocationID,
1243
1248
epochManager ().currentEpoch (),
1244
- closedAtEpoch,
1249
+ alloc. closedAtEpoch,
1245
1250
tokensToClaim,
1246
1251
rebatePool.unclaimedAllocationsCount,
1247
1252
delegationRewards
@@ -1504,17 +1509,18 @@ contract Staking is StakingV1Storage, GraphUpgradeable, IStaking {
1504
1509
1505
1510
/**
1506
1511
* @dev Get the effective stake allocation considering epochs from allocation to closing.
1512
+ * @param _maxAllocationEpochs Max amount of epochs to cap the allocated stake
1507
1513
* @param _tokens Amount of tokens allocated
1508
1514
* @param _numEpochs Number of epochs that passed from allocation to closing
1509
1515
* @return Effective allocated tokens across epochs
1510
1516
*/
1511
- function _getEffectiveAllocation (uint256 _tokens , uint256 _numEpochs )
1512
- private
1513
- view
1514
- returns ( uint256 )
1515
- {
1516
- bool shouldCap = maxAllocationEpochs > 0 && _numEpochs > maxAllocationEpochs ;
1517
- return _tokens.mul ((shouldCap) ? maxAllocationEpochs : _numEpochs);
1517
+ function _getEffectiveAllocation (
1518
+ uint256 _maxAllocationEpochs ,
1519
+ uint256 _tokens ,
1520
+ uint256 _numEpochs
1521
+ ) private pure returns ( uint256 ) {
1522
+ bool shouldCap = _maxAllocationEpochs > 0 && _numEpochs > _maxAllocationEpochs ;
1523
+ return _tokens.mul ((shouldCap) ? _maxAllocationEpochs : _numEpochs);
1518
1524
}
1519
1525
1520
1526
/**
0 commit comments