Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 55 additions & 53 deletions test/AsyncCSMM.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ contract AsyncCsmmTest is SetupHook {
asyncFiller = address(router);
}

modifier userAction() {
vm.startPrank(user);
modifier userAction(address _user) {
vm.startPrank(_user);
_;
vm.stopPrank();
}
Expand All @@ -38,81 +38,83 @@ contract AsyncCsmmTest is SetupHook {
token1.transfer(_user, amount);
}

function testAsyncSwap() public {
uint256 balance0Before = currency0.balanceOf(user);
uint256 balance1Before = currency1.balanceOf(user);

// Perform a test swap //
uint256 amount = 1e18;
bool zeroForOne = true;
vm.startPrank(user);
if (zeroForOne) {
token0.approve(address(router), amount);
function swap(address _user, address _asyncFiller, AsyncOrder memory order) public {
vm.startPrank(_user);
if (order.zeroForOne) {
token0.approve(address(router), order.amountIn);
} else {
token1.approve(address(router), amount);
token1.approve(address(router), order.amountIn);
}
router.swap(order, abi.encode(user, _asyncFiller));
vm.stopPrank();
}

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

router.swap(order, abi.encode(user, asyncFiller));
function fillOrder(address _user, AsyncOrder memory order, address _asyncFiller) public {
vm.startPrank(_user);
if (order.zeroForOne) {
token1.approve(address(router), order.amountIn);
} else {
token0.approve(address(router), order.amountIn);
}
router.fillOrder(order, abi.encode(_asyncFiller));
vm.stopPrank();
// ------------------- //
}

uint256 balance0After = currency0.balanceOf(user);
uint256 balance1After = currency1.balanceOf(user);
function testFuzzAsyncSwap(AsyncOrder memory order) public {
vm.assume(order.amountIn >= 1);
vm.assume(order.amountIn < 2 ** 128 / 2);
topUp(order.owner, order.amountIn);
topUp(user2, order.amountIn);
user = order.owner;
order.key = key;

// user paid token0
assertEq(balance0Before - balance0After, amount);
uint256 balance0Before = currency0.balanceOf(user);
uint256 balance1Before = currency1.balanceOf(user);

// user did not recieve token1 (AsyncSwap)
assertEq(balance1Before, balance1After);
// swap
swap(user, asyncFiller, order);

// user received a claimable balance
assertEq(hook.asyncOrders(poolId, user, zeroForOne), amount);
uint256 balance0After = currency0.balanceOf(user);
uint256 balance1After = currency1.balanceOf(user);

// check executor
if (order.zeroForOne) {
assertEq(balance0Before - balance0After, order.amountIn);
assertEq(balance1Before, balance1After);
} else {
assertEq(balance1Before - balance1After, order.amountIn);
assertEq(balance0Before, balance0After);
}
assertEq(hook.asyncOrders(poolId, user, order.zeroForOne), order.amountIn);
assertEq(hook.setExecutor(user, asyncFiller), true);

balance0Before = currency0.balanceOf(user2);
balance1Before = currency1.balanceOf(user2);

vm.startPrank(user2);
// User 2 does not event need to add liquidity to fill user 1's async order
// token0.approve(address(hook), amount);
// token1.approve(address(hook), amount);
// router.addLiquidity(key, amount, amount);

// User 2 (LP) decides to fill user 1's order using router
if (zeroForOne) {
token1.approve(address(router), amount);
} else {
token0.approve(address(router), amount);
}
router.fillOrder(order, abi.encode(asyncFiller));
vm.stopPrank();
// fill
fillOrder(user2, order, asyncFiller);

balance0After = currency0.balanceOf(user2);
balance1After = currency1.balanceOf(user2);

// user 2 balance 0 remained the same
assertEq(balance0Before, balance0After);
// user 2 balance increased
assertEq(balance1Before - balance1After, amount);

// user can:
assertEq(hook.asyncOrders(poolId, user, zeroForOne), 0);
if (zeroForOne) {
assertEq(manager.balanceOf(user, currency0.toId()), uint256(amount));
if (order.zeroForOne) {
assertEq(balance0Before, balance0After);
assertEq(balance1Before - balance1After, order.amountIn);
assertEq(hook.asyncOrders(poolId, user, order.zeroForOne), 0);
} else {
assertEq(balance1Before, balance1After);
assertEq(balance0Before - balance0After, order.amountIn);
assertEq(hook.asyncOrders(poolId, user, order.zeroForOne), 0);
}
if (order.zeroForOne) {
assertEq(manager.balanceOf(user, currency0.toId()), uint256(order.amountIn));
} else {
assertEq(manager.balanceOf(user, currency1.toId()), uint256(amount));
assertEq(manager.balanceOf(user, currency1.toId()), uint256(order.amountIn));
}
}

function testFuzzAsyncSwapOrder(bool zeroForOne, uint256 amount, bool settleUsingBurn) public userAction {
function testFuzzAsyncSwapOrder(bool zeroForOne, uint256 amount) public userAction(user) {
vm.assume(amount >= 1);
vm.assume(amount <= 1 ether);
vm.assume(settleUsingBurn == false);

uint256 balance0Before = manager.balanceOf(address(hook), currency0.toId());
uint256 balance1Before = manager.balanceOf(address(hook), currency0.toId());
Expand Down
4 changes: 2 additions & 2 deletions test/SetupHook.sol
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ contract SetupHook is Test {
}

function mint() public ownerAction {
token0.mint(owner, 100 ether);
token1.mint(owner, 100 ether);
token0.mint(owner, 2 ** 128 - 1);
token1.mint(owner, 2 ** 128 - 1);
}

function deployPoolManager() public {
Expand Down