@@ -14,8 +14,15 @@ import { PPMMath } from "@graphprotocol/horizon/contracts/libraries/PPMMath.sol"
14
14
15
15
import { Allocation } from "../libraries/Allocation.sol " ;
16
16
import { LegacyAllocation } from "../libraries/LegacyAllocation.sol " ;
17
- import { AllocationManager } from "../utilities/AllocationManager.sol " ;
18
17
18
+ /**
19
+ * @title AllocationHandler contract
20
+ * @notice A helper contract implementing allocation lifecycle management.
21
+ * Allows opening, resizing, and closing allocations, as well as collecting indexing rewards by presenting a Proof
22
+ * of Indexing (POI).
23
+ * @custom:security-contact Please email security+contracts@thegraph.com if you find any
24
+ * bugs. We may have an active bug bounty program.
25
+ */
19
26
library AllocationHandler {
20
27
using ProvisionTracker for mapping (address => uint256 );
21
28
using Allocation for mapping (address => Allocation.State);
@@ -76,6 +83,122 @@ library AllocationHandler {
76
83
address _paymentsDestination;
77
84
}
78
85
86
+ /**
87
+ * @notice Emitted when an indexer creates an allocation
88
+ * @param indexer The address of the indexer
89
+ * @param allocationId The id of the allocation
90
+ * @param subgraphDeploymentId The id of the subgraph deployment
91
+ * @param tokens The amount of tokens allocated
92
+ * @param currentEpoch The current epoch
93
+ */
94
+ event AllocationCreated (
95
+ address indexed indexer ,
96
+ address indexed allocationId ,
97
+ bytes32 indexed subgraphDeploymentId ,
98
+ uint256 tokens ,
99
+ uint256 currentEpoch
100
+ );
101
+
102
+ /**
103
+ * @notice Emitted when an indexer collects indexing rewards for an allocation
104
+ * @param indexer The address of the indexer
105
+ * @param allocationId The id of the allocation
106
+ * @param subgraphDeploymentId The id of the subgraph deployment
107
+ * @param tokensRewards The amount of tokens collected
108
+ * @param tokensIndexerRewards The amount of tokens collected for the indexer
109
+ * @param tokensDelegationRewards The amount of tokens collected for delegators
110
+ * @param poi The POI presented
111
+ * @param currentEpoch The current epoch
112
+ * @param poiMetadata The metadata associated with the POI
113
+ */
114
+ event IndexingRewardsCollected (
115
+ address indexed indexer ,
116
+ address indexed allocationId ,
117
+ bytes32 indexed subgraphDeploymentId ,
118
+ uint256 tokensRewards ,
119
+ uint256 tokensIndexerRewards ,
120
+ uint256 tokensDelegationRewards ,
121
+ bytes32 poi ,
122
+ bytes poiMetadata ,
123
+ uint256 currentEpoch
124
+ );
125
+
126
+ /**
127
+ * @notice Emitted when an indexer resizes an allocation
128
+ * @param indexer The address of the indexer
129
+ * @param allocationId The id of the allocation
130
+ * @param subgraphDeploymentId The id of the subgraph deployment
131
+ * @param newTokens The new amount of tokens allocated
132
+ * @param oldTokens The old amount of tokens allocated
133
+ */
134
+ event AllocationResized (
135
+ address indexed indexer ,
136
+ address indexed allocationId ,
137
+ bytes32 indexed subgraphDeploymentId ,
138
+ uint256 newTokens ,
139
+ uint256 oldTokens
140
+ );
141
+
142
+ /**
143
+ * @dev Emitted when an indexer closes an allocation
144
+ * @param indexer The address of the indexer
145
+ * @param allocationId The id of the allocation
146
+ * @param subgraphDeploymentId The id of the subgraph deployment
147
+ * @param tokens The amount of tokens allocated
148
+ * @param forceClosed Whether the allocation was force closed
149
+ */
150
+ event AllocationClosed (
151
+ address indexed indexer ,
152
+ address indexed allocationId ,
153
+ bytes32 indexed subgraphDeploymentId ,
154
+ uint256 tokens ,
155
+ bool forceClosed
156
+ );
157
+
158
+ /**
159
+ * @notice Emitted when a legacy allocation is migrated into the subgraph service
160
+ * @param indexer The address of the indexer
161
+ * @param allocationId The id of the allocation
162
+ * @param subgraphDeploymentId The id of the subgraph deployment
163
+ */
164
+ event LegacyAllocationMigrated (
165
+ address indexed indexer ,
166
+ address indexed allocationId ,
167
+ bytes32 indexed subgraphDeploymentId
168
+ );
169
+
170
+ /**
171
+ * @notice Emitted when the maximum POI staleness is updated
172
+ * @param maxPOIStaleness The max POI staleness in seconds
173
+ */
174
+ event MaxPOIStalenessSet (uint256 maxPOIStaleness );
175
+
176
+ /**
177
+ * @notice Thrown when an allocation proof is invalid
178
+ * Both `signer` and `allocationId` should match for a valid proof.
179
+ * @param signer The address that signed the proof
180
+ * @param allocationId The id of the allocation
181
+ */
182
+ error AllocationHandlerInvalidAllocationProof (address signer , address allocationId );
183
+
184
+ /**
185
+ * @notice Thrown when attempting to create an allocation with a zero allocation id
186
+ */
187
+ error AllocationHandlerInvalidZeroAllocationId ();
188
+
189
+ /**
190
+ * @notice Thrown when attempting to collect indexing rewards on a closed allocationl
191
+ * @param allocationId The id of the allocation
192
+ */
193
+ error AllocationHandlerAllocationClosed (address allocationId );
194
+
195
+ /**
196
+ * @notice Thrown when attempting to resize an allocation with the same size
197
+ * @param allocationId The id of the allocation
198
+ * @param tokens The amount of tokens
199
+ */
200
+ error AllocationHandlerAllocationSameSize (address allocationId , uint256 tokens );
201
+
79
202
/**
80
203
* @notice Create an allocation
81
204
* @dev The `_allocationProof` is a 65-bytes Ethereum signed message of `keccak256(indexerAddress,allocationId)`
@@ -98,7 +221,7 @@ library AllocationHandler {
98
221
mapping (bytes32 subgraphDeploymentId = > uint256 tokens ) storage _subgraphAllocatedTokens ,
99
222
AllocateParams memory params
100
223
) external {
101
- require (params._allocationId != address (0 ), AllocationManager. AllocationManagerInvalidZeroAllocationId ());
224
+ require (params._allocationId != address (0 ), AllocationHandler. AllocationHandlerInvalidZeroAllocationId ());
102
225
103
226
_verifyAllocationProof (params._encodeAllocationProof, params._allocationId, params._allocationProof);
104
227
@@ -124,7 +247,7 @@ library AllocationHandler {
124
247
_subgraphAllocatedTokens[allocation.subgraphDeploymentId] +
125
248
allocation.tokens;
126
249
127
- emit AllocationManager .AllocationCreated (
250
+ emit AllocationHandler .AllocationCreated (
128
251
params._indexer,
129
252
params._allocationId,
130
253
params._subgraphDeploymentId,
@@ -166,7 +289,7 @@ library AllocationHandler {
166
289
PresentParams memory params
167
290
) external returns (uint256 ) {
168
291
Allocation.State memory allocation = _allocations.get (params._allocationId);
169
- require (allocation.isOpen (), AllocationManager. AllocationManagerAllocationClosed (params._allocationId));
292
+ require (allocation.isOpen (), AllocationHandler. AllocationHandlerAllocationClosed (params._allocationId));
170
293
171
294
// Mint indexing rewards if all conditions are met
172
295
uint256 tokensRewards = (! allocation.isStale (params.maxPOIStaleness) &&
@@ -217,7 +340,7 @@ library AllocationHandler {
217
340
}
218
341
}
219
342
220
- emit AllocationManager .IndexingRewardsCollected (
343
+ emit AllocationHandler .IndexingRewardsCollected (
221
344
allocation.indexer,
222
345
params._allocationId,
223
346
allocation.subgraphDeploymentId,
@@ -318,10 +441,10 @@ library AllocationHandler {
318
441
uint32 _delegationRatio
319
442
) external {
320
443
Allocation.State memory allocation = _allocations.get (_allocationId);
321
- require (allocation.isOpen (), AllocationManager. AllocationManagerAllocationClosed (_allocationId));
444
+ require (allocation.isOpen (), AllocationHandler. AllocationHandlerAllocationClosed (_allocationId));
322
445
require (
323
446
_tokens != allocation.tokens,
324
- AllocationManager. AllocationManagerAllocationSameSize (_allocationId, _tokens)
447
+ AllocationHandler. AllocationHandlerAllocationSameSize (_allocationId, _tokens)
325
448
);
326
449
327
450
// Update provision tracker
@@ -355,7 +478,7 @@ library AllocationHandler {
355
478
_subgraphAllocatedTokens[allocation.subgraphDeploymentId] -= (oldTokens - _tokens);
356
479
}
357
480
358
- emit AllocationManager .AllocationResized (
481
+ emit AllocationHandler .AllocationResized (
359
482
allocation.indexer,
360
483
_allocationId,
361
484
allocation.subgraphDeploymentId,
@@ -421,7 +544,7 @@ library AllocationHandler {
421
544
_subgraphAllocatedTokens[allocation.subgraphDeploymentId] -
422
545
allocation.tokens;
423
546
424
- emit AllocationManager .AllocationClosed (
547
+ emit AllocationHandler .AllocationClosed (
425
548
allocation.indexer,
426
549
_allocationId,
427
550
allocation.subgraphDeploymentId,
@@ -463,7 +586,7 @@ library AllocationHandler {
463
586
address signer = ECDSA.recover (_encodeAllocationProof, _proof);
464
587
require (
465
588
signer == _allocationId,
466
- AllocationManager. AllocationManagerInvalidAllocationProof (signer, _allocationId)
589
+ AllocationHandler. AllocationHandlerInvalidAllocationProof (signer, _allocationId)
467
590
);
468
591
}
469
592
}
0 commit comments