183 lines
7.4 KiB
Solidity
183 lines
7.4 KiB
Solidity
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
pragma solidity ^0.8.19;
|
|
|
|
import "forge-std/Test.sol";
|
|
import "forge-std/console.sol";
|
|
import {IERC20} from "@openzeppelin/token/ERC20/IERC20.sol";
|
|
import "../src/Harberg.sol";
|
|
|
|
contract HarbergTest is Test {
|
|
Harberg harberg;
|
|
address stakingPool;
|
|
address liquidityPool;
|
|
address liquidityManager;
|
|
|
|
function setUp() public {
|
|
harberg = new Harberg("HARB", "HARB");
|
|
stakingPool = makeAddr("stakingPool");
|
|
harberg.setStakingPool(stakingPool);
|
|
liquidityManager = makeAddr("liquidityManager");
|
|
harberg.setLiquidityManager(liquidityManager);
|
|
}
|
|
|
|
// Simulates staking by transferring tokens to the stakingPool address.
|
|
function simulateStake(uint256 amount) internal {
|
|
// the amount of token has to be available on the balance
|
|
// of the test contract
|
|
harberg.transfer(stakingPool, amount);
|
|
}
|
|
|
|
// Simulates unstaking by transferring tokens from the stakingPool back to a given address.
|
|
function simulateUnstake(uint256 amount) internal {
|
|
// Direct transfer from the stakingPool to 'to' address to simulate unstaking
|
|
vm.prank(stakingPool); // Assuming 'stake' contract would allow this in an actual scenario
|
|
harberg.transfer(address(this), amount);
|
|
}
|
|
|
|
function testHarbergConstructor() public view {
|
|
// Check if the token details are set as expected
|
|
assertEq(harberg.name(), "HARB");
|
|
assertEq(harberg.symbol(), "HARB");
|
|
|
|
// Confirm that the TwabController address is correctly set
|
|
(address _lm, address _sp) = harberg.peripheryContracts();
|
|
assertEq(_lm, liquidityManager);
|
|
assertEq(_sp, stakingPool);
|
|
}
|
|
|
|
function testMintWithEmptyStakingPool() public {
|
|
uint256 initialSupply = harberg.totalSupply();
|
|
uint256 mintAmount = 1000 * 1e18; // 1000 HARB tokens
|
|
|
|
vm.prank(address(liquidityManager));
|
|
harberg.mint(mintAmount);
|
|
|
|
// Check if the total supply has increased correctly
|
|
assertEq(harberg.totalSupply(), initialSupply + mintAmount);
|
|
// Check if the staking pool balance is still 0, as before
|
|
assertEq(harberg.balanceOf(stakingPool), 0);
|
|
}
|
|
|
|
function testBurnWithEmptyStakingPool() public {
|
|
uint256 initialSupply = harberg.totalSupply();
|
|
uint256 burnAmount = 500 * 1e18; // 500 HARB tokens
|
|
|
|
// First, mint some tokens to burn
|
|
vm.prank(address(liquidityManager));
|
|
harberg.mint(burnAmount);
|
|
|
|
vm.prank(address(liquidityManager));
|
|
harberg.burn(burnAmount);
|
|
|
|
// Check if the total supply has decreased correctly
|
|
assertEq(harberg.totalSupply(), initialSupply);
|
|
// Check if the staking pool balance has decreased correctly
|
|
assertEq(harberg.balanceOf(stakingPool), 0);
|
|
}
|
|
|
|
function testMintImpactOnSimulatedStaking() public {
|
|
uint256 initialStakingPoolBalance = harberg.balanceOf(stakingPool);
|
|
uint256 mintAmount = 1000 * 1e18; // 1000 HARB tokens
|
|
// Ensure the test contract has enough tokens to simulate staking
|
|
vm.prank(address(liquidityManager));
|
|
harberg.mint(mintAmount);
|
|
vm.prank(address(liquidityManager));
|
|
harberg.transfer(address(this), mintAmount);
|
|
|
|
// Simulate staking of the minted amount
|
|
simulateStake(mintAmount);
|
|
|
|
// Check balances after simulated staking
|
|
assertEq(harberg.balanceOf(stakingPool), initialStakingPoolBalance + mintAmount);
|
|
}
|
|
|
|
function testUnstakeImpactOnTotalSupply() public {
|
|
uint256 stakeAmount = 500 * 1e18; // 500 HARB tokens
|
|
// Ensure the test contract has enough tokens to simulate staking
|
|
vm.prank(address(liquidityManager));
|
|
harberg.mint(stakeAmount);
|
|
vm.prank(address(liquidityManager));
|
|
harberg.transfer(address(this), stakeAmount);
|
|
|
|
uint256 initialTotalSupply = harberg.totalSupply();
|
|
|
|
// Simulate staking and then unstaking
|
|
simulateStake(stakeAmount);
|
|
simulateUnstake(stakeAmount);
|
|
|
|
// Check total supply remains unchanged after unstake
|
|
assertEq(harberg.totalSupply(), initialTotalSupply);
|
|
}
|
|
|
|
// Fuzz test for mint function with varying stake amounts
|
|
function testMintWithStake(uint8 _stakePercentage, uint256 mintAmount) public {
|
|
uint256 initialAmount = 500 * 1e18;
|
|
// Ensure the test contract has enough tokens to simulate staking
|
|
vm.prank(address(liquidityManager));
|
|
harberg.mint(initialAmount);
|
|
vm.prank(address(liquidityManager));
|
|
harberg.transfer(address(this), initialAmount);
|
|
|
|
// Limit fuzzing input to 0% - 20%
|
|
uint8 effectiveStakePercentage = _stakePercentage % 21;
|
|
uint256 stakeAmount = (initialAmount * effectiveStakePercentage) / 100;
|
|
simulateStake(stakeAmount);
|
|
|
|
uint256 initialTotalSupply = harberg.totalSupply();
|
|
uint256 initialStakingPoolBalance = harberg.balanceOf(stakingPool);
|
|
|
|
mintAmount = bound(mintAmount, 1, 500 * 1e18);
|
|
uint256 expectedNewStake =
|
|
initialStakingPoolBalance * mintAmount / (initialTotalSupply - initialStakingPoolBalance);
|
|
|
|
// Expect Transfer events
|
|
vm.expectEmit(true, true, true, true, address(harberg));
|
|
emit IERC20.Transfer(address(0), address(liquidityManager), mintAmount);
|
|
|
|
vm.prank(address(liquidityManager));
|
|
harberg.mint(mintAmount);
|
|
uint256 expectedStakingPoolBalance = initialStakingPoolBalance + expectedNewStake;
|
|
uint256 expectedTotalSupply = initialTotalSupply + mintAmount + expectedNewStake;
|
|
|
|
assertEq(
|
|
harberg.balanceOf(stakingPool),
|
|
expectedStakingPoolBalance,
|
|
"Staking pool balance did not adjust correctly after mint."
|
|
);
|
|
assertEq(harberg.totalSupply(), expectedTotalSupply, "Total supply did not match expected after mint.");
|
|
}
|
|
|
|
// Fuzz test for burn function with varying stake amounts
|
|
function testBurnWithStake(uint8 _stakePercentage, uint256 burnAmount) public {
|
|
uint256 mintAmount = 500 * 1e18;
|
|
// Ensure the test contract has enough tokens to simulate staking
|
|
vm.prank(address(liquidityManager));
|
|
harberg.mint(mintAmount);
|
|
|
|
// Limit fuzzing input to 0% - 20%
|
|
uint8 effectiveStakePercentage = _stakePercentage % 21;
|
|
uint256 stakeAmount = (mintAmount * effectiveStakePercentage) / 100;
|
|
vm.prank(address(liquidityManager));
|
|
harberg.transfer(address(this), stakeAmount);
|
|
simulateStake(stakeAmount);
|
|
|
|
burnAmount = bound(burnAmount, 0, 200 * 1e18);
|
|
uint256 initialTotalSupply = harberg.totalSupply();
|
|
uint256 initialStakingPoolBalance = harberg.balanceOf(stakingPool);
|
|
uint256 expectedExcessStake =
|
|
initialStakingPoolBalance * burnAmount / (initialTotalSupply - initialStakingPoolBalance);
|
|
|
|
vm.prank(address(liquidityManager));
|
|
harberg.burn(burnAmount);
|
|
|
|
uint256 expectedStakingPoolBalance = initialStakingPoolBalance - expectedExcessStake;
|
|
uint256 expectedTotalSupply = initialTotalSupply - burnAmount - expectedExcessStake;
|
|
|
|
assertEq(
|
|
harberg.balanceOf(stakingPool),
|
|
expectedStakingPoolBalance,
|
|
"Staking pool balance did not adjust correctly after burn."
|
|
);
|
|
assertEq(harberg.totalSupply(), expectedTotalSupply, "Total supply did not match expected after burn.");
|
|
}
|
|
}
|