Skip to content
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

test: add dedicated tests for smaller scenarios #2

Merged
merged 1 commit into from
Oct 9, 2024
Merged
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
275 changes: 274 additions & 1 deletion test/RewardsStreamerMP.t.sol
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;

import { Test, console } from "forge-std/Test.sol";

Check warning on line 4 in test/RewardsStreamerMP.t.sol

View workflow job for this annotation

GitHub Actions / lint

imported name console is not used
import { RewardsStreamerMP } from "../src/RewardsStreamerMP.sol";
import { MockToken } from "./mocks/MockToken.sol";
import "forge-std/console.sol";

Check warning on line 7 in test/RewardsStreamerMP.t.sol

View workflow job for this annotation

GitHub Actions / lint

global import of path forge-std/console.sol is not allowed. Specify names to import individually or bind all exports of the module into a name (import "path" as Name)

contract RewardsStreamerMPTest is Test {
MockToken rewardToken;
Expand All @@ -17,7 +17,7 @@
address charlie = makeAddr("charlie");
address dave = makeAddr("dave");

function setUp() public {
function setUp() public virtual {
rewardToken = new MockToken("Reward Token", "RT");
stakingToken = new MockToken("Staking Token", "ST");
streamer = new RewardsStreamerMP(address(stakingToken), address(rewardToken));
Expand Down Expand Up @@ -74,6 +74,28 @@
assertEq(userInfo.userPotentialMP, p.userPotentialMP, "wrong user potential MP");
}

function _stake(address account, uint256 amount, uint256 lockupTime) public {
vm.prank(account);
streamer.stake(amount, lockupTime);
}

function _unstake(address account, uint256 amount) public {
vm.prank(account);
streamer.unstake(amount);
}

function _addReward(uint256 amount) public {
vm.prank(admin);
rewardToken.transfer(address(streamer), amount);
streamer.updateGlobalState();
}
}

contract IntegrationTest is RewardsStreamerMPTest {
function setUp() public virtual override {
super.setUp();
}

function testStake() public {
streamer.updateGlobalState();

Expand Down Expand Up @@ -411,3 +433,254 @@
);
}
}

contract StakeTest is RewardsStreamerMPTest {
function setUp() public virtual override {
super.setUp();
}

function test_StakeOneAccount() public {
// Alice stakes 10 tokens
_stake(alice, 10e18, 0);

checkStreamer(
CheckStreamerParams({
totalStaked: 10e18,
totalMP: 10e18,
potentialMP: 40e18,
stakingBalance: 10e18,
rewardBalance: 0,
rewardIndex: 0,
accountedRewards: 0
})
);
checkUser(
CheckUserParams({
user: alice,
rewardBalance: 0,
stakedBalance: 10e18,
rewardIndex: 0,
userMP: 10e18,
userPotentialMP: 40e18
})
);
}

function test_StakeOneAccountAndRewards() public {
_stake(alice, 10e18, 0);

checkStreamer(
CheckStreamerParams({
totalStaked: 10e18,
totalMP: 10e18,
potentialMP: 40e18,
stakingBalance: 10e18,
rewardBalance: 0,
rewardIndex: 0,
accountedRewards: 0
})
);

checkUser(
CheckUserParams({
user: alice,
rewardBalance: 0,
stakedBalance: 10e18,
rewardIndex: 0,
userMP: 10e18,
userPotentialMP: 40e18
})
);

// 1000 rewards generated
_addReward(1000e18);

checkStreamer(
CheckStreamerParams({
totalStaked: 10e18,
totalMP: 10e18,
potentialMP: 40e18,
stakingBalance: 10e18,
rewardBalance: 1000e18,
rewardIndex: 50e18, // (1000 rewards / (10 staked + 10 MP)) = 50
accountedRewards: 1000e18
})
);
}

function test_StakeMultipleAccounts() public {
// Alice stakes 10 tokens
_stake(alice, 10e18, 0);

// Bob stakes 30 tokens
_stake(bob, 30e18, 0);

checkStreamer(
CheckStreamerParams({
totalStaked: 40e18,
totalMP: 40e18,
potentialMP: 160e18,
stakingBalance: 40e18,
rewardBalance: 0,
rewardIndex: 0,
accountedRewards: 0
})
);

checkUser(
CheckUserParams({
user: alice,
rewardBalance: 0,
stakedBalance: 10e18,
rewardIndex: 0,
userMP: 10e18,
userPotentialMP: 40e18
})
);

checkUser(
CheckUserParams({
user: bob,
rewardBalance: 0,
stakedBalance: 30e18,
rewardIndex: 0,
userMP: 30e18,
userPotentialMP: 120e18
})
);
}

function test_StakeMultipleAccountsAndRewards() public {
// Alice stakes 10 tokens
_stake(alice, 10e18, 0);

// Bob stakes 30 tokens
_stake(bob, 30e18, 0);

checkStreamer(
CheckStreamerParams({
totalStaked: 40e18,
totalMP: 40e18,
potentialMP: 160e18,
stakingBalance: 40e18,
rewardBalance: 0,
rewardIndex: 0,
accountedRewards: 0
})
);

checkUser(
CheckUserParams({
user: alice,
rewardBalance: 0,
stakedBalance: 10e18,
rewardIndex: 0,
userMP: 10e18,
userPotentialMP: 40e18
})
);

checkUser(
CheckUserParams({
user: bob,
rewardBalance: 0,
stakedBalance: 30e18,
rewardIndex: 0,
userMP: 30e18,
userPotentialMP: 120e18
})
);
// 1000 rewards generated
_addReward(1000e18);

checkStreamer(
CheckStreamerParams({
totalStaked: 40e18,
totalMP: 40e18,
potentialMP: 160e18,
stakingBalance: 40e18,
rewardBalance: 1000e18,
rewardIndex: 125e17, // (1000 rewards / (40 staked + 40 MP)) = 12,5
accountedRewards: 1000e18
})
);
}
}

contract UnstakeTest is StakeTest {
function setUp() public override {
super.setUp();
}

function test_UnstakeOneAccount() public {
test_StakeOneAccount();

_unstake(alice, 8e18);

checkStreamer(
CheckStreamerParams({
totalStaked: 2e18,
totalMP: 2e18,
potentialMP: 8e18,
stakingBalance: 2e18,
rewardBalance: 0,
rewardIndex: 0,
accountedRewards: 0
})
);

checkUser(
CheckUserParams({
user: alice,
rewardBalance: 0,
stakedBalance: 2e18,
rewardIndex: 0,
userMP: 2e18,
userPotentialMP: 8e18
})
);

_unstake(alice, 2e18);

checkStreamer(
CheckStreamerParams({
totalStaked: 0,
totalMP: 0,
potentialMP: 0,
stakingBalance: 0,
rewardBalance: 0,
rewardIndex: 0,
accountedRewards: 0
})
);
}

function test_UnstakeOneAccountAndRewards() public {
test_StakeOneAccountAndRewards();

_unstake(alice, 8e18);

checkStreamer(
CheckStreamerParams({
totalStaked: 2e18,
totalMP: 2e18,
potentialMP: 8e18,
stakingBalance: 2e18,
rewardBalance: 0, // rewards are all paid out to alice
rewardIndex: 50e18,
accountedRewards: 0
})
);

checkUser(
CheckUserParams({
user: alice,
rewardBalance: 1000e18,
stakedBalance: 2e18,
rewardIndex: 50e18, // alice reward index has been updated
userMP: 2e18,
userPotentialMP: 8e18
})
);
}
}
Loading