Skip to content

Commit

Permalink
fmt: test(Saking.sol)
Browse files Browse the repository at this point in the history
  • Loading branch information
Akshola00 committed Sep 20, 2024
1 parent 2dc75c8 commit 948c7ce
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 131 deletions.
21 changes: 5 additions & 16 deletions src/ERC20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ contract ERC20 is IERC20 {
* @dev Emitted when the allowance of a `spender` for an `owner` is set by
* a call to {approve}. `value` is the new allowance.
*/
event Approval(
address indexed owner,
address indexed spender,
uint256 value
);
event Approval(address indexed owner, address indexed spender, uint256 value);

/**
* @dev Emitted when ownership of the contract is transferred from `old` to `new`.
Expand Down Expand Up @@ -114,10 +110,7 @@ contract ERC20 is IERC20 {
* @return True if the operation was successful.
* @dev Emits a {Transfer} event.
*/
function transfer(
address recipient,
uint256 amount
) external returns (bool) {
function transfer(address recipient, uint256 amount) external returns (bool) {
if (recipient == address(0)) {
revert InvalidRecipient();
}
Expand Down Expand Up @@ -155,18 +148,14 @@ contract ERC20 is IERC20 {
* @return True if the operation was successful.
* @dev Emits a {Transfer} event.
*/
function transferFrom(
address sender,
address recipient,
uint256 amount
) external returns (bool) {
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool) {
// require(msg.sender != recipient, "cannot transfer to self");
if (recipient == address(0)) {
revert InvalidRecipient();
}

uint b = allowance[sender][msg.sender];
uint c = b - amount;
uint256 b = allowance[sender][msg.sender];
uint256 c = b - amount;
if (c > b) {
revert InsufficientBalance();
}
Expand Down
94 changes: 19 additions & 75 deletions src/Staking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,13 @@ pragma solidity ^0.8.19;
interface IERC20 {
function balanceOf(address account) external view returns (uint256);

function transfer(
address recipient,
uint256 amount
) external returns (bool);
function transfer(address recipient, uint256 amount) external returns (bool);

function allowance(
address owner,
address spender
) external view returns (uint256);
function allowance(address owner, address spender) external view returns (uint256);

function approve(address spender, uint256 amount) external returns (bool);

function transferFrom(
address sender,
address recipient,
uint256 amount
) external returns (bool);
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
}

contract StakingContract {
Expand Down Expand Up @@ -51,11 +41,7 @@ contract StakingContract {
//////////////////
// CONSTRUCTOR
//////////////////
constructor(
address _bwcErc20TokenAddress,
address _receiptTokenAddress,
address _rewardTokenAddress
) {
constructor(address _bwcErc20TokenAddress, address _receiptTokenAddress, address _rewardTokenAddress) {
bwcErc20TokenAddress = _bwcErc20TokenAddress;
receiptTokenAddress = _receiptTokenAddress;
rewardTokenAddress = _rewardTokenAddress;
Expand All @@ -72,22 +58,13 @@ contract StakingContract {
IERC20 receiptToken = IERC20(receiptTokenAddress);

// Staker have enough token to stake
require(
bwcToken.balanceOf(msg.sender) >= amount,
"STAKE: Insufficient funds"
);
require(bwcToken.balanceOf(msg.sender) >= amount, "STAKE: Insufficient funds");

// Contract has enough receipt token to send to staker
require(
receiptToken.balanceOf(address(this)) >= amount,
"STAKE: Low contract receipt token balance"
);
require(receiptToken.balanceOf(address(this)) >= amount, "STAKE: Low contract receipt token balance");

// Staker has approved enough tokens to be staked
require(
bwcToken.allowance(msg.sender, address(this)) >= amount,
"STAKE: Amount not allowed"
);
require(bwcToken.allowance(msg.sender, address(this)) >= amount, "STAKE: Amount not allowed");

StakeDetail storage stakeDetail = stakers[msg.sender];

Expand All @@ -96,19 +73,13 @@ contract StakingContract {
stakeDetail.status = true;

// Transfer stake token from Staker to contract
require(
bwcToken.transferFrom(msg.sender, address(this), amount),
"STAKE: Transfer failed"
);
require(bwcToken.transferFrom(msg.sender, address(this), amount), "STAKE: Transfer failed");

// Increase total stake amount of Staker
totalStaked += amount;

// Transfer receipt token from contract to Staker
require(
receiptToken.transfer(msg.sender, amount),
"STAKE: Receipt token transfer failed"
);
require(receiptToken.transfer(msg.sender, amount), "STAKE: Receipt token transfer failed");

emit TokenStaked(msg.sender, amount, block.timestamp);
return true;
Expand All @@ -122,48 +93,26 @@ contract StakingContract {

require(msg.sender != address(0), "WITHDRAW: Address zero not allowed");
require(amount > 0, "WITHDRAW: Zero amount not allowed");
require(
stakeDetail.amount >= amount,
"WITHDRAW: Withdraw amount not allowed"
);
require(
isTimeToWithdraw(stakeDetail.timeStaked),
"WITHDRAW: Not yet time to withdraw"
);
require(stakeDetail.amount >= amount, "WITHDRAW: Withdraw amount not allowed");
require(isTimeToWithdraw(stakeDetail.timeStaked), "WITHDRAW: Not yet time to withdraw");

IERC20 bwcToken = IERC20(bwcErc20TokenAddress);
IERC20 receiptToken = IERC20(receiptTokenAddress);
IERC20 rewardToken = IERC20(rewardTokenAddress);

uint256 withdrawAmount = amount * MULTIPLIER;

require(rewardToken.balanceOf(address(this)) >= withdrawAmount, "WITHDRAW: Insufficient reward token balance");
require(bwcToken.balanceOf(address(this)) >= amount, "WITHDRAW: Insufficient BWC token balance");
require(
rewardToken.balanceOf(address(this)) >= withdrawAmount,
"WITHDRAW: Insufficient reward token balance"
);
require(
bwcToken.balanceOf(address(this)) >= amount,
"WITHDRAW: Insufficient BWC token balance"
);
require(
receiptToken.allowance(msg.sender, address(this)) >= amount,
"WITHDRAW: Receipt token allowance too low"
receiptToken.allowance(msg.sender, address(this)) >= amount, "WITHDRAW: Receipt token allowance too low"
);

stakeDetail.amount -= amount;

require(
receiptToken.transferFrom(msg.sender, address(this), amount),
"WITHDRAW: Receipt token transfer failed"
);
require(
rewardToken.transfer(msg.sender, withdrawAmount),
"WITHDRAW: Reward token transfer failed"
);
require(
bwcToken.transfer(msg.sender, amount),
"WITHDRAW: BWC token transfer failed"
);
require(receiptToken.transferFrom(msg.sender, address(this), amount), "WITHDRAW: Receipt token transfer failed");
require(rewardToken.transfer(msg.sender, withdrawAmount), "WITHDRAW: Reward token transfer failed");
require(bwcToken.transfer(msg.sender, amount), "WITHDRAW: BWC token transfer failed");

totalStaked -= amount;

Expand All @@ -178,13 +127,8 @@ contract StakingContract {
return stakers[staker].amount;
}

function getNextWithdrawTime(
address staker
) external view returns (uint256) {
return
stakers[staker].timeStaked +
MIN_TIME_BEFORE_WITHDRAW -
block.timestamp;
function getNextWithdrawTime(address staker) external view returns (uint256) {
return stakers[staker].timeStaked + MIN_TIME_BEFORE_WITHDRAW - block.timestamp;
}

function getTotalStake() external view returns (uint256) {
Expand Down
16 changes: 3 additions & 13 deletions src/interfaces/IERC20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,11 @@ interface IERC20 {

function balanceOf(address account) external view returns (uint256);

function transfer(
address recipient,
uint256 amount
) external returns (bool);
function transfer(address recipient, uint256 amount) external returns (bool);

function allowance(
address owner,
address spender
) external view returns (uint256);
function allowance(address owner, address spender) external view returns (uint256);

function approve(address spender, uint256 amount) external returns (bool);

function transferFrom(
address sender,
address recipient,
uint256 amount
) external returns (bool);
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
}
19 changes: 7 additions & 12 deletions test/ERC20.t.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

Expand All @@ -15,7 +13,6 @@ contract ERC20ContractTest is Test {
event Approval(address indexed owner, address indexed spender, uint256 value);
event Owned(address indexed old, address indexed newAddress);


error InvalidRecipient();

function setUp() public {
Expand Down Expand Up @@ -73,7 +70,7 @@ contract ERC20ContractTest is Test {
erc20Contract.mint(ownerAddress, 1000);
assertEq(erc20Contract.balanceOf(ownerAddress), 1000, "Amount supposed to be 1000");
vm.expectEmit(true, true, false, true);
emit Transfer(ownerAddress ,recipient, 500);
emit Transfer(ownerAddress, recipient, 500);
vm.prank(ownerAddress);
erc20Contract.transfer(recipient, 500);
assertEq(erc20Contract.balanceOf(ownerAddress), 500, "Amount supposed to be 500");
Expand All @@ -91,8 +88,6 @@ contract ERC20ContractTest is Test {
assertEq(erc20Contract.balanceOf(ownerAddress), 1000, "Amount supposed to be 500");
}



function test_TransferFrom() public {
address recipient = address(0x2938);
address caller = address(0x2373);
Expand Down Expand Up @@ -120,7 +115,7 @@ contract ERC20ContractTest is Test {

uint256 balanceOfSenderBeforeTransfer = erc20Contract.balanceOf(randomAddress);
uint256 allowanceOfCallerBeforeTransfer = erc20Contract.allowance(randomAddress, caller);

//testing events for transfer
vm.expectEmit(true, true, false, true);
emit Transfer(randomAddress, recipient, amount);
Expand All @@ -144,24 +139,24 @@ contract ERC20ContractTest is Test {
emit Owned(ownerAddress, newowner);
vm.prank(ownerAddress);
erc20Contract.changeOwner(newowner);
assertEq(erc20Contract.owner(), newowner, "Owner not changed");
assertEq(erc20Contract.owner(), newowner, "Owner not changed");
}

function testRevert_changeOwnerShouldReverIfUnauthorized () public {
address newowner = address(0x2938);
function testRevert_changeOwnerShouldReverIfUnauthorized() public {
address newowner = address(0x2938);
vm.expectRevert("Unauthorized");
vm.prank(randomAddress);
erc20Contract.changeOwner(newowner);
}

function test_Burn() public {
vm.startPrank(ownerAddress);
address add = address(0x2938);
address add = address(0x2938);
assertEq(erc20Contract.balanceOf(add), 0, "Token balance should be 0");
erc20Contract.mint(add, 1000);
assertEq(erc20Contract.balanceOf(add), 1000, "Token balance should be 1000");
erc20Contract.burn(add, 500);
assertEq(erc20Contract.balanceOf(add), 500, "Token balance should be 500");
vm.stopPrank();
}
}
}
26 changes: 11 additions & 15 deletions test/Staking.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {ERC20} from "../src/ERC20.sol";

contract StakingContractTest is Test {
StakingContract public stakingContract;

struct StakeDetail {
uint256 timeStaked;
uint256 amount;
Expand All @@ -15,7 +16,7 @@ contract StakingContractTest is Test {

address ownerAddress = address(0x0101);
address callingAddress = address(0x333);
uint256 constant mintConst = 1000;
uint256 constant mintConst = 1000;

// Events
event TokenStaked(address indexed staker, uint256 amount, uint256 time);
Expand All @@ -39,11 +40,7 @@ contract StakingContractTest is Test {
receiptTokenAddress = address(receiptTokenContract);
rewardTokenAddress = address(rewardTokenContract);
vm.prank(ownerAddress);
stakingContract = new StakingContract(
bwcTokenAddress,
receiptTokenAddress,
rewardTokenAddress
);
stakingContract = new StakingContract(bwcTokenAddress, receiptTokenAddress, rewardTokenAddress);
stakingContractAddress = address(stakingContract);
}

Expand All @@ -54,22 +51,22 @@ contract StakingContractTest is Test {
assertEq(stakingContract.totalStaked(), 0);
}

function testRevert_StakeWillRevertifZeroAddress () public {
function testRevert_StakeWillRevertifZeroAddress() public {
vm.expectRevert("STAKE: Address zero not allowed");
vm.startPrank(address(0));
stakingContract.stake(200);
}

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

function test_StakeSuccessful () public {
function test_StakeSuccessful() public {
uint256 callerStakingAmount = 200;
// mint all needed toknens
bwcErc20TokenContract.mint(callingAddress, mintConst);
receiptTokenContract.mint(stakingContractAddress, mintConst );
receiptTokenContract.mint(stakingContractAddress, mintConst);
rewardTokenContract.mint(stakingContractAddress, mintConst);

//check if they were minted succesfully
Expand Down Expand Up @@ -102,18 +99,18 @@ contract StakingContractTest is Test {
assertApproxEqRel(timeStaked, block.timestamp, 1, "Time staked is not close enough to block timestamp");
}

function testRevert_WithdrawWillRevertifZeroAddress () public {
function testRevert_WithdrawWillRevertifZeroAddress() public {
vm.expectRevert("WITHDRAW: Address zero not allowed");
vm.startPrank(address(0));
stakingContract.withdraw(200);
}

function testRevert_WithdrawWillRevertifAmountLessThanZero () public {
function testRevert_WithdrawWillRevertifAmountLessThanZero() public {
vm.expectRevert("WITHDRAW: Zero amount not allowed");
stakingContract.withdraw(0);
}

function testRevert_WithdrawWillRevertifStakeAmountGreaterThanWithdrawAmount () public {
function testRevert_WithdrawWillRevertifStakeAmountGreaterThanWithdrawAmount() public {
test_StakeSuccessful();
vm.expectRevert("WITHDRAW: Withdraw amount not allowed");
vm.prank(callingAddress);
Expand Down Expand Up @@ -145,8 +142,7 @@ contract StakingContractTest is Test {
vm.prank(callingAddress);
stakingContract.withdraw(200);

( timeStaked, stakedAmount, status) = stakingContract.stakers(callingAddress);
(timeStaked, stakedAmount, status) = stakingContract.stakers(callingAddress);
assertEq(stakedAmount, 0, "Amount Supposed to be 0");
}

}

0 comments on commit 948c7ce

Please sign in to comment.