Skip to content

Commit b66db5f

Browse files
committed
feat: add getter for provision tracker free tokens
Signed-off-by: Tomás Migone <[email protected]>
1 parent 8f922a1 commit b66db5f

File tree

4 files changed

+64
-0
lines changed

4 files changed

+64
-0
lines changed

packages/horizon/contracts/data-service/libraries/ProvisionTracker.sol

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,21 @@ library ProvisionTracker {
7272
uint256 tokensAvailable = graphStaking.getTokensAvailable(serviceProvider, address(this), delegationRatio);
7373
return self[serviceProvider] <= tokensAvailable;
7474
}
75+
76+
/**
77+
* @notice Returns the number of free tokens for a service provider. Free tokens are those currently available to lock.
78+
* @param self The provision tracker mapping
79+
* @param graphStaking The HorizonStaking contract
80+
* @param serviceProvider The service provider address
81+
* @param delegationRatio A delegation ratio to limit the amount of delegation that's usable
82+
*/
83+
function getTokensFree(
84+
mapping(address => uint256) storage self,
85+
IHorizonStaking graphStaking,
86+
address serviceProvider,
87+
uint32 delegationRatio
88+
) internal view returns (uint256) {
89+
uint256 tokensAvailable = graphStaking.getTokensAvailable(serviceProvider, address(this), delegationRatio);
90+
return tokensAvailable > self[serviceProvider] ? tokensAvailable - self[serviceProvider] : 0;
91+
}
7592
}

packages/subgraph-service/contracts/SubgraphService.sol

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { TokenUtils } from "@graphprotocol/contracts/contracts/utils/TokenUtils.
2121
import { PPMMath } from "@graphprotocol/horizon/contracts/libraries/PPMMath.sol";
2222
import { Allocation } from "./libraries/Allocation.sol";
2323
import { LegacyAllocation } from "./libraries/LegacyAllocation.sol";
24+
import { ProvisionTracker } from "@graphprotocol/horizon/contracts/data-service/libraries/ProvisionTracker.sol";
2425

2526
/**
2627
* @title SubgraphService contract
@@ -40,6 +41,7 @@ contract SubgraphService is
4041
IRewardsIssuer,
4142
ISubgraphService
4243
{
44+
using ProvisionTracker for mapping(address => uint256);
4345
using PPMMath for uint256;
4446
using Allocation for mapping(address => Allocation.State);
4547
using Allocation for Allocation.State;
@@ -489,6 +491,14 @@ contract SubgraphService is
489491
return _isOverAllocated(indexer, delegationRatio);
490492
}
491493

494+
function getAllocationTokensFree(address indexer) external view override returns (uint256) {
495+
return _getAllocationTokensFree(indexer, delegationRatio);
496+
}
497+
498+
function getFeesTokensFree(address indexer) external view override returns (uint256) {
499+
return feesProvisionTracker.getTokensFree(_graphStaking(), indexer, delegationRatio);
500+
}
501+
492502
// -- Data service parameter getters --
493503
/**
494504
* @notice Getter for the accepted thawing period range for provisions
@@ -579,6 +589,10 @@ contract SubgraphService is
579589
return tokensCollected;
580590
}
581591

592+
/**
593+
* @notice Set the stake to fees ratio
594+
* @param _stakeToFeesRatio The new stake to fees ratio
595+
*/
582596
function _setStakeToFeesRatio(uint256 _stakeToFeesRatio) private {
583597
require(_stakeToFeesRatio != 0, SubgraphServiceInvalidZeroStakeToFeesRatio());
584598
stakeToFeesRatio = _stakeToFeesRatio;

packages/subgraph-service/contracts/interfaces/ISubgraphService.sol

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,29 @@ interface ISubgraphService is IDataServiceFees {
228228
*/
229229
function getLegacyAllocation(address allocationId) external view returns (LegacyAllocation.State memory);
230230

231+
/**
232+
* @notice Returns the number of free tokens for an indexer.
233+
* Free tokens are those that can be immediately used to lock as economic security.
234+
* @param indexer The address of the indexer
235+
* @param paymentType The type of payment to consider
236+
* @return The number of free tokens
237+
*/
238+
// function getTokensFree(address indexer, IGraphPayments.PaymentTypes paymentType) external view returns (uint256);
239+
240+
/**
241+
* @notice Returns the number of free tokens for an indexer for indexing rewards
242+
* @param indexer The address of the indexer
243+
* @return The number of free tokens
244+
*/
245+
function getAllocationTokensFree(address indexer) external view returns (uint256);
246+
247+
/**
248+
* @notice Returns the number of free tokens for an indexer for query fees
249+
* @param indexer The address of the indexer
250+
* @return The number of free tokens
251+
*/
252+
function getFeesTokensFree(address indexer) external view returns (uint256);
253+
231254
/**
232255
* @notice Encodes the allocation proof for EIP712 signing
233256
* @param indexer The address of the indexer

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,16 @@ abstract contract AllocationManager is EIP712Upgradeable, GraphDirectory, Alloca
476476
return !allocationProvisionTracker.check(_graphStaking(), _indexer, _delegationRatio);
477477
}
478478

479+
/**
480+
* @notice Returns the number of free tokens for an indexer
481+
* @param _indexer The address of the indexer
482+
* @param _delegationRatio The delegation ratio to consider when locking tokens
483+
* @return The number of free tokens
484+
*/
485+
function _getAllocationTokensFree(address _indexer, uint32 _delegationRatio) internal view returns (uint256) {
486+
return allocationProvisionTracker.getTokensFree(_graphStaking(), _indexer, _delegationRatio);
487+
}
488+
479489
/**
480490
* @notice Verifies ownership of an allocation id by verifying an EIP712 allocation proof
481491
* @dev Requirements:

0 commit comments

Comments
 (0)