Skip to content

Commit 9f1c92e

Browse files
committed
feat: allow zero token allocation
1 parent a4a0771 commit 9f1c92e

File tree

1 file changed

+24
-21
lines changed

1 file changed

+24
-21
lines changed

contracts/staking/Staking.sol

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,9 +1105,6 @@ contract Staking is StakingV2Storage, GraphUpgradeable, IStaking {
11051105
) private {
11061106
require(_isAuth(_indexer), "!auth");
11071107

1108-
// Only allocations with a non-zero token amount are allowed
1109-
require(_tokens > 0, "!tokens");
1110-
11111108
// Check allocation
11121109
require(_allocationID != address(0), "!alloc");
11131110
require(_getAllocationState(_allocationID) == AllocationState.Null, "!null");
@@ -1119,7 +1116,9 @@ contract Staking is StakingV2Storage, GraphUpgradeable, IStaking {
11191116
require(ECDSA.recover(digest, _proof) == _allocationID, "!proof");
11201117

11211118
// Needs to have free capacity not used for other purposes to allocate
1122-
require(getIndexerCapacity(_indexer) >= _tokens, "!capacity");
1119+
if (_tokens > 0) {
1120+
require(getIndexerCapacity(_indexer) >= _tokens, "!capacity");
1121+
}
11231122

11241123
// Creates an allocation
11251124
// Allocation identifiers are not reused
@@ -1136,14 +1135,16 @@ contract Staking is StakingV2Storage, GraphUpgradeable, IStaking {
11361135
);
11371136
allocations[_allocationID] = alloc;
11381137

1139-
// Mark allocated tokens as used
1140-
stakes[_indexer].allocate(alloc.tokens);
1138+
if (_tokens > 0) {
1139+
// Mark allocated tokens as used
1140+
stakes[_indexer].allocate(alloc.tokens);
11411141

1142-
// Track total allocations per subgraph
1143-
// Used for rewards calculations
1144-
subgraphAllocations[alloc.subgraphDeploymentID] = subgraphAllocations[
1145-
alloc.subgraphDeploymentID
1146-
].add(alloc.tokens);
1142+
// Track total allocations per subgraph
1143+
// Used for rewards calculations
1144+
subgraphAllocations[alloc.subgraphDeploymentID] = subgraphAllocations[
1145+
alloc.subgraphDeploymentID
1146+
].add(alloc.tokens);
1147+
}
11471148

11481149
emit AllocationCreated(
11491150
_indexer,
@@ -1206,14 +1207,16 @@ contract Staking is StakingV2Storage, GraphUpgradeable, IStaking {
12061207
_updateRewards(alloc.subgraphDeploymentID);
12071208
}
12081209

1209-
// Free allocated tokens from use
1210-
stakes[alloc.indexer].unallocate(alloc.tokens);
1210+
if (alloc.tokens > 0) {
1211+
// Free allocated tokens from use
1212+
stakes[alloc.indexer].unallocate(alloc.tokens);
12111213

1212-
// Track total allocations per subgraph
1213-
// Used for rewards calculations
1214-
subgraphAllocations[alloc.subgraphDeploymentID] = subgraphAllocations[
1215-
alloc.subgraphDeploymentID
1216-
].sub(alloc.tokens);
1214+
// Track total allocations per subgraph
1215+
// Used for rewards calculations
1216+
subgraphAllocations[alloc.subgraphDeploymentID] = subgraphAllocations[
1217+
alloc.subgraphDeploymentID
1218+
].sub(alloc.tokens);
1219+
}
12171220

12181221
emit AllocationClosed(
12191222
alloc.indexer,
@@ -1255,8 +1258,8 @@ contract Staking is StakingV2Storage, GraphUpgradeable, IStaking {
12551258
// Purge allocation data except for:
12561259
// - indexer: used in disputes and to avoid reusing an allocationID
12571260
// - subgraphDeploymentID: used in disputes
1258-
allocations[_allocationID].tokens = 0; // This avoid collect(), close() and claim() to be called
1259-
allocations[_allocationID].createdAtEpoch = 0;
1261+
allocations[_allocationID].tokens = 0;
1262+
allocations[_allocationID].createdAtEpoch = 0; // This avoid collect(), close() and claim() to be called
12601263
allocations[_allocationID].closedAtEpoch = 0;
12611264
allocations[_allocationID].collectedFees = 0;
12621265
allocations[_allocationID].effectiveAllocation = 0;
@@ -1526,7 +1529,7 @@ contract Staking is StakingV2Storage, GraphUpgradeable, IStaking {
15261529
if (alloc.indexer == address(0)) {
15271530
return AllocationState.Null;
15281531
}
1529-
if (alloc.tokens == 0) {
1532+
if (alloc.createdAtEpoch == 0) {
15301533
return AllocationState.Claimed;
15311534
}
15321535

0 commit comments

Comments
 (0)