Skip to content

Commit

Permalink
test(ERC20): some test
Browse files Browse the repository at this point in the history
  • Loading branch information
GoSTEAN committed Sep 20, 2024
1 parent 9fd4a5e commit 0cca744
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 1 deletion.
1 change: 0 additions & 1 deletion src/ERC20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ contract ERC20 is IERC20 {
address recipient,
uint256 amount
) external returns (bool) {
require(msg.sender != recipient, "cannot transfer to self");
if (recipient == address(0)) {
revert InvalidRecipient();
}
Expand Down
4 changes: 4 additions & 0 deletions src/Staking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ contract StakingContract {
address public rewardTokenAddress;
uint256 public totalStaked;

function getStakers(address _addr) public view returns (StakeDetail memory){
return stakers[_addr];
}

//////////////////
// CONSTANTS
//////////////////
Expand Down
141 changes: 141 additions & 0 deletions test/Staking.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
pragma solidity ^0.8.19;

import {Test, console} from "forge-std/Test.sol";
import "forge-std/console.sol";
import {StakingContract} from "../src/Staking.sol";
import {ERC20} from "../src/ERC20.sol";

Expand All @@ -11,11 +12,19 @@ contract StakingContractTest is Test {
ERC20 public bwcErc20TokenContract;
ERC20 public receiptTokenContract;
ERC20 public rewardTokenContract;
address public user = address(0x1);
address public user1 = address(0x21);
uint256 public constant INITIAL_BALANCE = 10000e18;
uint256 public stakingAmount = 100e18;
uint256 constant MIN_TIME_BEFORE_WITHDRAW = 240;

address bwcTokenAddress;
address receiptTokenAddress;
address rewardTokenAddress;

address ownerAddr;
address addr1;

function setUp() public {
bwcErc20TokenContract = new ERC20("BlockheaderWeb3 Token", "BWC", 0);
receiptTokenContract = new ERC20("Receipt Token", "cBWC", 0);
Expand All @@ -30,9 +39,141 @@ contract StakingContractTest is Test {
receiptTokenAddress,
rewardTokenAddress
);

bwcErc20TokenContract.mint(user, INITIAL_BALANCE);
bwcErc20TokenContract.mint(user1, INITIAL_BALANCE);

receiptTokenContract.mint(address(stakingContract), INITIAL_BALANCE);
}

function test_StakingContractDeployment() public view {
assertEq(stakingContract.bwcErc20TokenAddress(), bwcTokenAddress);
assertEq(stakingContract.receiptTokenAddress(), receiptTokenAddress);
assertEq(stakingContract.rewardTokenAddress(), rewardTokenAddress);
}

function test_StakingNotAddZero() public {
vm.startPrank(address(0));
uint256 amount = 100e18;
bwcErc20TokenContract.approve(address(stakingContract), amount);
vm.expectRevert("STAKE: Address zero not allowed");
stakingContract.stake(amount);
vm.stopPrank();
}

function test_StakingWithZeroAmount() public {
vm.startPrank(user);
vm.expectRevert("STAKE: Zero amount not allowed");
stakingContract.stake(0);
vm.stopPrank();
}

function test_StakingWithInsufficientBalance() public {
uint256 stakeAmount = INITIAL_BALANCE + 1;
vm.startPrank(user);
bwcErc20TokenContract.approve(address(stakingContract), stakeAmount);
vm.expectRevert("STAKE: Insufficient funds");
stakingContract.stake(stakeAmount);
vm.stopPrank();
}

function test_StakingWithInsufficientAllowance() public {
uint256 stakeAmount = 100e18;
vm.startPrank(user);
bwcErc20TokenContract.approve(address(stakingContract), stakeAmount - 1);
vm.expectRevert("STAKE: Amount not allowed");
stakingContract.stake(stakeAmount);
vm.stopPrank();
}

function test_StakingWhenContractHasInsufficientReceiptTokens() public {
uint256 stakeAmount = INITIAL_BALANCE;
receiptTokenContract.burn(address(stakingContract), INITIAL_BALANCE);

vm.startPrank(user);
bwcErc20TokenContract.approve(address(stakingContract), stakeAmount);
vm.expectRevert("STAKE: Low contract receipt token balance");
stakingContract.stake(stakeAmount);
vm.stopPrank();
}

function test_StakeTransferFailed() public {
vm.startPrank(user);
uint256 stakeAmount = 100e18;

vm.mockCall(
address(bwcTokenAddress),
abi.encodeWithSignature("transferFrom(address,address,uint256)", user, address(stakingContract), stakeAmount),
abi.encode(false) // Simulate a transfer failure
);
// uint time = block.timestamp;
// bool status = true;
// bwcErc20TokenContract.approve(address(stakingContract), stakeAmount);

// // assertEq(stakingContract.getStakers(user).amount, stakeAmount);
// assertEq(stakingContract.getStakers(user).timeStaked, time);
// assertEq(stakingContract.getStakers(user).status, status);


vm.expectRevert("STAKE: Transfer failed");
stakingContract.stake(stakeAmount);
vm.stopPrank();
}


function test_GetTotalStake() public {
// Initial total stake should be 0
assertEq(stakingContract.getTotalStake(), 0, "Initial total stake should be 0");

// Stake some tokens
uint256 stakingAmount1 = 100e18;
uint256 stakingAmount2 = 150e18;

uint256 totalAmount = stakingAmount1 + stakingAmount2;

vm.startPrank(user);
bwcErc20TokenContract.approve(address(stakingContract), totalAmount);
stakingContract.stake(stakingAmount1);

// Check total stake after first staking
assertEq(stakingContract.getTotalStake(), stakingAmount1, "Total stake should equal first staking amount");

// Stake more tokens
stakingContract.stake(stakingAmount2);
vm.stopPrank();

// Check total stake after second staking
assertEq(stakingContract.getTotalStake(), totalAmount, "Total stake should equal sum of both staking amounts");
}

function test_GetBwcTokenAddress() public view{

assertEq(stakingContract.getBwcTokenAddress(), bwcTokenAddress, "The bwc token address");
}

function test_GetRewardTokenAddress() public view {
assertEq(stakingContract.getRewardTokenAddress(), rewardTokenAddress, "The reward token address");
}

function test_GetReceiptTokenAddress() public view {
assertEq(stakingContract.getReceiptTokenAddress(), receiptTokenAddress, "The receive token address");
}



function test_NextWithdrawal() public view {
uint time = block.timestamp;
uint256 nextTime = time + MIN_TIME_BEFORE_WITHDRAW - block.timestamp;
uint256 actualTime = stakingContract.getNextWithdrawTime(user);
assertApproxEqAbs(actualTime, nextTime, 1, "The time left for the next withdrawal should be within 1 second of the expected time.");
}

// function test_IsTimeToWithdraw() public {

// }

function test_Withdraw() public {
uint256 amount = 100e18;

}
}

0 comments on commit 0cca744

Please sign in to comment.