-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZeroMoonDifferential.t.sol
More file actions
346 lines (286 loc) · 12.2 KB
/
ZeroMoonDifferential.t.sol
File metadata and controls
346 lines (286 loc) · 12.2 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// SPDX-License-Identifier: MIT
pragma solidity 0.8.30;
import "forge-std/Test.sol";
import "../src/ZeroMoon.sol";
/**
* @title ZeroMoonDifferentialTest
* @notice Differential fuzzing: compares contract behavior vs reference model
* @dev Tests that on-chain calculations match off-chain reference implementation
*
* Run with: forge test --match-contract ZeroMoonDifferentialTest --fuzz-runs 100000
*/
contract ZeroMoonDifferentialTest is Test {
ZeroMoon public token;
address public owner;
address public dev;
address public user1;
uint256 public constant INITIAL_ETH = 10 ether;
// Reference model constants (matching contract)
uint256 private constant BASE_PRICE = 0.0001 ether;
uint256 private constant PRECISION_DIVISOR = 10000;
uint256 private constant EFFECTIVE_BACKING_NUMERATOR = 999;
uint256 private constant EFFECTIVE_BACKING_DENOMINATOR = 1000;
// Fee constants
uint256 private constant BUY_DEV_FEE_BPS = 5;
uint256 private constant BUY_RESERVE_FEE_BPS = 10;
uint256 private constant BUY_REFLECTION_FEE_BPS = 10;
uint256 private constant REFUND_DEV_FEE_BPS = 5;
uint256 private constant REFUND_REFLECTION_FEE_BPS = 5;
uint256 private constant REFUND_BURN_FEE_BPS = 75; // 0.75% = 75/10000
uint256 private constant REFUND_RESERVE_FEE_BPS_BEFORE_LIMIT = 75;
uint256 private constant REFUND_RESERVE_FEE_BPS_AFTER_LIMIT = 150;
function setUp() public {
owner = address(this);
dev = address(0x123);
user1 = address(0x456);
vm.deal(owner, 1000 ether);
vm.deal(user1, 100 ether);
token = new ZeroMoon{value: INITIAL_ETH}(owner, dev);
}
/**
* @notice Reference model: Calculate zETH for native (off-chain simulation)
* @dev Replicates contract's _getzETHForNative logic
*/
function reference_getzETHForNative(
uint256 nativeAmount,
uint256 contractBalance,
uint256 totalSupply,
uint256 contractTokenBalance
) private pure returns (uint256) {
if (nativeAmount == 0) return 0;
if (contractTokenBalance == 0) return 0;
uint256 circulating = totalSupply - contractTokenBalance;
uint256 pricePerToken;
if (circulating == 0 || contractBalance == 0) {
pricePerToken = BASE_PRICE;
} else {
uint256 refundPrice = (contractBalance * 1e18) / circulating;
pricePerToken = (refundPrice * 10010) / PRECISION_DIVISOR;
}
uint256 tokensToPurchase = (nativeAmount * 1e18) / pricePerToken;
return tokensToPurchase < contractTokenBalance ? tokensToPurchase : contractTokenBalance;
}
/**
* @notice Reference model: Calculate fees for buy
* @dev Replicates contract's fee calculation logic
*/
function reference_calculateBuyFees(uint256 zETHAmount) private pure returns (
uint256 devFee,
uint256 reserveFee,
uint256 reflectionFee,
uint256 netAmount
) {
devFee = (zETHAmount * BUY_DEV_FEE_BPS) / 10000;
reserveFee = (zETHAmount * BUY_RESERVE_FEE_BPS) / 10000;
reflectionFee = (zETHAmount * BUY_REFLECTION_FEE_BPS) / 10000;
unchecked {
netAmount = zETHAmount - devFee - reserveFee - reflectionFee;
}
}
/**
* @notice Reference model: Calculate fees for refund
* @dev Replicates contract's refund fee calculation logic
*/
function reference_calculateRefundFees(
uint256 zETHAmount,
uint256 totalBurned,
uint256 burningLimit
) private pure returns (
uint256 devFee,
uint256 reflectionFee,
uint256 burnFee,
uint256 reserveFee,
uint256 netAmount
) {
devFee = (zETHAmount * REFUND_DEV_FEE_BPS) / 10000;
reflectionFee = (zETHAmount * REFUND_REFLECTION_FEE_BPS) / 10000;
if (totalBurned < burningLimit) {
burnFee = (zETHAmount * REFUND_BURN_FEE_BPS) / 100000;
reserveFee = (zETHAmount * REFUND_RESERVE_FEE_BPS_BEFORE_LIMIT) / 100000;
} else {
burnFee = 0;
reserveFee = (zETHAmount * REFUND_RESERVE_FEE_BPS_AFTER_LIMIT) / 100000;
}
unchecked {
netAmount = zETHAmount - devFee - reflectionFee - burnFee - reserveFee;
}
}
/**
* @notice Reference model: Calculate native refund for zETH
* @dev Replicates contract's calculateNativeForZETH logic
*/
function reference_calculateNativeForZETH(
uint256 zETHAmount,
uint256 contractBalance,
uint256 totalSupply,
uint256 totalBurned,
uint256 contractTokenBalance,
uint256 burningLimit
) private pure returns (uint256) {
if (zETHAmount == 0) return 0;
if (zETHAmount < 1 ether) return 0; // Minimum refund
// Calculate fees
(uint256 devFee, uint256 reflectionFee, uint256 burnFee, uint256 reserveFee, uint256 netAmount) =
reference_calculateRefundFees(zETHAmount, totalBurned, burningLimit);
uint256 currentCirculatingSupply = (totalSupply - totalBurned) - contractTokenBalance + zETHAmount;
if (currentCirculatingSupply == 0) return 0;
uint256 effectiveBacking = (contractBalance * EFFECTIVE_BACKING_NUMERATOR) / EFFECTIVE_BACKING_DENOMINATOR;
// Use mulDiv equivalent (simplified for reference)
uint256 nativeToUser = (netAmount * effectiveBacking) / currentCirculatingSupply;
return nativeToUser;
}
/**
* @notice DIFFERENTIAL TEST: Buy calculation matches reference model
* @dev Compares contract's buy calculation vs off-chain reference
*/
function testFuzz_Differential_BuyCalculation(uint256 ethAmount) public {
ethAmount = bound(ethAmount, 0.0001 ether, 10 ether);
// Get contract state
uint256 contractBalance = address(token).balance;
uint256 totalSupply = token.totalSupply();
uint256 contractTokenBalance = token.balanceOf(address(token));
// Reference calculation
uint256 referenceTokens = reference_getzETHForNative(
ethAmount,
contractBalance,
totalSupply,
contractTokenBalance
);
// Contract calculation
uint256 contractTokens = token.calculatezETHForNative(ethAmount);
// Allow 1 wei tolerance for rounding differences
assertApproxEqAbs(
contractTokens,
referenceTokens,
1,
"Contract buy calculation should match reference model"
);
}
/**
* @notice DIFFERENTIAL TEST: Refund calculation matches reference model
* @dev Compares contract's refund calculation vs off-chain reference
*/
function testFuzz_Differential_RefundCalculation(uint256 zETHAmount) public {
// First, buy some tokens
vm.prank(user1);
token.buy{value: 1 ether}();
uint256 userBalance = token.balanceOf(user1);
// Skip if not enough tokens
if (userBalance < 1 ether) {
return;
}
zETHAmount = bound(zETHAmount, 1 ether, userBalance);
// Get contract state
uint256 contractBalance = address(token).balance;
uint256 totalSupply = token.totalSupply();
uint256 totalBurned = token.totalBurned();
uint256 contractTokenBalance = token.balanceOf(address(token));
uint256 burningLimit = token.BURNING_LIMIT();
// Reference calculation
uint256 referenceRefund = reference_calculateNativeForZETH(
zETHAmount,
contractBalance,
totalSupply,
totalBurned,
contractTokenBalance,
burningLimit
);
// Contract calculation
uint256 contractRefund = token.calculateNativeForZETH(zETHAmount);
// Allow 1% tolerance for rounding differences in mulDiv
uint256 tolerance = referenceRefund / 100;
if (tolerance < 1000 wei) tolerance = 1000 wei;
assertApproxEqAbs(
contractRefund,
referenceRefund,
tolerance,
"Contract refund calculation should match reference model"
);
}
/**
* @notice DIFFERENTIAL TEST: Buy fees match reference model
* @dev Compares actual buy fees vs reference calculation
*/
function testFuzz_Differential_BuyFees(uint256 ethAmount) public {
ethAmount = bound(ethAmount, 0.0001 ether, 10 ether);
// Calculate expected tokens
uint256 zETHToPurchase = token.calculatezETHForNative(ethAmount);
if (zETHToPurchase == 0) {
return;
}
// Reference fee calculation
(uint256 refDevFee, uint256 refReserveFee, uint256 refReflectionFee, uint256 refNetAmount) =
reference_calculateBuyFees(zETHToPurchase);
// Execute buy
uint256 userBalanceBefore = token.balanceOf(user1);
uint256 devBalanceBefore = token.balanceOf(dev);
vm.prank(user1);
token.buy{value: ethAmount}();
// Calculate actual fees
uint256 userReceived = token.balanceOf(user1) - userBalanceBefore;
uint256 devReceived = token.balanceOf(dev) - devBalanceBefore;
// Compare net amount (allow 1 wei tolerance)
assertApproxEqAbs(
userReceived,
refNetAmount,
1,
"User should receive net amount matching reference"
);
// Compare dev fee (allow 1 wei tolerance)
assertApproxEqAbs(
devReceived,
refDevFee,
1,
"Dev fee should match reference"
);
}
/**
* @notice DIFFERENTIAL TEST: Refund fees match reference model
* @dev Compares actual refund fees vs reference calculation
*/
function testFuzz_Differential_RefundFees(uint256 zETHAmount) public {
// Buy tokens first
vm.prank(user1);
token.buy{value: 1 ether}();
uint256 userBalance = token.balanceOf(user1);
if (userBalance < 1 ether) {
return;
}
zETHAmount = bound(zETHAmount, 1 ether, userBalance);
// Get state
uint256 totalBurned = token.totalBurned();
uint256 burningLimit = token.BURNING_LIMIT();
// Reference fee calculation
(uint256 refDevFee, uint256 refReflectionFee, uint256 refBurnFee, uint256 refReserveFee, uint256 refNetAmount) =
reference_calculateRefundFees(zETHAmount, totalBurned, burningLimit);
// Execute refund
uint256 userEthBefore = user1.balance;
uint256 devBalanceBefore = token.balanceOf(dev);
uint256 totalBurnedBefore = token.totalBurned();
vm.prank(user1);
token.transfer(address(token), zETHAmount);
// Calculate actual results
uint256 userEthReceived = user1.balance - userEthBefore;
uint256 devReceived = token.balanceOf(dev) - devBalanceBefore;
uint256 totalBurnedAfter = token.totalBurned();
uint256 actualBurned = totalBurnedAfter - totalBurnedBefore;
// Compare dev fee (allow 1 wei tolerance)
assertApproxEqAbs(
devReceived,
refDevFee,
1,
"Dev fee should match reference"
);
// Compare burn fee (allow 1 wei tolerance, but only if under limit)
if (totalBurnedBefore < burningLimit) {
assertApproxEqAbs(
actualBurned,
refBurnFee,
1,
"Burn fee should match reference"
);
} else {
assertEq(actualBurned, 0, "No burn after limit");
}
}
}