-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVault.t.sol
More file actions
109 lines (86 loc) · 3.78 KB
/
Vault.t.sol
File metadata and controls
109 lines (86 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
import {Test, console} from "forge-std/Test.sol";
import {Vault} from "../src/Vault.sol";
import {BahlulToken} from "../src/Bahlul.sol";
/**
* @title VaultTest
* @notice Test suite for the Vault contract functionality
* @dev Uses Foundry's Test framework to test deposit, withdraw, and yield distribution features
*/
contract VaultTest is Test {
/// @notice The vault contract instance being tested
Vault public vault;
/// @notice Mock ERC20 token used as the underlying asset for the vault
BahlulToken public mockERC20;
/// @notice Test user address for simulating deposits and withdrawals
address public user1 = makeAddr("user1");
/// @notice Second test user address for multi-user scenarios
address public user2 = makeAddr("user2");
/**
* @notice Sets up the test environment before each test
* @dev Deploys fresh instances of BahlulToken and Vault contracts
* Mints 1000 tokens to both test users for testing purposes
*/
function setUp() public {
// Deploy the mock ERC20 token
mockERC20 = new BahlulToken();
// Deploy the vault with the mock token as the underlying asset
vault = new Vault(address(mockERC20));
// Mint initial token balances to test users
mockERC20.mint(user1, 1000);
mockERC20.mint(user2, 1000);
}
/**
* @notice Tests the basic deposit functionality of the vault
* @dev Verifies that:
* - User can approve and deposit tokens into the vault
* - Token balance decreases after deposit
* - Vault shares are minted to the depositor
*/
function test_Deposit() public {
// Simulate transactions from user1's perspective
vm.startPrank(user1);
// Approve the vault to spend user1's tokens
mockERC20.approve(address(vault), 50);
// Record balance before deposit
uint256 beforeBalance = mockERC20.balanceOf(user1);
console.log("Balance before deposit", beforeBalance);
// Deposit 50 tokens into the vault
vault.deposit(50);
// Record balance after deposit (should be 50 less)
uint256 afterBalance = mockERC20.balanceOf(user1);
console.log("Balance after deposit", afterBalance);
// Stop simulating user1's transactions
vm.stopPrank();
}
/**
* @notice Tests the deposit and withdraw cycle
* @dev Verifies that:
* - User can deposit tokens and receive shares
* - User can withdraw by burning shares to get tokens back
* - Balance is restored to original amount (assuming no yield distribution)
* - The full deposit-withdraw cycle works correctly
*/
function test_Withdraw() public {
// Simulate transactions from user1's perspective
vm.startPrank(user1);
// Approve the vault to spend user1's tokens
mockERC20.approve(address(vault), 50);
// Record initial balance (should be 1000 from setUp)
uint256 balanceBefore = mockERC20.balanceOf(user1);
console.log("Balance before deposit", balanceBefore);
// Deposit 50 tokens into the vault, receiving 50 shares (1:1 ratio for first deposit)
vault.deposit(50);
// Record balance after deposit (should be 950)
uint256 balanceAfterDeposit = mockERC20.balanceOf(user1);
console.log("Balance after deposit", balanceAfterDeposit);
// Withdraw by burning 50 shares, should receive 50 tokens back
vault.withdraw(50);
// Record balance after withdrawal (should be back to 1000)
uint256 balanceAfterWithdraw = mockERC20.balanceOf(user1);
console.log("Balance after withdraw", balanceAfterWithdraw);
// Stop simulating user1's transactions
vm.stopPrank();
}
}