Skip to content

Commit e51f74a

Browse files
authored
Merge pull request #15 from classcool/mmsaki/asynswap-tests
update mmsaki/asynswap tests
2 parents 1aeea25 + 4fc52da commit e51f74a

File tree

2 files changed

+57
-55
lines changed

2 files changed

+57
-55
lines changed

test/AsyncCSMM.t.sol

Lines changed: 55 additions & 53 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,83 @@ 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);
44-
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);
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);
5145
} else {
52-
token1.approve(address(router), amount);
46+
token1.approve(address(router), order.amountIn);
5347
}
48+
router.swap(order, abi.encode(user, _asyncFiller));
49+
vm.stopPrank();
50+
}
5451

55-
AsyncOrder memory order =
56-
AsyncOrder({ key: key, owner: user, zeroForOne: zeroForOne, amountIn: amount, sqrtPrice: 2 ** 96 });
57-
58-
router.swap(order, abi.encode(user, asyncFiller));
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);
56+
} else {
57+
token0.approve(address(router), order.amountIn);
58+
}
59+
router.fillOrder(order, abi.encode(_asyncFiller));
5960
vm.stopPrank();
60-
// ------------------- //
61+
}
6162

62-
uint256 balance0After = currency0.balanceOf(user);
63-
uint256 balance1After = currency1.balanceOf(user);
63+
function testFuzzAsyncSwap(AsyncOrder memory order) public {
64+
vm.assume(order.amountIn >= 1);
65+
vm.assume(order.amountIn < 2 ** 128 / 2);
66+
topUp(order.owner, order.amountIn);
67+
topUp(user2, order.amountIn);
68+
user = order.owner;
69+
order.key = key;
6470

65-
// user paid token0
66-
assertEq(balance0Before - balance0After, amount);
71+
uint256 balance0Before = currency0.balanceOf(user);
72+
uint256 balance1Before = currency1.balanceOf(user);
6773

68-
// user did not recieve token1 (AsyncSwap)
69-
assertEq(balance1Before, balance1After);
74+
// swap
75+
swap(user, asyncFiller, order);
7076

71-
// user received a claimable balance
72-
assertEq(hook.asyncOrders(poolId, user, zeroForOne), amount);
77+
uint256 balance0After = currency0.balanceOf(user);
78+
uint256 balance1After = currency1.balanceOf(user);
7379

74-
// check executor
80+
if (order.zeroForOne) {
81+
assertEq(balance0Before - balance0After, order.amountIn);
82+
assertEq(balance1Before, balance1After);
83+
} else {
84+
assertEq(balance1Before - balance1After, order.amountIn);
85+
assertEq(balance0Before, balance0After);
86+
}
87+
assertEq(hook.asyncOrders(poolId, user, order.zeroForOne), order.amountIn);
7588
assertEq(hook.setExecutor(user, asyncFiller), true);
7689

7790
balance0Before = currency0.balanceOf(user2);
7891
balance1Before = currency1.balanceOf(user2);
7992

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();
93+
// fill
94+
fillOrder(user2, order, asyncFiller);
9495

9596
balance0After = currency0.balanceOf(user2);
9697
balance1After = currency1.balanceOf(user2);
9798

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);
105-
if (zeroForOne) {
106-
assertEq(manager.balanceOf(user, currency0.toId()), uint256(amount));
99+
if (order.zeroForOne) {
100+
assertEq(balance0Before, balance0After);
101+
assertEq(balance1Before - balance1After, order.amountIn);
102+
assertEq(hook.asyncOrders(poolId, user, order.zeroForOne), 0);
103+
} else {
104+
assertEq(balance1Before, balance1After);
105+
assertEq(balance0Before - balance0After, order.amountIn);
106+
assertEq(hook.asyncOrders(poolId, user, order.zeroForOne), 0);
107+
}
108+
if (order.zeroForOne) {
109+
assertEq(manager.balanceOf(user, currency0.toId()), uint256(order.amountIn));
107110
} else {
108-
assertEq(manager.balanceOf(user, currency1.toId()), uint256(amount));
111+
assertEq(manager.balanceOf(user, currency1.toId()), uint256(order.amountIn));
109112
}
110113
}
111114

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

117119
uint256 balance0Before = manager.balanceOf(address(hook), currency0.toId());
118120
uint256 balance1Before = manager.balanceOf(address(hook), currency0.toId());

test/SetupHook.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ contract SetupHook is Test {
4848
}
4949

5050
function mint() public ownerAction {
51-
token0.mint(owner, 100 ether);
52-
token1.mint(owner, 100 ether);
51+
token0.mint(owner, 2 ** 128 - 1);
52+
token1.mint(owner, 2 ** 128 - 1);
5353
}
5454

5555
function deployPoolManager() public {

0 commit comments

Comments
 (0)