Skip to content

Commit

Permalink
test(RewardsStreamerMP): ensure bonusMP and maxMP are decreased
Browse files Browse the repository at this point in the history
correctly at unstake

This commit adds some tests to check that, if a user (partically)
unstakes their funds, their initial and bonus MP get decreased
proportionally, as well as their max mp.

Closes #46
  • Loading branch information
0x-r4bbit committed Oct 10, 2024
1 parent a66dd7e commit 6c12715
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 22 deletions.
34 changes: 17 additions & 17 deletions .gas-report
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,31 @@
| Deployment Cost | Deployment Size | | | | |
| 1111690 | 5010 | | | | |
| Function Name | min | avg | median | max | # calls |
| MAX_LOCKING_PERIOD | 228 | 228 | 228 | 228 | 18 |
| MAX_MULTIPLIER | 252 | 252 | 252 | 252 | 34 |
| MIN_LOCKING_PERIOD | 229 | 229 | 229 | 229 | 8 |
| MAX_LOCKING_PERIOD | 228 | 228 | 228 | 228 | 21 |
| MAX_MULTIPLIER | 252 | 252 | 252 | 252 | 40 |
| MIN_LOCKING_PERIOD | 229 | 229 | 229 | 229 | 10 |
| MP_RATE_PER_YEAR | 231 | 231 | 231 | 231 | 2 |
| SCALE_FACTOR | 251 | 251 | 251 | 251 | 30 |
| accountedRewards | 373 | 921 | 373 | 2373 | 62 |
| getUserInfo | 1576 | 1576 | 1576 | 1576 | 61 |
| rewardIndex | 373 | 405 | 373 | 2373 | 62 |
| stake | 167787 | 215016 | 228586 | 249238 | 43 |
| totalMP | 330 | 330 | 330 | 330 | 62 |
| totalMaxMP | 329 | 329 | 329 | 329 | 62 |
| totalStaked | 373 | 373 | 373 | 373 | 62 |
| unstake | 75511 | 107624 | 110519 | 134250 | 10 |
| updateGlobalState | 30008 | 56733 | 47387 | 80335 | 22 |
| updateUserMP | 34631 | 36857 | 37133 | 37431 | 16 |
| SCALE_FACTOR | 251 | 251 | 251 | 251 | 36 |
| accountedRewards | 373 | 931 | 373 | 2373 | 68 |
| getUserInfo | 1576 | 1576 | 1576 | 1576 | 62 |
| rewardIndex | 373 | 402 | 373 | 2373 | 68 |
| stake | 167787 | 216078 | 228586 | 249238 | 45 |
| totalMP | 330 | 330 | 330 | 330 | 68 |
| totalMaxMP | 329 | 329 | 329 | 329 | 68 |
| totalStaked | 373 | 373 | 373 | 373 | 68 |
| unstake | 75511 | 104952 | 91588 | 134250 | 12 |
| updateGlobalState | 30008 | 55954 | 47387 | 80335 | 24 |
| updateUserMP | 34631 | 36888 | 37133 | 37431 | 18 |


| test/mocks/MockToken.sol:MockToken contract | | | | | |
|---------------------------------------------|-----------------|-------|--------|-------|---------|
| Deployment Cost | Deployment Size | | | | |
| 639406 | 3369 | | | | |
| Function Name | min | avg | median | max | # calls |
| approve | 46346 | 46346 | 46346 | 46346 | 150 |
| balanceOf | 561 | 1321 | 561 | 2561 | 271 |
| mint | 51284 | 58124 | 51284 | 68384 | 150 |
| approve | 46346 | 46346 | 46346 | 46346 | 160 |
| balanceOf | 561 | 1324 | 561 | 2561 | 288 |
| mint | 51284 | 58124 | 51284 | 68384 | 160 |
| transfer | 34390 | 48070 | 51490 | 51490 | 10 |


Expand Down
12 changes: 7 additions & 5 deletions .gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,19 @@ StakeTest:test_StakeOneAccountWithMinLockUp() (gas: 284055)
StakeTest:test_StakeOneAccountWithRandomLockUp() (gas: 284099)
UnstakeTest:test_StakeMultipleAccounts() (gas: 438822)
UnstakeTest:test_StakeMultipleAccountsAndRewards() (gas: 586066)
UnstakeTest:test_StakeMultipleAccountsMPIncreasesMaxMPDoesNotChange() (gas: 743414)
UnstakeTest:test_StakeMultipleAccountsWithMinLockUp() (gas: 449118)
UnstakeTest:test_StakeMultipleAccountsMPIncreasesMaxMPDoesNotChange() (gas: 743436)
UnstakeTest:test_StakeMultipleAccountsWithMinLockUp() (gas: 449140)
UnstakeTest:test_StakeMultipleAccountsWithRandomLockUp() (gas: 470688)
UnstakeTest:test_StakeOneAccount() (gas: 267816)
UnstakeTest:test_StakeOneAccountAndRewards() (gas: 415080)
UnstakeTest:test_StakeOneAccountMPIncreasesMaxMPDoesNotChange() (gas: 472851)
UnstakeTest:test_StakeOneAccountReachingMPLimit() (gas: 468459)
UnstakeTest:test_StakeOneAccountWithMaxLockUp() (gas: 284068)
UnstakeTest:test_StakeOneAccountWithMinLockUp() (gas: 284055)
UnstakeTest:test_StakeOneAccountWithRandomLockUp() (gas: 284144)
UnstakeTest:test_StakeOneAccountWithRandomLockUp() (gas: 284121)
UnstakeTest:test_UnstakeMultipleAccounts() (gas: 616415)
UnstakeTest:test_UnstakeMultipleAccountsAndRewards() (gas: 937787)
UnstakeTest:test_UnstakeOneAccount() (gas: 446390)
UnstakeTest:test_UnstakeOneAccountAndRewards() (gas: 557241)
UnstakeTest:test_UnstakeOneAccount() (gas: 446348)
UnstakeTest:test_UnstakeOneAccountAndAccruedMP() (gas: 467641)
UnstakeTest:test_UnstakeOneAccountAndRewards() (gas: 557241)
UnstakeTest:test_UnstakeOneAccountWithLockUpAndAccruedMP() (gas: 493412)
83 changes: 83 additions & 0 deletions test/RewardsStreamerMP.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1102,6 +1102,89 @@ contract UnstakeTest is StakeTest {
);
}

function test_UnstakeOneAccountAndAccruedMP() public {
test_StakeOneAccount();

// wait for 1 year
uint256 currentTime = vm.getBlockTimestamp();
vm.warp(currentTime + (365 days));

streamer.updateGlobalState();
streamer.updateUserMP(alice);

checkStreamer(
CheckStreamerParams({
totalStaked: 10e18,
totalMP: 20e18, // total MP must have been doubled
totalMaxMP: 40e18,
stakingBalance: 10e18,
rewardBalance: 0,
rewardIndex: 0,
accountedRewards: 0
})
);

// unstake half of the tokens
_unstake(alice, 5e18);

checkStreamer(
CheckStreamerParams({
totalStaked: 5e18, // 10 - 5
totalMP: 10e18, // 20 - 10 (5 initial + 5 accrued)
totalMaxMP: 20e18,
stakingBalance: 5e18,
rewardBalance: 0,
rewardIndex: 0,
accountedRewards: 0
})
);
}

function test_UnstakeOneAccountWithLockUpAndAccruedMP() public {
test_StakeOneAccountWithMinLockUp();

uint256 stakeAmount = 10e18;
uint256 lockUpPeriod = streamer.MIN_LOCKING_PERIOD();
// 10e18 is what's used in `test_StakeOneAccountWithMinLockUp`
uint256 expectedBonusMP = _calculateBonusMP(stakeAmount, lockUpPeriod);

// wait for 1 year
uint256 currentTime = vm.getBlockTimestamp();
vm.warp(currentTime + (365 days));

streamer.updateGlobalState();
streamer.updateUserMP(alice);

checkStreamer(
CheckStreamerParams({
totalStaked: stakeAmount,
totalMP: (stakeAmount + expectedBonusMP) + stakeAmount, // we do `+ stakeAmount` we've accrued
// `stakeAmount` after 1 year
totalMaxMP: stakeAmount * streamer.MAX_MULTIPLIER() + expectedBonusMP,
stakingBalance: 10e18,
rewardBalance: 0,
rewardIndex: 0,
accountedRewards: 0
})
);

// unstake half of the tokens
_unstake(alice, 5e18);
expectedBonusMP = _calculateBonusMP(5e18, lockUpPeriod);

checkStreamer(
CheckStreamerParams({
totalStaked: 5e18,
totalMP: (5e18 + expectedBonusMP) + 5e18,
totalMaxMP: 5e18 * streamer.MAX_MULTIPLIER() + expectedBonusMP,
stakingBalance: 5e18,
rewardBalance: 0,
rewardIndex: 0,
accountedRewards: 0
})
);
}

function test_UnstakeOneAccountAndRewards() public {
test_StakeOneAccountAndRewards();

Expand Down

0 comments on commit 6c12715

Please sign in to comment.