-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathERC20Module.t.sol
More file actions
215 lines (163 loc) · 6.31 KB
/
Copy pathERC20Module.t.sol
File metadata and controls
215 lines (163 loc) · 6.31 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//SPDX-License-Identifier: MPL-2.0
pragma solidity ^0.8.20;
import "./HelperContract.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "CMTAT/interfaces/technical/IERC20Allowance.sol";
contract ERC20ModuleTest is HelperContract {
uint256 constant INITIAL_SUPPLY = 1000;
function setUp() public {
_deployToken();
// Mint initial tokens to USER1
vm.prank(ADMIN);
cmtat.mint(USER1, INITIAL_SUPPLY);
}
// ============ Token Metadata Tests ============
function test_Name() public {
assertEq(cmtat.name(), TOKEN_NAME);
}
function test_Symbol() public {
assertEq(cmtat.symbol(), TOKEN_SYMBOL);
}
function test_Decimals() public {
assertEq(cmtat.decimals(), TOKEN_DECIMALS);
}
// ============ Balance and Supply Tests ============
function test_TotalSupply() public {
assertEq(cmtat.totalSupply(), INITIAL_SUPPLY);
}
function test_BalanceOf() public {
assertEq(cmtat.balanceOf(USER1), INITIAL_SUPPLY);
assertEq(cmtat.balanceOf(USER2), 0);
}
// ============ Transfer Tests ============
function test_Transfer() public {
uint256 transferAmount = 100;
vm.prank(USER1);
vm.expectEmit(true, true, false, true);
emit IERC20.Transfer(USER1, USER2, transferAmount);
bool success = cmtat.transfer(USER2, transferAmount);
assertTrue(success);
assertEq(cmtat.balanceOf(USER1), INITIAL_SUPPLY - transferAmount);
assertEq(cmtat.balanceOf(USER2), transferAmount);
}
function test_TransferEntireBalance() public {
vm.prank(USER1);
bool success = cmtat.transfer(USER2, INITIAL_SUPPLY);
assertTrue(success);
assertEq(cmtat.balanceOf(USER1), 0);
assertEq(cmtat.balanceOf(USER2), INITIAL_SUPPLY);
}
function test_TransferToSelf() public {
uint256 balanceBefore = cmtat.balanceOf(USER1);
vm.prank(USER1);
bool success = cmtat.transfer(USER1, 100);
assertTrue(success);
assertEq(cmtat.balanceOf(USER1), balanceBefore);
}
function test_RevertWhen_TransferExceedsBalance() public {
vm.prank(USER1);
vm.expectRevert();
cmtat.transfer(USER2, INITIAL_SUPPLY + 1);
}
function test_RevertWhen_TransferToZeroAddress() public {
vm.prank(USER1);
vm.expectRevert();
cmtat.transfer(ZERO_ADDRESS, 100);
}
// ============ Approve and Allowance Tests ============
function test_Approve() public {
uint256 approveAmount = 500;
vm.prank(USER1);
vm.expectEmit(true, true, false, true);
emit IERC20.Approval(USER1, USER2, approveAmount);
bool success = cmtat.approve(USER2, approveAmount);
assertTrue(success);
assertEq(cmtat.allowance(USER1, USER2), approveAmount);
}
function test_ApproveOverwrite() public {
// First approval
vm.prank(USER1);
cmtat.approve(USER2, 500);
assertEq(cmtat.allowance(USER1, USER2), 500);
// Overwrite with new amount
vm.prank(USER1);
cmtat.approve(USER2, 200);
assertEq(cmtat.allowance(USER1, USER2), 200);
}
function test_ApproveToZero() public {
vm.prank(USER1);
cmtat.approve(USER2, 500);
vm.prank(USER1);
cmtat.approve(USER2, 0);
assertEq(cmtat.allowance(USER1, USER2), 0);
}
// ============ TransferFrom Tests ============
function test_TransferFrom() public {
uint256 approveAmount = 500;
uint256 transferAmount = 200;
// USER1 approves USER2
vm.prank(USER1);
cmtat.approve(USER2, approveAmount);
// USER2 transfers from USER1 to USER3
vm.prank(USER2);
vm.expectEmit(true, true, false, true);
emit IERC20.Transfer(USER1, USER3, transferAmount);
bool success = cmtat.transferFrom(USER1, USER3, transferAmount);
assertTrue(success);
assertEq(cmtat.balanceOf(USER1), INITIAL_SUPPLY - transferAmount);
assertEq(cmtat.balanceOf(USER3), transferAmount);
assertEq(cmtat.allowance(USER1, USER2), approveAmount - transferAmount);
}
function test_TransferFromWithExactAllowance() public {
uint256 amount = 300;
vm.prank(USER1);
cmtat.approve(USER2, amount);
vm.prank(USER2);
bool success = cmtat.transferFrom(USER1, USER3, amount);
assertTrue(success);
assertEq(cmtat.allowance(USER1, USER2), 0);
}
function test_RevertWhen_TransferFromExceedsAllowance() public {
vm.prank(USER1);
cmtat.approve(USER2, 100);
vm.prank(USER2);
vm.expectRevert();
cmtat.transferFrom(USER1, USER3, 200);
}
function test_RevertWhen_TransferFromExceedsBalance() public {
// Approve more than balance
vm.prank(USER1);
cmtat.approve(USER2, INITIAL_SUPPLY + 100);
vm.prank(USER2);
vm.expectRevert();
cmtat.transferFrom(USER1, USER3, INITIAL_SUPPLY + 1);
}
function test_RevertWhen_TransferFromWithoutApproval() public {
vm.prank(USER2);
vm.expectRevert();
cmtat.transferFrom(USER1, USER3, 100);
}
// ============ Allowance / Spend Event Tests ============
function test_TransferFromEmitsSpendAndReducesAllowance() public {
vm.prank(USER1);
cmtat.approve(USER2, 100);
vm.expectEmit(true, true, true, true);
emit IERC20Allowance.Spend(USER1, USER2, 40);
vm.prank(USER2);
cmtat.transferFrom(USER1, USER3, 40);
assertEq(cmtat.allowance(USER1, USER2), 60);
}
/// @dev CMTAT v3.2.0 documents that `Spend` does not imply an allowance reduction: with an
/// infinite approval OpenZeppelin leaves the allowance untouched, yet the event is still
/// emitted. Integrators must not track allowances from `Spend` alone.
function test_InfiniteAllowanceIsNotReducedButStillEmitsSpend() public {
vm.prank(USER1);
cmtat.approve(USER2, type(uint256).max);
vm.expectEmit(true, true, true, true);
emit IERC20Allowance.Spend(USER1, USER2, 100);
vm.prank(USER2);
cmtat.transferFrom(USER1, USER3, 100);
assertEq(cmtat.allowance(USER1, USER2), type(uint256).max);
assertEq(cmtat.balanceOf(USER3), 100);
}
}