Skip to content

Commit

Permalink
starting with some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gab0071 committed Aug 21, 2024
1 parent 032c9d0 commit 4208fef
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 0 deletions.
31 changes: 31 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions src/Main.sol
Original file line number Diff line number Diff line change
@@ -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
Expand Down
101 changes: 101 additions & 0 deletions test/MainTest.t.sol
Original file line number Diff line number Diff line change
@@ -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();
}



}
19 changes: 19 additions & 0 deletions test/RewardTokenTest.t.sol
Original file line number Diff line number Diff line change
@@ -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);
}
}
18 changes: 18 additions & 0 deletions test/StakingTokenTest.t.sol
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 4208fef

Please sign in to comment.