Skip to content

Commit 7e9adca

Browse files
authored
Accumulated subgraph rewards reset to zero on edge case (#452)
Avoid resetting acc rewards for a subgraph when it reaches zero signal
1 parent 478339e commit 7e9adca

File tree

3 files changed

+1952
-2336
lines changed

3 files changed

+1952
-2336
lines changed

contracts/rewards/RewardsManager.sol

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,10 @@ contract RewardsManager is RewardsManagerV1Storage, GraphUpgradeable, IRewardsMa
187187
uint256 p = graphToken.totalSupply();
188188
uint256 a = p.mul(_pow(r, t, TOKEN_DECIMALS)).div(TOKEN_DECIMALS);
189189

190-
// New issuance per signalled token during time steps
190+
// New issuance of tokens during time steps
191191
uint256 x = a.sub(p);
192192

193+
// Get the new issuance per signalled token
193194
// We multiply the decimals to keep the precision as fixed-point number
194195
return x.mul(TOKEN_DECIMALS).div(signalledTokens);
195196
}
@@ -214,14 +215,12 @@ contract RewardsManager is RewardsManagerV1Storage, GraphUpgradeable, IRewardsMa
214215
{
215216
Subgraph storage subgraph = subgraphs[_subgraphDeploymentID];
216217

217-
uint256 newAccrued = getAccRewardsPerSignal().sub(subgraph.accRewardsPerSignalSnapshot);
218+
uint256 newRewardsPerSignal = getAccRewardsPerSignal().sub(
219+
subgraph.accRewardsPerSignalSnapshot
220+
);
218221
uint256 subgraphSignalledTokens = curation().getCurationPoolTokens(_subgraphDeploymentID);
219-
if (subgraphSignalledTokens == 0) {
220-
return 0;
221-
}
222-
223-
uint256 newValue = newAccrued.mul(subgraphSignalledTokens).div(TOKEN_DECIMALS);
224-
return subgraph.accRewardsForSubgraph.add(newValue);
222+
uint256 newRewards = newRewardsPerSignal.mul(subgraphSignalledTokens).div(TOKEN_DECIMALS);
223+
return subgraph.accRewardsForSubgraph.add(newRewards);
225224
}
226225

227226
/**
@@ -239,7 +238,9 @@ contract RewardsManager is RewardsManagerV1Storage, GraphUpgradeable, IRewardsMa
239238
Subgraph storage subgraph = subgraphs[_subgraphDeploymentID];
240239

241240
uint256 accRewardsForSubgraph = getAccRewardsForSubgraph(_subgraphDeploymentID);
242-
uint256 newAccrued = accRewardsForSubgraph.sub(subgraph.accRewardsForSubgraphSnapshot);
241+
uint256 newRewardsForSubgraph = accRewardsForSubgraph.sub(
242+
subgraph.accRewardsForSubgraphSnapshot
243+
);
243244

244245
uint256 subgraphAllocatedTokens = staking().getSubgraphAllocatedTokens(
245246
_subgraphDeploymentID
@@ -248,8 +249,13 @@ contract RewardsManager is RewardsManagerV1Storage, GraphUpgradeable, IRewardsMa
248249
return (0, accRewardsForSubgraph);
249250
}
250251

251-
uint256 newValue = newAccrued.mul(TOKEN_DECIMALS).div(subgraphAllocatedTokens);
252-
return (subgraph.accRewardsPerAllocatedToken.add(newValue), accRewardsForSubgraph);
252+
uint256 newRewardsPerAllocatedToken = newRewardsForSubgraph.mul(TOKEN_DECIMALS).div(
253+
subgraphAllocatedTokens
254+
);
255+
return (
256+
subgraph.accRewardsPerAllocatedToken.add(newRewardsPerAllocatedToken),
257+
accRewardsForSubgraph
258+
);
253259
}
254260

255261
/**
@@ -356,9 +362,7 @@ contract RewardsManager is RewardsManagerV1Storage, GraphUpgradeable, IRewardsMa
356362
IStaking staking = staking();
357363
require(msg.sender == address(staking), "Caller must be the staking contract");
358364

359-
IGraphToken graphToken = graphToken();
360365
IStaking.Allocation memory alloc = staking.getAllocation(_allocationID);
361-
362366
uint256 accRewardsPerAllocatedToken = onSubgraphAllocationUpdate(
363367
alloc.subgraphDeploymentID
364368
);
@@ -375,11 +379,12 @@ contract RewardsManager is RewardsManagerV1Storage, GraphUpgradeable, IRewardsMa
375379
alloc.accRewardsPerAllocatedToken,
376380
accRewardsPerAllocatedToken
377381
);
378-
379-
// Mint directly to staking contract for the reward amount
380-
// The staking contract will do bookkeeping of the reward and
381-
// assign in proportion to each stakeholder incentive
382-
graphToken.mint(address(staking), rewards);
382+
if (rewards > 0) {
383+
// Mint directly to staking contract for the reward amount
384+
// The staking contract will do bookkeeping of the reward and
385+
// assign in proportion to each stakeholder incentive
386+
graphToken().mint(address(staking), rewards);
387+
}
383388

384389
emit RewardsAssigned(alloc.indexer, _allocationID, alloc.closedAtEpoch, rewards);
385390

0 commit comments

Comments
 (0)