Skip to content

Commit

Permalink
Merge pull request #2 from sammed-21/testing-stakeing
Browse files Browse the repository at this point in the history
add test to claimreward
  • Loading branch information
sammed-21 authored Oct 13, 2024
2 parents b6305dd + 4559477 commit 7683ec2
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/RewardToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ pragma solidity ^0.8.27;

contract RewardToken is ERC20 {
constructor(uint256 initialSupply) ERC20("RewardToken", "RWT") {
_mint(msg.sender, initialSupply);
_mint(msg.sender, initialSupply*10**18);
}
}
2 changes: 1 addition & 1 deletion src/StakeXToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ pragma solidity ^0.8.27;

contract StakeXToken is ERC20 {
constructor(uint256 initialSupply) ERC20("StakeXToken", "STX") {
_mint(msg.sender, initialSupply);
_mint(msg.sender, initialSupply*10**18);
}
}
3 changes: 2 additions & 1 deletion src/Staking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
pragma solidity ^0.8.27;

import "@openzeppelin/security/ReentrancyGuard.sol";
import "@openzeppelin/token/ERC20/IERC20.sol";
import "@openzeppelin/token/ERC20/IERC20.sol";

import "@openzeppelin/utils/math/SafeMath.sol";

contract Staking is ReentrancyGuard {
Expand Down
37 changes: 19 additions & 18 deletions test/Staking.t.sol
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import {Test,console} from "forge-std/Test.sol";
import {Test, console} from "forge-std/Test.sol";
import {Staking} from '../src/Staking.sol';
import {RewardToken} from '../src/RewardToken.sol';
import {StakeXToken} from '../src/StakeXToken.sol';


contract StakingTest is Test {
StakeXToken stakeXToken;
RewardToken rewardToken;
Expand All @@ -16,25 +15,28 @@ contract StakingTest is Test {
address bob = address(2);

function setUp() public {
// Mint 1000 stakeXTokens for Alice and Bob
stakeXToken = new StakeXToken(1000 * 10**18);
rewardToken = new RewardToken(1000 * 10**18);
// Deploy token contracts with initial supply
stakeXToken = new StakeXToken(1000000 * 10**18);
rewardToken = new RewardToken(100000000000 * 10**18);

// Deploy the staking contract
staking = new Staking(address(stakeXToken), address(rewardToken));

// Distribute some stakeXTokens to Alice and Bob
stakeXToken.transfer(alice, 100 * 10**18);
stakeXToken.transfer(bob, 100 * 10**18);

// Mint initial rewards to the staking contract
rewardToken.transfer(address(staking), 100000 * 10**18); // Ensure enough tokens for rewards
}

function teststakeXTokens() public {
function testStakeXTokens() public {
// Start staking tokens for Alice
vm.startPrank(alice);

// Approve and stake 50 tokens from Alice's account
stakeXToken.approve(address(staking), 50 * 10**18);
staking.Stake(50 * 10**18);
staking.stake(50 * 10**18);

// Check that the balance has been staked
assertEq(staking.stakedBalance(alice), 50 * 10**18);
Expand All @@ -48,10 +50,10 @@ contract StakingTest is Test {

// Approve and stake 50 tokens from Alice's account
stakeXToken.approve(address(staking), 50 * 10**18);
staking.Stake(50 * 10**18);
staking.stake(50 * 10**18);

// Withdraw 25 tokens
staking.Withdraw(25 * 10**18);
staking.withdrawStakedTokens(25 * 10**18);

// Check that the staked balance has been updated
assertEq(staking.stakedBalance(alice), 25 * 10**18);
Expand All @@ -65,25 +67,24 @@ contract StakingTest is Test {

// Approve and stake 50 tokens
stakeXToken.approve(address(staking), 50 * 10**18);
staking.Stake(50 * 10**18);
staking.stake(50 * 10**18);

// Fast forward time to simulate reward accumulation

skip(1 minutes);

// Check the earned rewards before claiming
uint earned = staking.earned(address(alice));
console.log(earned);
rewardToken.transfer(address(staking),10000);
assertGt(rewardToken.balanceOf(address(staking)),earned);

console.log("Earned rewards:", earned);

// Ensure that the staking contract has enough reward tokens
assertGt(rewardToken.balanceOf(address(staking)), earned, "Insufficient reward tokens in staking contract");

// Claim the rewards
staking.getReward(earned);
staking.getReward();

// Check that rewards have been claimed (replace `0` with expected reward amount)
// Check that rewards have been claimed
uint reward = staking.rewards(alice);
assertGt(reward, earned);
console.log(reward);

vm.stopPrank();
}
Expand Down

0 comments on commit 7683ec2

Please sign in to comment.