Skip to content

Commit fbd95e5

Browse files
committed
feat: calculateReward to return 0 if claimed
1 parent cea4bca commit fbd95e5

4 files changed

Lines changed: 35 additions & 10 deletions

File tree

src/Hyperfund.sol

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,11 @@ contract Hyperfund is AccessControlUpgradeable, PausableUpgradeable, UUPSUpgrade
112112
/// @notice Issue a Hypercert fraction for a non-financial contribution, only callable by a manager
113113
/// @param _contributor address of the contributor to receive the Hypercert fraction
114114
/// @param _units amount of units to register as a non-financial contribution
115-
function nonfinancialContribution(address _contributor, uint256 _units) external whenNotPaused onlyRole(MANAGER_ROLE) {
115+
function nonfinancialContribution(address _contributor, uint256 _units)
116+
external
117+
whenNotPaused
118+
onlyRole(MANAGER_ROLE)
119+
{
116120
uint256 availableSupply = hypercertMinter.unitsOf(hypercertId);
117121
require(availableSupply >= _units, AmountExceedsAvailableSupply(availableSupply));
118122
_nonfinancialContribution(_contributor, _units);

src/Hyperstaker.sol

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ contract Hyperstaker is AccessControlUpgradeable, PausableUpgradeable, UUPSUpgra
162162
address staker = stakes[_hypercertId].staker;
163163
require(staker == msg.sender, NotStakerOfHypercert(staker));
164164
require(!isRoundClaimed(_hypercertId, _roundId), AlreadyClaimed());
165-
uint256 reward = calculateReward(_hypercertId, _roundId);
165+
uint256 reward = _calculateReward(_hypercertId, _roundId);
166166
require(reward != 0, NoRewardAvailable());
167167

168168
_setRoundClaimed(_hypercertId, _roundId);
@@ -184,14 +184,10 @@ contract Hyperstaker is AccessControlUpgradeable, PausableUpgradeable, UUPSUpgra
184184
/// @param _roundId id of the round to calculate the reward for
185185
/// @return amount of the reward eligable for the staked Hypercert for the given round
186186
function calculateReward(uint256 _hypercertId, uint256 _roundId) public view returns (uint256) {
187-
Round memory round = rounds[_roundId];
188-
require(round.endTime != 0, RoundNotSet());
189-
uint256 stakeStartTime = stakes[_hypercertId].stakingStartTime;
190-
require(stakeStartTime != 0, NotStaked());
191-
stakeStartTime = stakeStartTime < round.startTime ? round.startTime : stakeStartTime;
192-
uint256 stakeDuration = stakeStartTime > round.endTime ? 0 : round.endTime - stakeStartTime;
193-
return
194-
round.totalRewards * hypercertMinter.unitsOf(_hypercertId) * stakeDuration / (totalUnits * round.duration);
187+
if (isRoundClaimed(_hypercertId, _roundId)) {
188+
return 0;
189+
}
190+
return _calculateReward(_hypercertId, _roundId);
195191
}
196192

197193
/// @notice Check if a staked Hypercert had already claimed a reward for a given round
@@ -218,6 +214,21 @@ contract Hyperstaker is AccessControlUpgradeable, PausableUpgradeable, UUPSUpgra
218214

219215
// INTERNAL FUNCTIONS
220216

217+
/// @notice Calculate the reward for a staked Hypercert for a given round
218+
/// @param _hypercertId id of the Hypercert to calculate the reward for
219+
/// @param _roundId id of the round to calculate the reward for
220+
/// @return amount of the reward eligable for the staked Hypercert for the given round
221+
function _calculateReward(uint256 _hypercertId, uint256 _roundId) internal view returns (uint256) {
222+
Round memory round = rounds[_roundId];
223+
require(round.endTime != 0, RoundNotSet());
224+
uint256 stakeStartTime = stakes[_hypercertId].stakingStartTime;
225+
require(stakeStartTime != 0, NotStaked());
226+
stakeStartTime = stakeStartTime < round.startTime ? round.startTime : stakeStartTime;
227+
uint256 stakeDuration = stakeStartTime > round.endTime ? 0 : round.endTime - stakeStartTime;
228+
return
229+
round.totalRewards * hypercertMinter.unitsOf(_hypercertId) * stakeDuration / (totalUnits * round.duration);
230+
}
231+
221232
/// @notice Get the hypercert type id for a given hypercert id
222233
/// @param _hypercertId id of the Hypercert to get the type id for
223234
/// @return hypercert type id for the given hypercert id

test/Hyperfund.t.sol

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,8 @@ contract HyperfundTest is Test {
364364
}
365365

366366
function test_RevertWhen_RedeemNotFraction() public {
367+
vm.prank(manager);
368+
hyperfund.nonfinancialContribution(contributor, amount);
367369
vm.startPrank(contributor);
368370
vm.recordLogs();
369371
hypercertMinter.mintClaim(contributor, totalUnits, "uri", IHypercertToken.TransferRestrictions.AllowAll);

test/Hyperstaker.t.sol

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,14 @@ contract HyperstakerTest is Test {
225225
assertEq(hyperstaker.calculateReward(stakerHypercertId, 0), 0);
226226
}
227227

228+
function test_CalculateReward_0WhenRoundClaimed() public {
229+
_setupStake();
230+
_setupRewardEth();
231+
vm.prank(staker);
232+
hyperstaker.claimReward(stakerHypercertId, 0);
233+
assertEq(hyperstaker.calculateReward(stakerHypercertId, 0), 0);
234+
}
235+
228236
function test_RevertWhen_CalculateRewardNoReward() public {
229237
_setupStake();
230238
vm.expectRevert(RoundNotSet.selector);

0 commit comments

Comments
 (0)