Skip to content

Add a threshold above which rewards start accruing for a subgraph #528

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

Merged
merged 6 commits into from
May 26, 2022
Merged
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
Binary file not shown.
4 changes: 3 additions & 1 deletion contracts/rewards/IRewardsManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ interface IRewardsManager {
uint256 accRewardsPerAllocatedToken;
}

// -- Params --
// -- Config --

function setIssuanceRate(uint256 _issuanceRate) external;

function setMinimumSubgraphSignal(uint256 _minimumSubgraphSignal) external;

// -- Denylist --

function setSubgraphAvailabilityOracle(address _subgraphAvailabilityOracle) external;
Expand Down
50 changes: 45 additions & 5 deletions contracts/rewards/RewardsManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,17 @@ import "./IRewardsManager.sol";
* towards each subgraph. Then each Subgraph can have multiple Indexers Staked on it. Thus, the
* total rewards for the Subgraph are split up for each Indexer based on much they have Staked on
* that Subgraph.
*
* Note:
* The contract provides getter functions to query the state of accrued rewards:
* - getAccRewardsPerSignal
* - getAccRewardsForSubgraph
* - getAccRewardsPerAllocatedToken
* - getRewards
* These functions may overestimate the actual rewards due to changes in the total supply
* until the actual takeRewards function is called.
*/
contract RewardsManager is RewardsManagerV1Storage, GraphUpgradeable, IRewardsManager {
contract RewardsManager is RewardsManagerV2Storage, GraphUpgradeable, IRewardsManager {
using SafeMath for uint256;

uint256 private constant TOKEN_DECIMALS = 1e18;
Expand Down Expand Up @@ -66,6 +75,8 @@ contract RewardsManager is RewardsManagerV1Storage, GraphUpgradeable, IRewardsMa
_setIssuanceRate(_issuanceRate);
}

// -- Config --

/**
* @dev Sets the issuance rate.
* The issuance rate is defined as a percentage increase of the total supply per block.
Expand Down Expand Up @@ -105,6 +116,24 @@ contract RewardsManager is RewardsManagerV1Storage, GraphUpgradeable, IRewardsMa
emit ParameterUpdated("subgraphAvailabilityOracle");
}

/**
* @dev Sets the minimum signaled tokens on a subgraph to start accruing rewards.
* @dev Can be set to zero which means that this feature is not being used.
* @param _minimumSubgraphSignal Minimum signaled tokens
*/
function setMinimumSubgraphSignal(uint256 _minimumSubgraphSignal) external override {
// Caller can be the SAO or the governor
require(
msg.sender == address(subgraphAvailabilityOracle) ||
msg.sender == controller.getGovernor(),
"Not authorized"
);
minimumSubgraphSignal = _minimumSubgraphSignal;
emit ParameterUpdated("minimumSubgraphSignal");
}

// -- Denylist --

/**
* @dev Denies to claim rewards for a subgraph.
* NOTE: Can only be called by the subgraph availability oracle
Expand Down Expand Up @@ -150,11 +179,14 @@ contract RewardsManager is RewardsManagerV1Storage, GraphUpgradeable, IRewardsMa
/**
* @dev Tells if subgraph is in deny list
* @param _subgraphDeploymentID Subgraph deployment ID to check
* @return Whether the subgraph is denied for claiming rewards or not
*/
function isDenied(bytes32 _subgraphDeploymentID) public view override returns (bool) {
return denylist[_subgraphDeploymentID] > 0;
}

// -- Getters --

/**
* @dev Gets the issuance of rewards per signal since last updated.
*
Expand Down Expand Up @@ -205,6 +237,7 @@ contract RewardsManager is RewardsManagerV1Storage, GraphUpgradeable, IRewardsMa

/**
* @dev Gets the currently accumulated rewards per signal.
* @return Currently accumulated rewards per signal
*/
function getAccRewardsPerSignal() public view override returns (uint256) {
return accRewardsPerSignal.add(getNewRewardsPerSignal());
Expand All @@ -223,11 +256,16 @@ contract RewardsManager is RewardsManagerV1Storage, GraphUpgradeable, IRewardsMa
{
Subgraph storage subgraph = subgraphs[_subgraphDeploymentID];

uint256 newRewardsPerSignal = getAccRewardsPerSignal().sub(
subgraph.accRewardsPerSignalSnapshot
);
// Get tokens signalled on the subgraph
uint256 subgraphSignalledTokens = curation().getCurationPoolTokens(_subgraphDeploymentID);
uint256 newRewards = newRewardsPerSignal.mul(subgraphSignalledTokens).div(TOKEN_DECIMALS);

// Only accrue rewards if over a threshold
uint256 newRewards = (subgraphSignalledTokens >= minimumSubgraphSignal) // Accrue new rewards since last snapshot
? getAccRewardsPerSignal()
.sub(subgraph.accRewardsPerSignalSnapshot)
.mul(subgraphSignalledTokens)
.div(TOKEN_DECIMALS)
: 0;
return subgraph.accRewardsForSubgraph.add(newRewards);
}

Expand Down Expand Up @@ -266,6 +304,8 @@ contract RewardsManager is RewardsManagerV1Storage, GraphUpgradeable, IRewardsMa
);
}

// -- Updates --

/**
* @dev Updates the accumulated rewards per signal and save checkpoint block number.
* Must be called before `issuanceRate` or `total signalled GRT` changes
Expand Down
5 changes: 5 additions & 0 deletions contracts/rewards/RewardsManagerStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,8 @@ contract RewardsManagerV1Storage is Managed {
// Subgraph denylist : subgraph deployment ID => block when added or zero (if not denied)
mapping(bytes32 => uint256) public denylist;
}

contract RewardsManagerV2Storage is RewardsManagerV1Storage {
// Minimum amount of signaled tokens on a subgraph required to accrue rewards
uint256 public minimumSubgraphSignal;
}