From 4208fef603d5576d85b686340d2fce1541527128 Mon Sep 17 00:00:00 2001 From: gab0071 Date: Wed, 21 Aug 2024 11:38:35 -0500 Subject: [PATCH] starting with some tests --- Makefile | 31 +++++++++++ src/Main.sol | 3 ++ test/MainTest.t.sol | 101 ++++++++++++++++++++++++++++++++++++ test/RewardTokenTest.t.sol | 19 +++++++ test/StakingTokenTest.t.sol | 18 +++++++ 5 files changed, 172 insertions(+) create mode 100644 test/MainTest.t.sol create mode 100644 test/RewardTokenTest.t.sol create mode 100644 test/StakingTokenTest.t.sol diff --git a/Makefile b/Makefile index e69de29..958d5d8 100644 --- a/Makefile +++ b/Makefile @@ -0,0 +1,31 @@ +-include .env +.PHONY: all test clean deploy fund help install snapshot format anvil + +help: + @echo "Usage:" + @echo " make deploy [ARGS=...]\n example: make deploy ARGS=\"--network sepolia\"" + @echo "" + @echo " make fund [ARGS=...]\n example: make fund ARGS=\"--network sepolia\"" + +all: clean remove install update build + +# Clean the repo +clean :; forge clean + +# Remove modules +remove :; rm -rf .gitmodules && rm -rf .git/modules/* && rm -rf lib && touch .gitmodules && git add . && git commit -m "modules" +install :; forge install +# Update Dependencies +update:; forge update + +build:; forge build + +test:; forge test + +verbose:; forge test -vvvv + +format:; forge fmt + +anvil:; anvil -m 'test test test test test test test test test test test junk' --steps-tracing --block-time 1 + +coverage:; forge coverage \ No newline at end of file diff --git a/src/Main.sol b/src/Main.sol index f37794d..a8fd351 100644 --- a/src/Main.sol +++ b/src/Main.sol @@ -1,10 +1,13 @@ // SPDX-LINCENSE-Identifier: MIT pragma solidity ^0.8.20; +pragma experimental ABIEncoderV2; + import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol"; import {StakingToken} from "./StakingToken.sol"; import {RewardToken} from "./RewardToken.sol"; + /// @title Main Contract /// @author Gabi Maverick from catellatech /// @notice This is the main contract that manages the staking and unstaking also the rewardTokensDistribution logic diff --git a/test/MainTest.t.sol b/test/MainTest.t.sol new file mode 100644 index 0000000..e456ea8 --- /dev/null +++ b/test/MainTest.t.sol @@ -0,0 +1,101 @@ +// SPDX-Lincese-Identifier: MIT +pragma solidity ^0.8.20; + +import {StakingToken} from "../src/StakingToken.sol"; +import {RewardToken} from "../src/RewardToken.sol"; +import {Main} from "../src/Main.sol"; +import {Test, console} from "forge-std/Test.sol"; + +/** +1. So i need to deploy the staking contract and fund with some STK tokens to 2 users +2. So the main token deploy reward token to use the rewardTokensDistribution function +*/ + +contract MainTest is Test { + // custom errors + error Main_amountMustBeGreaterThanZero(); + error Main_stakingBalanceMustBeGreaterThanZero(); + + // call it all the contract that i need + StakingToken public stakingToken; + RewardToken public rewardToken; + Main public main; + + address public user1 = makeAddr("user1"); + address public user2 = makeAddr("user2"); + address public user3 = makeAddr("user3"); + + uint256 public stakingTokenInitialSupply = 1_000_000; + uint256 public rewardTokenInitialSupply = 1_000_000; + + function setUp() public { + stakingToken = new StakingToken(stakingTokenInitialSupply); + rewardToken = new RewardToken(rewardTokenInitialSupply); + main = new Main(stakingToken, rewardToken); + + vm.prank(address(this)); + stakingToken.transfer(user1, 1_000); + stakingToken.transfer(user2, 5_000); + } + + function testMainContractOwner() public view { + assertEq(main.owner(), address(this)); + } + + // Test the staking function of the Main contract + // user1 and user2 stake some STK tokens correctly + function testStakingToken() public { + vm.startPrank(user1); + uint256 amountToStake = 555; + // Let's approve the Main contract to stake the STK tokens + stakingToken.approve(address(main), amountToStake); + // Let's stake the STK tokens + main.stake(amountToStake); + vm.stopPrank(); + + // verify that user1 was succefully added into the stakers array + address staker = main.stakers(0); + assertEq(staker, user1, "Staker was not added to the array."); + + // Test that the Main contract receives the STK tokens + assertEq(stakingToken.balanceOf(address(main)), amountToStake); + + // Destructure the returned struct into individual variables for user1 + (uint256 stakedBalance, bool hasStaked, bool isStaking) = main.Stakes(user1); + // Test that the values of the Stakes struct are updated correctly user1 + assertEq(stakedBalance, amountToStake); + assertEq(isStaking, true); + assertEq(hasStaked, true); + + // Let's make user2 stake some STK tokens + vm.startPrank(user2); + uint256 amountToStakeUser2 = 4_000; + stakingToken.approve(address(main), amountToStakeUser2); + main.stake(amountToStakeUser2); + vm.stopPrank(); + + + uint256 newStakedBalance = amountToStake + amountToStakeUser2; + assertEq(stakingToken.balanceOf(address(main)), newStakedBalance); + + // Destructure the returned struct into individual variables for user2 + (uint256 stakedBalanceUser2, bool hasStakedUser2, bool isStakingUser2) = main.Stakes(user2); + // Test that the values of the Stakes struct are updated correctly user2 + assertEq(stakedBalanceUser2, amountToStakeUser2); + assertEq(isStakingUser2, true); + assertEq(hasStakedUser2, true); + + } + + function testStaingFailure() public { + vm.startPrank(user3); + uint256 amountToStake = 0; + stakingToken.approve(address(main), amountToStake); + vm.expectRevert(Main_amountMustBeGreaterThanZero.selector); + main.stake(amountToStake); + vm.stopPrank(); + } + + + +} \ No newline at end of file diff --git a/test/RewardTokenTest.t.sol b/test/RewardTokenTest.t.sol new file mode 100644 index 0000000..2920b4b --- /dev/null +++ b/test/RewardTokenTest.t.sol @@ -0,0 +1,19 @@ +// SPDX-Lincese-Identifier: MIT +pragma solidity ^0.8.20; + +import {RewardToken} from "../src/RewardToken.sol"; +import {Test} from "forge-std/Test.sol"; + +contract RewardTokenTest is Test { + RewardToken rewardToken; + uint256 initialSupply = 8_000_000; + + + function setUp() public { + rewardToken = new RewardToken(initialSupply); + } + + function testRewardTokenSupply() public view { + assertEq(rewardToken.totalSupply(), initialSupply); + } +} \ No newline at end of file diff --git a/test/StakingTokenTest.t.sol b/test/StakingTokenTest.t.sol new file mode 100644 index 0000000..a06797f --- /dev/null +++ b/test/StakingTokenTest.t.sol @@ -0,0 +1,18 @@ +// SPDX-Lincese-Identifier: MIT +pragma solidity ^0.8.20; + +import {StakingToken} from "../src/StakingToken.sol"; +import {Test} from "forge-std/Test.sol"; + +contract StakingTokenTest is Test { + StakingToken stakingToken; + uint256 initialStakingSupply = 1_000_000; + + function setUp() public { + stakingToken = new StakingToken(initialStakingSupply); + } + + function testSupply() public view { + assertEq(stakingToken.totalSupply(), initialStakingSupply); + } +}