Skip to content

Commit db16dba

Browse files
committed
rewards: add tests
1 parent ee7950e commit db16dba

File tree

7 files changed

+429
-44
lines changed

7 files changed

+429
-44
lines changed

contracts/rewards/IRewardsManager.sol

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ interface IRewardsManager {
77

88
// -- Denylist --
99

10+
function setEnforcer(address _enforcer) external;
11+
1012
function setDenied(bytes32 _subgraphDeploymentID, bool _deny) external;
1113

1214
function isDenied(bytes32 _subgraphDeploymentID) external returns (bool);

contracts/rewards/RewardsManager.sol

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import "./IRewardsManager.sol";
1010
contract RewardsManager is RewardsManagerV1Storage, GraphUpgradeable, IRewardsManager, Governed {
1111
using SafeMath for uint256;
1212

13-
uint256 private constant ISSUANCE_RATE_DECIMALS = 1e18;
13+
uint256 private constant TOKEN_DECIMALS = 1e18;
1414

1515
// -- Events --
1616

@@ -113,6 +113,15 @@ contract RewardsManager is RewardsManagerV1Storage, GraphUpgradeable, IRewardsMa
113113
emit ParameterUpdated("issuanceRate");
114114
}
115115

116+
/**
117+
* @dev Sets the enforcer for denegation of rewards to subgraphs
118+
* @param _enforcer Address of the enforcer of denied subgraphs
119+
*/
120+
function setEnforcer(address _enforcer) external override onlyGovernor {
121+
enforcer = _enforcer;
122+
emit ParameterUpdated("enforcer");
123+
}
124+
116125
/**
117126
* @dev Sets the indexer as denied to claim rewards
118127
* NOTE: Can only be called by the enforcer role
@@ -165,13 +174,13 @@ contract RewardsManager is RewardsManagerV1Storage, GraphUpgradeable, IRewardsMa
165174

166175
uint256 r = issuanceRate;
167176
uint256 p = token.totalSupply();
168-
uint256 a = p.mul(_pow(r, t, ISSUANCE_RATE_DECIMALS)).div(ISSUANCE_RATE_DECIMALS);
177+
uint256 a = p.mul(_pow(r, t, TOKEN_DECIMALS)).div(TOKEN_DECIMALS);
169178

170179
// New issuance per signal during time steps
171180
uint256 x = a.sub(p);
172181

173182
// We multiply the decimals to keep the precision as fixed-point number
174-
return x.mul(ISSUANCE_RATE_DECIMALS).div(signalledTokens);
183+
return x.mul(TOKEN_DECIMALS).div(signalledTokens);
175184
}
176185

177186
/**
@@ -195,9 +204,10 @@ contract RewardsManager is RewardsManagerV1Storage, GraphUpgradeable, IRewardsMa
195204
Subgraph storage subgraph = subgraphs[_subgraphDeploymentID];
196205

197206
uint256 newAccrued = getAccRewardsPerSignal().sub(subgraph.accRewardsPerSignalSnapshot);
198-
199207
uint256 subgraphSignalledTokens = curation.getCurationPoolTokens(_subgraphDeploymentID);
200-
return subgraphSignalledTokens.mul(newAccrued).add(subgraph.accRewardsForSubgraph);
208+
209+
uint256 newValue = newAccrued.mul(subgraphSignalledTokens).div(TOKEN_DECIMALS);
210+
return subgraph.accRewardsForSubgraph.add(newValue);
201211
}
202212

203213
/**
@@ -218,10 +228,12 @@ contract RewardsManager is RewardsManagerV1Storage, GraphUpgradeable, IRewardsMa
218228
uint256 newAccrued = accRewardsForSubgraph.sub(subgraph.accRewardsForSubgraphSnapshot);
219229

220230
uint256 subgraphAllocatedTokens = staking.getSubgraphAllocatedTokens(_subgraphDeploymentID);
221-
return (
222-
subgraphAllocatedTokens.mul(newAccrued).add(subgraph.accRewardsPerAllocatedToken),
223-
accRewardsForSubgraph
224-
);
231+
if (subgraphAllocatedTokens == 0) {
232+
return (0, accRewardsForSubgraph);
233+
}
234+
235+
uint256 newValue = newAccrued.mul(TOKEN_DECIMALS).div(subgraphAllocatedTokens);
236+
return (subgraph.accRewardsPerAllocatedToken.add(newValue), accRewardsForSubgraph);
225237
}
226238

227239
/**
@@ -291,7 +303,8 @@ contract RewardsManager is RewardsManagerV1Storage, GraphUpgradeable, IRewardsMa
291303
(uint256 accRewardsPerAllocatedToken, uint256 _) = getAccRewardsPerAllocatedToken(
292304
alloc.subgraphDeploymentID
293305
);
294-
return accRewardsPerAllocatedToken.sub(alloc.accRewardsPerAllocatedToken).mul(alloc.tokens);
306+
uint256 newAccrued = accRewardsPerAllocatedToken.sub(alloc.accRewardsPerAllocatedToken);
307+
return newAccrued.mul(alloc.tokens).div(TOKEN_DECIMALS);
295308
}
296309

297310
/**
@@ -326,6 +339,7 @@ contract RewardsManager is RewardsManagerV1Storage, GraphUpgradeable, IRewardsMa
326339
address indexer = msg.sender;
327340

328341
uint256 rewards = indexerRewards[indexer];
342+
require(rewards > 0, "No rewards available for claiming");
329343
emit RewardsClaimed(indexer, rewards);
330344

331345
// Mint rewards tokens

contracts/rewards/RewardsManagerStorage.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ contract RewardsManagerV1Storage {
99

1010
struct Subgraph {
1111
uint256 accRewardsForSubgraph;
12-
uint256 accRewardsForSubgraphSnapshot; // blocks
12+
uint256 accRewardsForSubgraphSnapshot;
1313
uint256 accRewardsPerSignalSnapshot;
1414
uint256 accRewardsPerAllocatedToken;
1515
}

graph.config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ contracts:
6060
token: "${{GraphToken.address}}"
6161
curation: "${{Curation.address}}"
6262
staking: "${{Staking.address}}"
63-
issuanceRate: "1100000000000000000"
63+
issuanceRate: "1000000023206889619" # 5% annual rate
6464
proxy: true
6565
MinimumViableMultisig:
6666
init:

test/lib/deployment.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export const defaults = {
5858
initialSupply: toGRT('10000000'),
5959
},
6060
rewards: {
61-
issuanceRate: toGRT('1.00000004756468798'),
61+
issuanceRate: toGRT('1.000000023206889619'),
6262
},
6363
}
6464

test/lib/testHelpers.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ export const advanceBlockTo = async (blockNumber: string | number | BigNumber):
7070
}
7171
}
7272

73+
export const advanceBlocks = async (blocks: string | number | BigNumber) => {
74+
const steps = typeof blocks === 'number' || typeof blocks === 'string' ? toBN(blocks) : blocks
75+
const currentBlock = await latestBlock()
76+
const toBlock = currentBlock.add(steps)
77+
await advanceBlockTo(toBlock)
78+
}
79+
7380
export const advanceToNextEpoch = async (epochManager: EpochManager): Promise<void> => {
7481
const currentBlock = await latestBlock()
7582
const epochLength = await epochManager.epochLength()

0 commit comments

Comments
 (0)