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

[PDE-14] update staking tests with 100% test coverage #89

Merged
merged 1 commit into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions staking/script/DeployStakingContracts.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ contract DeployStakingContracts is Script {

RWAStaking rwaStaking = new RWAStaking();
PlumePreStaking plumePreStaking = new PlumePreStaking(
address(rwaStaking), abi.encodeCall(RWAStaking.initialize, (timelock, DEPLOYER_ADDRESS))
address(rwaStaking), abi.encodeCall(RWAStaking.initialize, (timelock, MULTISIG_ADDRESS))
);
RWAStaking(address(plumePreStaking)).allowStablecoin(IERC20(USDC_ADDRESS));
RWAStaking(address(plumePreStaking)).allowStablecoin(IERC20(USDT_ADDRESS));
Expand All @@ -45,7 +45,7 @@ contract DeployStakingContracts is Script {
PlumePreReserveFund plumePreReserveFund = new PlumePreReserveFund(
address(sbtcStaking),
abi.encodeCall(
ReserveStaking.initialize, (timelock, DEPLOYER_ADDRESS, IERC20(SBTC_ADDRESS), IERC20(STONE_ADDRESS))
ReserveStaking.initialize, (timelock, MULTISIG_ADDRESS, IERC20(SBTC_ADDRESS), IERC20(STONE_ADDRESS))
)
);
console2.log("Plume Pre-Reserve Fund Proxy deployed to:", address(plumePreReserveFund));
Expand Down
49 changes: 44 additions & 5 deletions staking/test/RWAStaking.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { IAccessControl } from "@openzeppelin/contracts/access/IAccessControl.so
import { TimelockController } from "@openzeppelin/contracts/governance/TimelockController.sol";
import { IERC20Errors } from "@openzeppelin/contracts/interfaces/draft-IERC6093.sol";
import { ERC1967Proxy } from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";

import { ERC20 } from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { Test } from "forge-std/Test.sol";
Expand All @@ -17,8 +16,7 @@ import { PlumePreStaking } from "../src/proxy/PlumePreStaking.sol";
contract MockPlumePreStaking is PlumePreStaking {

constructor(address logic, bytes memory data) PlumePreStaking(logic, data) { }

function test() public { }
function test() public override { }

function exposed_implementation() public view returns (address) {
return _implementation();
Expand Down Expand Up @@ -67,6 +65,8 @@ contract RWAStakingTest is Test {
uint256 constant INITIAL_BALANCE = 1000 ether;

function setUp() public {
vm.startPrank(owner);

MockERC20 usdcMock = new MockERC20(18);
usdcMock.mint(user1, INITIAL_BALANCE);
usdcMock.mint(user2, INITIAL_BALANCE);
Expand All @@ -77,6 +77,8 @@ contract RWAStakingTest is Test {
pusdMock.mint(user2, INITIAL_BALANCE);
pusd = IERC20(pusdMock);

vm.stopPrank();

address[] memory proposers = new address[](1);
address[] memory executors = new address[](1);
proposers[0] = owner;
Expand All @@ -95,14 +97,18 @@ contract RWAStakingTest is Test {

function helper_initialStake(address user, uint256 stakeAmount) public {
vm.startPrank(owner);

rwaStaking.allowStablecoin(usdc);

vm.stopPrank();

vm.startPrank(user);

usdc.approve(address(rwaStaking), stakeAmount);
vm.expectEmit(true, true, false, true, address(rwaStaking));
emit RWAStaking.Staked(user, usdc, stakeAmount);
rwaStaking.stake(stakeAmount, usdc);

vm.stopPrank();

assertEq(usdc.balanceOf(address(rwaStaking)), stakeAmount);
Expand Down Expand Up @@ -146,6 +152,39 @@ contract RWAStakingTest is Test {
assertEq(pusd.balanceOf(user2), INITIAL_BALANCE);
}

function test_reinitializeFail() public {
vm.startPrank(user1);

vm.expectRevert(
abi.encodeWithSelector(
IAccessControl.AccessControlUnauthorizedAccount.selector, user1, rwaStaking.ADMIN_ROLE()
)
);
rwaStaking.reinitialize(user1, timelock);

vm.stopPrank();

vm.startPrank(owner);

rwaStaking.reinitialize(owner, timelock);
vm.expectRevert(abi.encodeWithSelector(Initializable.InvalidInitialization.selector));
rwaStaking.reinitialize(user1, timelock);

vm.stopPrank();
}

function test_reinitialize() public {
vm.startPrank(owner);

assertEq(rwaStaking.getMultisig(), owner);
assertEq(address(rwaStaking.getTimelock()), address(timelock));
rwaStaking.reinitialize(user1, TimelockController(payable(user2)));
assertEq(rwaStaking.getMultisig(), user1);
assertEq(address(rwaStaking.getTimelock()), address(user2));

vm.stopPrank();
}

function test_stakingEnded() public {
vm.startPrank(address(timelock));
rwaStaking.adminWithdraw();
Expand Down Expand Up @@ -201,11 +240,11 @@ contract RWAStakingTest is Test {
IERC20 tooManyDecimalsToken = IERC20(tooManyDecimalsMock);

vm.startPrank(owner);

// Attempt to allow the token with too many decimals
vm.expectRevert(abi.encodeWithSelector(RWAStaking.TooManyDecimals.selector));
rwaStaking.allowStablecoin(tooManyDecimalsToken);

vm.stopPrank();

// Verify the token was not added
Expand Down
38 changes: 36 additions & 2 deletions staking/test/ReserveStaking.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ pragma solidity ^0.8.25;

import { Initializable } from "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import { IAccessControl } from "@openzeppelin/contracts/access/IAccessControl.sol";

import { TimelockController } from "@openzeppelin/contracts/governance/TimelockController.sol";
import { IERC20Errors } from "@openzeppelin/contracts/interfaces/draft-IERC6093.sol";
import { ERC20Mock } from "@openzeppelin/contracts/mocks/token/ERC20Mock.sol";
Expand All @@ -19,7 +18,7 @@ import { PlumePreReserveFund } from "../src/proxy/PlumePreReserveFund.sol";
contract MockPlumePreReserveFund is PlumePreReserveFund {

constructor(address logic, bytes memory data) PlumePreReserveFund(logic, data) { }
function test() public { }
function test() public override { }

function exposed_implementation() public view returns (address) {
return _implementation();
Expand Down Expand Up @@ -73,11 +72,13 @@ contract ReserveStakingTest is Test {

function helper_initialStake(address user, uint256 sbtcAmount, uint256 stoneAmount) public {
vm.startPrank(user);

sbtc.approve(address(staking), sbtcAmount);
stone.approve(address(staking), stoneAmount);
vm.expectEmit(true, false, false, true, address(staking));
emit ReserveStaking.Staked(user, sbtcAmount, stoneAmount);
staking.stake(sbtcAmount, stoneAmount);

vm.stopPrank();

assertEq(sbtc.balanceOf(address(staking)), sbtcAmount);
Expand Down Expand Up @@ -146,6 +147,39 @@ contract ReserveStakingTest is Test {
assertEq(stone.balanceOf(user2), INITIAL_BALANCE);
}

function test_reinitializeFail() public {
vm.startPrank(user1);

vm.expectRevert(
abi.encodeWithSelector(
IAccessControl.AccessControlUnauthorizedAccount.selector, user1, staking.ADMIN_ROLE()
)
);
staking.reinitialize(user1, timelock);

vm.stopPrank();

vm.startPrank(owner);

staking.reinitialize(owner, timelock);
vm.expectRevert(abi.encodeWithSelector(Initializable.InvalidInitialization.selector));
staking.reinitialize(user1, timelock);

vm.stopPrank();
}

function test_reinitialize() public {
vm.startPrank(owner);

assertEq(staking.getMultisig(), owner);
assertEq(address(staking.getTimelock()), address(timelock));
staking.reinitialize(user1, TimelockController(payable(user2)));
assertEq(staking.getMultisig(), user1);
assertEq(address(staking.getTimelock()), address(user2));

vm.stopPrank();
}

function test_stakingEnded() public {
vm.startPrank(address(timelock));
staking.adminWithdraw();
Expand Down