@@ -14,8 +14,15 @@ import { PPMMath } from "@graphprotocol/horizon/contracts/libraries/PPMMath.sol"
1414
1515import { Allocation } from "../libraries/Allocation.sol " ;
1616import { LegacyAllocation } from "../libraries/LegacyAllocation.sol " ;
17- import { AllocationManager } from "../utilities/AllocationManager.sol " ;
1817
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+ */
1926library AllocationHandler {
2027 using ProvisionTracker for mapping (address => uint256 );
2128 using Allocation for mapping (address => Allocation.State);
@@ -76,6 +83,122 @@ library AllocationHandler {
7683 address _paymentsDestination;
7784 }
7885
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+
79202 /**
80203 * @notice Create an allocation
81204 * @dev The `_allocationProof` is a 65-bytes Ethereum signed message of `keccak256(indexerAddress,allocationId)`
@@ -98,7 +221,7 @@ library AllocationHandler {
98221 mapping (bytes32 subgraphDeploymentId = > uint256 tokens ) storage _subgraphAllocatedTokens ,
99222 AllocateParams memory params
100223 ) external {
101- require (params._allocationId != address (0 ), AllocationManager. AllocationManagerInvalidZeroAllocationId ());
224+ require (params._allocationId != address (0 ), AllocationHandler. AllocationHandlerInvalidZeroAllocationId ());
102225
103226 _verifyAllocationProof (params._encodeAllocationProof, params._allocationId, params._allocationProof);
104227
@@ -124,7 +247,7 @@ library AllocationHandler {
124247 _subgraphAllocatedTokens[allocation.subgraphDeploymentId] +
125248 allocation.tokens;
126249
127- emit AllocationManager .AllocationCreated (
250+ emit AllocationHandler .AllocationCreated (
128251 params._indexer,
129252 params._allocationId,
130253 params._subgraphDeploymentId,
@@ -166,7 +289,7 @@ library AllocationHandler {
166289 PresentParams memory params
167290 ) external returns (uint256 ) {
168291 Allocation.State memory allocation = _allocations.get (params._allocationId);
169- require (allocation.isOpen (), AllocationManager. AllocationManagerAllocationClosed (params._allocationId));
292+ require (allocation.isOpen (), AllocationHandler. AllocationHandlerAllocationClosed (params._allocationId));
170293
171294 // Mint indexing rewards if all conditions are met
172295 uint256 tokensRewards = (! allocation.isStale (params.maxPOIStaleness) &&
@@ -217,7 +340,7 @@ library AllocationHandler {
217340 }
218341 }
219342
220- emit AllocationManager .IndexingRewardsCollected (
343+ emit AllocationHandler .IndexingRewardsCollected (
221344 allocation.indexer,
222345 params._allocationId,
223346 allocation.subgraphDeploymentId,
@@ -318,10 +441,10 @@ library AllocationHandler {
318441 uint32 _delegationRatio
319442 ) external {
320443 Allocation.State memory allocation = _allocations.get (_allocationId);
321- require (allocation.isOpen (), AllocationManager. AllocationManagerAllocationClosed (_allocationId));
444+ require (allocation.isOpen (), AllocationHandler. AllocationHandlerAllocationClosed (_allocationId));
322445 require (
323446 _tokens != allocation.tokens,
324- AllocationManager. AllocationManagerAllocationSameSize (_allocationId, _tokens)
447+ AllocationHandler. AllocationHandlerAllocationSameSize (_allocationId, _tokens)
325448 );
326449
327450 // Update provision tracker
@@ -355,7 +478,7 @@ library AllocationHandler {
355478 _subgraphAllocatedTokens[allocation.subgraphDeploymentId] -= (oldTokens - _tokens);
356479 }
357480
358- emit AllocationManager .AllocationResized (
481+ emit AllocationHandler .AllocationResized (
359482 allocation.indexer,
360483 _allocationId,
361484 allocation.subgraphDeploymentId,
@@ -421,7 +544,7 @@ library AllocationHandler {
421544 _subgraphAllocatedTokens[allocation.subgraphDeploymentId] -
422545 allocation.tokens;
423546
424- emit AllocationManager .AllocationClosed (
547+ emit AllocationHandler .AllocationClosed (
425548 allocation.indexer,
426549 _allocationId,
427550 allocation.subgraphDeploymentId,
@@ -463,7 +586,7 @@ library AllocationHandler {
463586 address signer = ECDSA.recover (_encodeAllocationProof, _proof);
464587 require (
465588 signer == _allocationId,
466- AllocationManager. AllocationManagerInvalidAllocationProof (signer, _allocationId)
589+ AllocationHandler. AllocationHandlerInvalidAllocationProof (signer, _allocationId)
467590 );
468591 }
469592}
0 commit comments