Skip to content

Commit

Permalink
setup test/unit/StakingUnitTest.t.sol
Browse files Browse the repository at this point in the history
  • Loading branch information
anajuliabit committed Jun 15, 2024
1 parent 904529a commit 41e5fd6
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 6 deletions.
1 change: 1 addition & 0 deletions remappings.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@forge-std/=lib/forge-std/src/
@openzeppelin=lib/openzeppelin-contracts/
@openzeppelin-upgradeable=lib/openzeppelin-contracts-upgradeable/
@solmate=lib/solmate/src/
2 changes: 1 addition & 1 deletion src/RewardsDistribution.sol → src/RewardsDistributor.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;
pragma solidity 0.8.25;

import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";

Check warning on line 4 in src/RewardsDistributor.sol

View workflow job for this annotation

GitHub Actions / lint

imported name ERC20 is not used
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
Expand Down
10 changes: 5 additions & 5 deletions src/Staking.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;
pragma solidity 0.8.25;

import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";

Check warning on line 5 in src/Staking.sol

View workflow job for this annotation

GitHub Actions / lint

imported name ERC20 is not used
Expand Down Expand Up @@ -111,8 +111,8 @@ contract Staking is ERC20VotesUpgradeable, Ownable2StepUpgradeable {
/// @param _minStake The minimum stake amount
function initialize(
address newOwner,
IERC20 _stakingToken,
IRewardsDistributor _rewardsDistributor,
address _stakingToken,
address _rewardsDistributor,
uint256 _lockPeriod,
uint256 _minStake
) public initializer {
Expand All @@ -123,8 +123,8 @@ contract Staking is ERC20VotesUpgradeable, Ownable2StepUpgradeable {
// Transfer ownership to the DAO contract
transferOwnership(newOwner);

stakingToken = _stakingToken;
rewardsDistributor = _rewardsDistributor;
stakingToken = IERC20(_stakingToken);
rewardsDistributor = IRewardsDistributor(_rewardsDistributor);
lockPeriod = _lockPeriod;
minStake = _minStake;
}
Expand Down
37 changes: 37 additions & 0 deletions src/interfaces/IRewardsDistributor.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;

interface IRewardsDistributor {
struct RewardConfiguration {
address token; // the reward token
uint256 emissionRate; // emission per second
uint256 lastUpdate; // last update timestamp
}

function addRewardConfiguration(
address receiver,
address token,
uint256 emissionRate
) external;

function updateEmissonRate(
address receiver,
address token,
uint256 emissionRate
) external;

function distributeReward(address token) external;

function rewardConfigurations(
address receiver,
uint256 index
)
external
view
returns (address token, uint256 emissionRate, uint256 lastUpdate);

function rewardConfigurationsIds(
address receiver,
address token
) external view returns (uint256);
}
62 changes: 62 additions & 0 deletions src/interfaces/IStaking.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;

import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {IRewardsDistributor} from "./IRewardsDistributor.sol";

interface IStaking {
function initialize(
address newOwner,
IERC20 _stakingToken,
IRewardsDistributor _rewardsDistributor,
uint256 _lockPeriod,
uint256 _minStake
) external;

function stake(uint256 amount) external;

function unstake(
address keyper,
uint256 stakeIndex,
uint256 amount
) external;

function claimReward(IERC20 rewardToken, uint256 amount) external;

function harvest(address keyper) external;

function setRewardsDistributor(
IRewardsDistributor _rewardsDistributor
) external;

function setLockPeriod(uint256 _lockPeriod) external;

function setMinStake(uint256 _minStake) external;

function setKeyper(address keyper, bool isKeyper) external;

function setKeypers(address[] memory _keypers, bool isKeyper) external;

function addRewardToken(address rewardToken) external;

function convertToShares(uint256 assets) external view returns (uint256);

function convertToAssets(uint256 shares) external view returns (uint256);

function totalAssets() external view returns (uint256);

function maxWithdraw(address keyper) external view returns (uint256);

function maxClaimableRewards(
address keyper
) external view returns (uint256);

event Staked(
address indexed user,
uint256 indexed amount,
uint256 indexed shares,
uint256 lockPeriod
);
event Unstaked(address user, uint256 amount, uint256 shares);
event ClaimRewards(address user, address rewardToken, uint256 rewards);
}
15 changes: 15 additions & 0 deletions test/mocks/MockShu.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;

import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract MockShu is ERC20 {
constructor() ERC20("Shu", "SHU") {
_mint(msg.sender, 5_000_000 * 1e18);
}

function mint(address account, uint256 amount) external {
_mint(account, amount);
}
}
51 changes: 51 additions & 0 deletions test/unit/StakingUnitTest.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;

import "@forge-std/Test.sol";

import {TransparentUpgradeableProxy, ITransparentUpgradeableProxy} from "@openzeppelin/contracts/proxy/transparent/TransparentUpgradeableProxy.sol";

import {Staking} from "../../src/Staking.sol";
import {IStaking} from "../../src/interfaces/IStaking.sol";
import {RewardsDistributor} from "../../src/RewardsDistributor.sol";
import {IRewardsDistributor} from "../../src/interfaces/IRewardsDistributor.sol";

import {MockShu} from "../mocks/MockShu.sol";

contract StakingUnitTest is Test {
IStaking staking;

function setUp() public {
// deploy mock shu
MockShu shu = new MockShu();

// deploy rewards distributor
address rewardsDistributor = address(new RewardsDistributor());

TransparentUpgradeableProxy rewardsDistributionProxy = new TransparentUpgradeableProxy(
rewardsDistributor,
address(this),
abi.encodeWithSignature("initialize(address)", address(this))
);

// deploy staking
address stakingContract = address(new Staking());

address stakingProxy = address(
new TransparentUpgradeableProxy(stakingContract, address(this), "")
);

uint256 lockPeriod = 60 * 24 * 30 * 6; // 6 months
uint256 minStake = 50_000 * 1e18; // 50k

Staking(address(stakingProxy)).initialize(
address(this), // owner
address(shu),
address(rewardsDistributionProxy),
lockPeriod,
minStake
);

staking = IStaking(stakingProxy);
}
}

0 comments on commit 41e5fd6

Please sign in to comment.