Skip to content

Commit

Permalink
✅ add vault operations tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Flocqst committed Oct 28, 2024
1 parent 0cea36b commit 8668cfe
Showing 1 changed file with 154 additions and 9 deletions.
163 changes: 154 additions & 9 deletions test/KSXVault.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@ pragma solidity 0.8.25;

import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import {Test} from "forge-std/Test.sol";
import {Bootstrap, KSXVault} from "test/utils/Bootstrap.sol";

import {console2} from "forge-std/console2.sol";
import {MockERC20} from "test/mocks/MockERC20.sol";
import {MockStakingRewards} from "test/mocks/MockStakingRewards.sol";
import {Bootstrap, KSXVault} from "test/utils/Bootstrap.sol";

contract KSXVaultTest is Bootstrap {

MockERC20 depositToken;
MockStakingRewards stakingRewards;

function setUp() public {

depositToken = new MockERC20("Deposit Token", "DT");
stakingRewards = new MockStakingRewards(address(depositToken));
initializeLocal(address(depositToken), address(stakingRewards), DECIMAL_OFFSET);
initializeLocal(
address(depositToken), address(stakingRewards), DECIMAL_OFFSET
);

depositToken.mint(alice, 10 ether);
depositToken.mint(bob, 10 ether);
Expand All @@ -39,13 +42,100 @@ contract KSXVaultTest is Bootstrap {

// Asserts correct deposit at 1000 shares ratio
// Converts asset values to shares and deposits assets into the vault
// asserts deposited kwenta is correctly staked in the staking rewards
// contract
function test_vault_deposit() public {
uint256 amount = 1 ether;
vm.startPrank(alice);
depositToken.approve(address(ksxVault), amount);
ksxVault.deposit(1 ether, alice);
assertEq(ksxVault.balanceOf(alice), amount * (10 ** ksxVault.offset()));
assertEq(stakingRewards.stakedBalanceOf(address(ksxVault)), amount);
assertEq(stakingRewards.balanceOf(address(ksxVault)), amount);
vm.stopPrank();
}

// Asserts correct deposit at 1000 shares ratio
function test_vault_deposit_1000_ratio() public {
uint256 depositAmount = 1 ether;
uint256 expectedShares = 1000 ether; // We expect 1000x the deposit
// amount

vm.startPrank(alice);
depositToken.approve(address(ksxVault), depositAmount);
ksxVault.deposit(depositAmount, alice);

assertEq(ksxVault.balanceOf(alice), expectedShares);
vm.stopPrank();
}

// Asserts correct deposit at 1000 shares ratio Fuzzing depositAmount
function test_vault_deposit_1000_ratio_Fuzz(uint256 depositAmount) public {
depositAmount = bound(depositAmount, 1, 1_000_000 ether);

depositToken.mint(alice, depositAmount);

vm.startPrank(alice);
depositToken.approve(address(ksxVault), depositAmount);
ksxVault.deposit(depositAmount, alice);

uint256 expectedKSX = depositAmount * 1000;

// Verify the initial 1:1000 ratio holds for any amount
assertEq(ksxVault.balanceOf(alice), expectedKSX);

vm.stopPrank();
}

// Asserts the 1:1000 ratio is maintained when no rewards have accrued
function test_consecutive_deposits() public {
// First deposit: 1 KWENTA
uint256 firstDeposit = 1 ether;

vm.startPrank(alice);

// First deposit
depositToken.approve(address(ksxVault), firstDeposit);
uint256 firstShares = ksxVault.deposit(firstDeposit, alice);

// Verify first deposit results
assertEq(
firstShares, 1000 ether, "First deposit should mint at 1:1000 ratio"
);
assertEq(
stakingRewards.balanceOf(address(ksxVault)),
firstDeposit,
"Should have staked first deposit"
);

// Second deposit: 2 KWENTA
uint256 secondDeposit = 2 ether;
depositToken.approve(address(ksxVault), secondDeposit);
uint256 secondShares = ksxVault.deposit(secondDeposit, alice);

// Verify second deposit results
assertEq(
secondShares,
2000 ether,
"Second deposit should mint at 1:1000 ratio"
);
assertEq(
stakingRewards.balanceOf(address(ksxVault)),
firstDeposit + secondDeposit,
"Should have staked both deposits"
);

// Verify final state
assertEq(
ksxVault.balanceOf(alice),
3000 ether,
"Should have total of 3000 KSX"
);
assertEq(
stakingRewards.balanceOf(address(ksxVault)),
3 ether,
"Should have 3 KWENTA staked total"
);

vm.stopPrank();
}

Expand All @@ -59,26 +149,81 @@ contract KSXVaultTest is Bootstrap {
ksxVault.mint(1 ether, alice);
assertEq(ksxVault.balanceOf(alice), amount);
assertEq(
stakingRewards.stakedBalanceOf(address(ksxVault)),
stakingRewards.balanceOf(address(ksxVault)),
amount / (10 ** ksxVault.offset())
);
vm.stopPrank();
}

// Asserts correct mint at 1000 shares ratio
function test_vault_mint_1000_ratio() public {
uint256 sharesToMint = 1000 ether; // minting 1000 KSX shares requires 1
// KWENTA
uint256 depositTokenNeeded = 1 ether;

uint256 initialDepositTokenBalance = depositToken.balanceOf(alice);

vm.startPrank(alice);
depositToken.approve(address(ksxVault), depositTokenNeeded);
ksxVault.mint(sharesToMint, alice);

// Verify we got the correct number of shares
assertEq(ksxVault.balanceOf(alice), sharesToMint);
assertEq(
depositToken.balanceOf(alice),
initialDepositTokenBalance - depositTokenNeeded
);
vm.stopPrank();
}

// Asserts correct mint at 1000 shares ratio Fuzzing sharesToMint
function test_vault_mint_1000_ratio_Fuzz(uint256 sharesToMint) public {
sharesToMint = bound(sharesToMint, 1000 ether, 100_000_000 ether);

uint256 depositTokenNeeded = (sharesToMint + 999) / 1000;

// Ensure alice has enough tokens
depositToken.mint(alice, depositTokenNeeded);

uint256 initialDepositTokenBalance = depositToken.balanceOf(alice);

vm.startPrank(alice);
depositToken.approve(address(ksxVault), depositTokenNeeded);
ksxVault.mint(sharesToMint, alice);

// Verify we got the correct number of shares
assertEq(ksxVault.balanceOf(alice), sharesToMint);

assertEq(
depositToken.balanceOf(alice),
initialDepositTokenBalance - depositTokenNeeded
);
vm.stopPrank();
}

// Withdraws a specified amount of assets from the vault by burning the
// equivalent shares
function test_withdraw() public {
uint256 amount = 1 ether;

vm.startPrank(alice);

// Approve and deposit
depositToken.approve(address(ksxVault), amount);
ksxVault.deposit(amount, alice);

// Verify initial state
assertEq(ksxVault.balanceOf(alice), amount * (10 ** ksxVault.offset()));
assertEq(stakingRewards.stakedBalanceOf(address(ksxVault)), amount);
assertEq(stakingRewards.balanceOf(address(ksxVault)), amount);

// Withdraw
ksxVault.withdraw(amount, alice, alice);

// Verify final state
assertEq(ksxVault.balanceOf(alice), 0);
assertEq(stakingRewards.stakedBalanceOf(address(ksxVault)), 0);
assertEq(stakingRewards.balanceOf(address(ksxVault)), 0);
assertEq(depositToken.balanceOf(alice), 10 ether);

vm.stopPrank();
}

Expand All @@ -87,9 +232,9 @@ contract KSXVaultTest is Bootstrap {
vm.startPrank(alice);
depositToken.approve(address(ksxVault), amount);
ksxVault.mint(1 ether, alice);
assertEq(stakingRewards.stakedBalanceOf(address(ksxVault)), amount / 1000);
assertEq(stakingRewards.balanceOf(address(ksxVault)), amount / 1000);
assertEq(
stakingRewards.stakedBalanceOf(address(ksxVault)),
stakingRewards.balanceOf(address(ksxVault)),
amount / (10 ** ksxVault.offset())
);

Expand Down

0 comments on commit 8668cfe

Please sign in to comment.