Skip to content

Commit 20f1bbf

Browse files
committed
update test to a fuzz test
1 parent 1aeea25 commit 20f1bbf

File tree

1 file changed

+48
-47
lines changed

1 file changed

+48
-47
lines changed

test/AsyncCSMM.t.sol

Lines changed: 48 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ contract AsyncCsmmTest is SetupHook {
2727
asyncFiller = address(router);
2828
}
2929

30-
modifier userAction() {
31-
vm.startPrank(user);
30+
modifier userAction(address _user) {
31+
vm.startPrank(_user);
3232
_;
3333
vm.stopPrank();
3434
}
@@ -38,81 +38,82 @@ contract AsyncCsmmTest is SetupHook {
3838
token1.transfer(_user, amount);
3939
}
4040

41-
function testAsyncSwap() public {
42-
uint256 balance0Before = currency0.balanceOf(user);
43-
uint256 balance1Before = currency1.balanceOf(user);
41+
function swap(address _user, address _asyncFiller, AsyncOrder memory order) public {
42+
vm.startPrank(_user);
43+
if (order.zeroForOne) {
44+
token0.approve(address(router), order.amountIn);
45+
} else {
46+
token1.approve(address(router), order.amountIn);
47+
}
48+
router.swap(order, abi.encode(user, _asyncFiller));
49+
vm.stopPrank();
50+
}
4451

45-
// Perform a test swap //
46-
uint256 amount = 1e18;
47-
bool zeroForOne = true;
48-
vm.startPrank(user);
49-
if (zeroForOne) {
50-
token0.approve(address(router), amount);
52+
function fillOrder(address _user, AsyncOrder memory order, address _asyncFiller) public {
53+
vm.startPrank(_user);
54+
if (order.zeroForOne) {
55+
token1.approve(address(router), order.amountIn);
5156
} else {
52-
token1.approve(address(router), amount);
57+
token0.approve(address(router), order.amountIn);
5358
}
59+
router.fillOrder(order, abi.encode(_asyncFiller));
60+
vm.stopPrank();
61+
}
62+
63+
function testFuzzAsyncSwap(uint256 amount, bool zeroForOne) public {
64+
vm.assume(amount >= 1);
65+
vm.assume(amount <= 1 ether);
5466

5567
AsyncOrder memory order =
5668
AsyncOrder({ key: key, owner: user, zeroForOne: zeroForOne, amountIn: amount, sqrtPrice: 2 ** 96 });
5769

58-
router.swap(order, abi.encode(user, asyncFiller));
59-
vm.stopPrank();
60-
// ------------------- //
70+
uint256 balance0Before = currency0.balanceOf(user);
71+
uint256 balance1Before = currency1.balanceOf(user);
72+
73+
// swap
74+
swap(user, asyncFiller, order);
6175

6276
uint256 balance0After = currency0.balanceOf(user);
6377
uint256 balance1After = currency1.balanceOf(user);
6478

65-
// user paid token0
66-
assertEq(balance0Before - balance0After, amount);
67-
68-
// user did not recieve token1 (AsyncSwap)
69-
assertEq(balance1Before, balance1After);
70-
71-
// user received a claimable balance
79+
if (zeroForOne) {
80+
assertEq(balance0Before - balance0After, amount);
81+
assertEq(balance1Before, balance1After);
82+
} else {
83+
assertEq(balance1Before - balance1After, amount);
84+
assertEq(balance0Before, balance0After);
85+
}
7286
assertEq(hook.asyncOrders(poolId, user, zeroForOne), amount);
73-
74-
// check executor
7587
assertEq(hook.setExecutor(user, asyncFiller), true);
7688

7789
balance0Before = currency0.balanceOf(user2);
7890
balance1Before = currency1.balanceOf(user2);
7991

80-
vm.startPrank(user2);
81-
// User 2 does not event need to add liquidity to fill user 1's async order
82-
// token0.approve(address(hook), amount);
83-
// token1.approve(address(hook), amount);
84-
// router.addLiquidity(key, amount, amount);
85-
86-
// User 2 (LP) decides to fill user 1's order using router
87-
if (zeroForOne) {
88-
token1.approve(address(router), amount);
89-
} else {
90-
token0.approve(address(router), amount);
91-
}
92-
router.fillOrder(order, abi.encode(asyncFiller));
93-
vm.stopPrank();
92+
// fill
93+
fillOrder(user2, order, asyncFiller);
9494

9595
balance0After = currency0.balanceOf(user2);
9696
balance1After = currency1.balanceOf(user2);
9797

98-
// user 2 balance 0 remained the same
99-
assertEq(balance0Before, balance0After);
100-
// user 2 balance increased
101-
assertEq(balance1Before - balance1After, amount);
102-
103-
// user can:
104-
assertEq(hook.asyncOrders(poolId, user, zeroForOne), 0);
98+
if (zeroForOne) {
99+
assertEq(balance0Before, balance0After);
100+
assertEq(balance1Before - balance1After, amount);
101+
assertEq(hook.asyncOrders(poolId, user, zeroForOne), 0);
102+
} else {
103+
assertEq(balance1Before, balance1After);
104+
assertEq(balance0Before - balance0After, amount);
105+
assertEq(hook.asyncOrders(poolId, user, zeroForOne), 0);
106+
}
105107
if (zeroForOne) {
106108
assertEq(manager.balanceOf(user, currency0.toId()), uint256(amount));
107109
} else {
108110
assertEq(manager.balanceOf(user, currency1.toId()), uint256(amount));
109111
}
110112
}
111113

112-
function testFuzzAsyncSwapOrder(bool zeroForOne, uint256 amount, bool settleUsingBurn) public userAction {
114+
function testFuzzAsyncSwapOrder(bool zeroForOne, uint256 amount) public userAction(user) {
113115
vm.assume(amount >= 1);
114116
vm.assume(amount <= 1 ether);
115-
vm.assume(settleUsingBurn == false);
116117

117118
uint256 balance0Before = manager.balanceOf(address(hook), currency0.toId());
118119
uint256 balance1Before = manager.balanceOf(address(hook), currency0.toId());

0 commit comments

Comments
 (0)