Skip to content

Commit ebb38aa

Browse files
HACK: final AllocationManager to lib - 20.785
1 parent aaec2e0 commit ebb38aa

File tree

2 files changed

+83
-35
lines changed

2 files changed

+83
-35
lines changed

packages/subgraph-service/contracts/libraries/AllocationManagerLib.sol

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,80 @@ library AllocationManagerLib {
213213
);
214214
}
215215

216+
/**
217+
* @notice Resize an allocation
218+
* @dev Will lock or release tokens in the provision tracker depending on the new allocation size.
219+
* Rewards accrued but not issued before the resize will be accounted for as pending rewards.
220+
* These will be paid out when the indexer presents a POI.
221+
*
222+
* Requirements:
223+
* - `_indexer` must be the owner of the allocation
224+
* - Allocation must be open
225+
* - `_tokens` must be different from the current allocation size
226+
*
227+
* Emits a {AllocationResized} event.
228+
*
229+
* @param _allocationId The id of the allocation to be resized
230+
* @param _tokens The new amount of tokens to allocate
231+
* @param _delegationRatio The delegation ratio to consider when locking tokens
232+
*/
233+
function resizeAllocation(
234+
mapping(address allocationId => Allocation.State allocation) storage _allocations,
235+
mapping(address indexer => uint256 tokens) storage allocationProvisionTracker,
236+
mapping(bytes32 subgraphDeploymentId => uint256 tokens) storage _subgraphAllocatedTokens,
237+
IHorizonStaking graphStaking,
238+
IRewardsManager graphRewardsManager,
239+
address _allocationId,
240+
uint256 _tokens,
241+
uint32 _delegationRatio
242+
) external {
243+
Allocation.State memory allocation = _allocations.get(_allocationId);
244+
require(allocation.isOpen(), AllocationManager.AllocationManagerAllocationClosed(_allocationId));
245+
require(
246+
_tokens != allocation.tokens,
247+
AllocationManager.AllocationManagerAllocationSameSize(_allocationId, _tokens)
248+
);
249+
250+
// Update provision tracker
251+
uint256 oldTokens = allocation.tokens;
252+
if (_tokens > oldTokens) {
253+
allocationProvisionTracker.lock(graphStaking, allocation.indexer, _tokens - oldTokens, _delegationRatio);
254+
} else {
255+
allocationProvisionTracker.release(allocation.indexer, oldTokens - _tokens);
256+
}
257+
258+
// Calculate rewards that have been accrued since the last snapshot but not yet issued
259+
uint256 accRewardsPerAllocatedToken = graphRewardsManager.onSubgraphAllocationUpdate(
260+
allocation.subgraphDeploymentId
261+
);
262+
uint256 accRewardsPerAllocatedTokenPending = !allocation.isAltruistic()
263+
? accRewardsPerAllocatedToken - allocation.accRewardsPerAllocatedToken
264+
: 0;
265+
266+
// Update the allocation
267+
_allocations[_allocationId].tokens = _tokens;
268+
_allocations[_allocationId].accRewardsPerAllocatedToken = accRewardsPerAllocatedToken;
269+
_allocations[_allocationId].accRewardsPending += graphRewardsManager.calcRewards(
270+
oldTokens,
271+
accRewardsPerAllocatedTokenPending
272+
);
273+
274+
// Update total allocated tokens for the subgraph deployment
275+
if (_tokens > oldTokens) {
276+
_subgraphAllocatedTokens[allocation.subgraphDeploymentId] += (_tokens - oldTokens);
277+
} else {
278+
_subgraphAllocatedTokens[allocation.subgraphDeploymentId] -= (oldTokens - _tokens);
279+
}
280+
281+
emit AllocationManager.AllocationResized(
282+
allocation.indexer,
283+
_allocationId,
284+
allocation.subgraphDeploymentId,
285+
_tokens,
286+
oldTokens
287+
);
288+
}
289+
216290
/**
217291
* @notice Checks if an allocation is over-allocated
218292
* @param _indexer The address of the indexer

packages/subgraph-service/contracts/utilities/AllocationManager.sol

Lines changed: 9 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -294,42 +294,16 @@ abstract contract AllocationManager is EIP712Upgradeable, GraphDirectory, Alloca
294294
* @param _delegationRatio The delegation ratio to consider when locking tokens
295295
*/
296296
function _resizeAllocation(address _allocationId, uint256 _tokens, uint32 _delegationRatio) internal {
297-
Allocation.State memory allocation = _allocations.get(_allocationId);
298-
require(allocation.isOpen(), AllocationManagerAllocationClosed(_allocationId));
299-
require(_tokens != allocation.tokens, AllocationManagerAllocationSameSize(_allocationId, _tokens));
300-
301-
// Update provision tracker
302-
uint256 oldTokens = allocation.tokens;
303-
if (_tokens > oldTokens) {
304-
allocationProvisionTracker.lock(_graphStaking(), allocation.indexer, _tokens - oldTokens, _delegationRatio);
305-
} else {
306-
allocationProvisionTracker.release(allocation.indexer, oldTokens - _tokens);
307-
}
308-
309-
// Calculate rewards that have been accrued since the last snapshot but not yet issued
310-
uint256 accRewardsPerAllocatedToken = _graphRewardsManager().onSubgraphAllocationUpdate(
311-
allocation.subgraphDeploymentId
312-
);
313-
uint256 accRewardsPerAllocatedTokenPending = !allocation.isAltruistic()
314-
? accRewardsPerAllocatedToken - allocation.accRewardsPerAllocatedToken
315-
: 0;
316-
317-
// Update the allocation
318-
_allocations[_allocationId].tokens = _tokens;
319-
_allocations[_allocationId].accRewardsPerAllocatedToken = accRewardsPerAllocatedToken;
320-
_allocations[_allocationId].accRewardsPending += _graphRewardsManager().calcRewards(
321-
oldTokens,
322-
accRewardsPerAllocatedTokenPending
297+
AllocationManagerLib.resizeAllocation(
298+
_allocations,
299+
allocationProvisionTracker,
300+
_subgraphAllocatedTokens,
301+
_graphStaking(),
302+
_graphRewardsManager(),
303+
_allocationId,
304+
_tokens,
305+
_delegationRatio
323306
);
324-
325-
// Update total allocated tokens for the subgraph deployment
326-
if (_tokens > oldTokens) {
327-
_subgraphAllocatedTokens[allocation.subgraphDeploymentId] += (_tokens - oldTokens);
328-
} else {
329-
_subgraphAllocatedTokens[allocation.subgraphDeploymentId] -= (oldTokens - _tokens);
330-
}
331-
332-
emit AllocationResized(allocation.indexer, _allocationId, allocation.subgraphDeploymentId, _tokens, oldTokens);
333307
}
334308

335309
/**

0 commit comments

Comments
 (0)