Skip to content

feat: add getter for provision tracker free tokens #1102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: horizon
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,21 @@ library ProvisionTracker {
uint256 tokensAvailable = graphStaking.getTokensAvailable(serviceProvider, address(this), delegationRatio);
return self[serviceProvider] <= tokensAvailable;
}

/**
* @notice Returns the number of free tokens for a service provider. Free tokens are those currently available to lock.
* @param self The provision tracker mapping
* @param graphStaking The HorizonStaking contract
* @param serviceProvider The service provider address
* @param delegationRatio A delegation ratio to limit the amount of delegation that's usable
*/
function getTokensFree(
mapping(address => uint256) storage self,
IHorizonStaking graphStaking,
address serviceProvider,
uint32 delegationRatio
) internal view returns (uint256) {
uint256 tokensAvailable = graphStaking.getTokensAvailable(serviceProvider, address(this), delegationRatio);
return tokensAvailable > self[serviceProvider] ? tokensAvailable - self[serviceProvider] : 0;
}
}
14 changes: 14 additions & 0 deletions packages/subgraph-service/contracts/SubgraphService.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { TokenUtils } from "@graphprotocol/contracts/contracts/utils/TokenUtils.
import { PPMMath } from "@graphprotocol/horizon/contracts/libraries/PPMMath.sol";
import { Allocation } from "./libraries/Allocation.sol";
import { LegacyAllocation } from "./libraries/LegacyAllocation.sol";
import { ProvisionTracker } from "@graphprotocol/horizon/contracts/data-service/libraries/ProvisionTracker.sol";

/**
* @title SubgraphService contract
Expand All @@ -40,6 +41,7 @@ contract SubgraphService is
IRewardsIssuer,
ISubgraphService
{
using ProvisionTracker for mapping(address => uint256);
using PPMMath for uint256;
using Allocation for mapping(address => Allocation.State);
using Allocation for Allocation.State;
Expand Down Expand Up @@ -489,6 +491,14 @@ contract SubgraphService is
return _isOverAllocated(indexer, delegationRatio);
}

function getAllocationTokensFree(address indexer) external view override returns (uint256) {
return _getAllocationTokensFree(indexer, delegationRatio);
}

function getFeesTokensFree(address indexer) external view override returns (uint256) {
return feesProvisionTracker.getTokensFree(_graphStaking(), indexer, delegationRatio);
}

// -- Data service parameter getters --
/**
* @notice Getter for the accepted thawing period range for provisions
Expand Down Expand Up @@ -579,6 +589,10 @@ contract SubgraphService is
return tokensCollected;
}

/**
* @notice Set the stake to fees ratio
* @param _stakeToFeesRatio The new stake to fees ratio
*/
function _setStakeToFeesRatio(uint256 _stakeToFeesRatio) private {
require(_stakeToFeesRatio != 0, SubgraphServiceInvalidZeroStakeToFeesRatio());
stakeToFeesRatio = _stakeToFeesRatio;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,29 @@ interface ISubgraphService is IDataServiceFees {
*/
function getLegacyAllocation(address allocationId) external view returns (LegacyAllocation.State memory);

/**
* @notice Returns the number of free tokens for an indexer.
* Free tokens are those that can be immediately used to lock as economic security.
* @param indexer The address of the indexer
* @param paymentType The type of payment to consider
* @return The number of free tokens
*/
// function getTokensFree(address indexer, IGraphPayments.PaymentTypes paymentType) external view returns (uint256);

/**
* @notice Returns the number of free tokens for an indexer for indexing rewards
* @param indexer The address of the indexer
* @return The number of free tokens
*/
function getAllocationTokensFree(address indexer) external view returns (uint256);

/**
* @notice Returns the number of free tokens for an indexer for query fees
* @param indexer The address of the indexer
* @return The number of free tokens
*/
function getFeesTokensFree(address indexer) external view returns (uint256);

/**
* @notice Encodes the allocation proof for EIP712 signing
* @param indexer The address of the indexer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,16 @@ abstract contract AllocationManager is EIP712Upgradeable, GraphDirectory, Alloca
return !allocationProvisionTracker.check(_graphStaking(), _indexer, _delegationRatio);
}

/**
* @notice Returns the number of free tokens for an indexer
* @param _indexer The address of the indexer
* @param _delegationRatio The delegation ratio to consider when locking tokens
* @return The number of free tokens
*/
function _getAllocationTokensFree(address _indexer, uint32 _delegationRatio) internal view returns (uint256) {
return allocationProvisionTracker.getTokensFree(_graphStaking(), _indexer, _delegationRatio);
}

/**
* @notice Verifies ownership of an allocation id by verifying an EIP712 allocation proof
* @dev Requirements:
Expand Down
Loading