Skip to content

Commit 9189c82

Browse files
committed
updates async filler storage reads for isExecutor and asyncOrder
1 parent d52f4c7 commit 9189c82

File tree

5 files changed

+30
-16
lines changed

5 files changed

+30
-16
lines changed

src/AsyncSwapCSMM.sol

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,12 @@ contract AsyncSwapCSMM is BaseHook, IAsyncSwapAMM {
2626
using SafeCast for *;
2727
using CurrencySettler for Currency;
2828
using PoolIdLibrary for PoolKey;
29+
using AsyncFiller for AsyncOrder;
2930

30-
/// @notice Algorithm used for ordering transactions in our Async Swap AMM.
31-
IAlgorithm public algorithm;
3231
/// @notice Mapping to store async orders.
3332
mapping(PoolId poolId => AsyncFiller.State) public asyncOrders;
34-
/// @notice Mapping to store executor permissions for users.
35-
mapping(address owner => mapping(address executor => bool)) public setExecutor;
33+
/// Ordering algortim
34+
IAlgorithm public immutable algorithm;
3635

3736
/// Event emitted when a swap is executed.
3837
/// @param id The poolId of the pool where the swap occurred.
@@ -63,6 +62,9 @@ contract AsyncSwapCSMM is BaseHook, IAsyncSwapAMM {
6362
/// @inheritdoc BaseHook
6463
function _beforeInitialize(address, PoolKey calldata key, uint160) internal virtual override returns (bytes4) {
6564
require(key.fee == LPFeeLibrary.DYNAMIC_FEE_FLAG, "Dude use dynamic fees flag");
65+
/// set algorithm for the pool being initialized
66+
asyncOrders[key.toId()].algorithm = algorithm;
67+
asyncOrders[key.toId()].poolManager = poolManager;
6668
return this.beforeInitialize.selector;
6769
}
6870

@@ -96,13 +98,14 @@ contract AsyncSwapCSMM is BaseHook, IAsyncSwapAMM {
9698
revert UnsupportedLiquidity();
9799
}
98100

99-
/// @inheritdoc IAsyncSwapOrder
100-
function isExecutor(address owner, address executor) public view returns (bool) {
101-
return setExecutor[owner][executor];
101+
function asyncOrder(PoolId poolId, address user, bool zeroForOne) external view returns (uint256 claimable) {
102+
AsyncFiller.State storage state = asyncOrders[poolId];
103+
return state.asyncOrders[user][zeroForOne];
102104
}
103105

104-
function asyncOrder(PoolId poolId, address user, bool zeroForOne) external view returns (uint256 claimable) {
105-
return asyncOrders[poolId].asyncOrders[user][zeroForOne];
106+
function isExecutor(PoolId poolId, address user, address executor) external view returns (bool) {
107+
AsyncFiller.State storage state = asyncOrders[poolId];
108+
return state.setExecutor[user][executor];
106109
}
107110

108111
function calculateHookFee(uint256) public pure returns (uint256) {
@@ -120,7 +123,7 @@ contract AsyncSwapCSMM is BaseHook, IAsyncSwapAMM {
120123
for (uint8 i = 0; i < orders.length; i++) {
121124
AsyncOrder calldata order = orders[i];
122125
// Use transaction ordering algorithm to ensure correct execution order
123-
algorithm.orderingRule(order.zeroForOne, uint256(order.amountIn));
126+
asyncOrders[order.key.toId()].algorithm.orderingRule(order.zeroForOne, uint256(order.amountIn));
124127
this.executeOrder(order, userParams);
125128
}
126129
}
@@ -138,10 +141,10 @@ contract AsyncSwapCSMM is BaseHook, IAsyncSwapAMM {
138141

139142
/// TODO: Document what this does
140143
uint256 amountToFill = uint256(amountIn);
141-
// AsyncFiller.State storage _asyncOrders = asyncOrders[poolId];
142144
uint256 claimableAmount = asyncOrders[poolId].asyncOrders[owner][zeroForOne];
143145
require(amountToFill <= claimableAmount, "Max fill order limit exceed");
144-
require(isExecutor(owner, msg.sender), "Caller is valid not excutor");
146+
AsyncFiller.State storage state = asyncOrders[poolId];
147+
require(order.isExecutor(state, msg.sender), "Caller is valid not excutor");
145148

146149
/// @dev Transfer currency of async order to user
147150
Currency currencyTake;
@@ -190,7 +193,7 @@ contract AsyncSwapCSMM is BaseHook, IAsyncSwapAMM {
190193
/// @dev Take pool fee for LP
191194
uint256 feeAmount = calculatePoolFee(key.fee, amountTaken);
192195
uint256 finalTaken = amountTaken - feeAmount;
193-
setExecutor[hookData.user][hookData.executor] = true;
196+
asyncOrders[poolId].setExecutor[hookData.user][hookData.executor] = true;
194197
emit AsyncSwapOrder(poolId, hookData.user, params.zeroForOne, finalTaken.toInt256());
195198

196199
/// @dev Issue 1:1 claimableAmount - pool fee to user

src/interfaces/IAsyncSwapOrder.sol

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@ interface IAsyncSwapOrder {
4040
function asyncOrder(PoolId poolId, address user, bool zeroForOne) external view returns (uint256 claimable);
4141

4242
/// @notice Checks if the given executor is valid for the async order.
43+
/// @param poolId The poolId the executor is interacting with.
4344
/// @param owner The async order owner be checked against.
4445
/// @param executor The address of the executor to be checked.
4546
/// @return isExecutor True if the executor is valid for the async order, false otherwise.
46-
function isExecutor(address owner, address executor) external returns (bool);
47+
function isExecutor(PoolId poolId, address owner, address executor) external returns (bool);
4748

4849
}

src/libraries/AsyncFiller.sol

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ library AsyncFiller {
4343
/// @notice Error thrown when an order is of zero amount.
4444
error ZeroFillOrder();
4545

46+
/// @notice Checks if the given executor is valid for the async order.
47+
/// @param order The async order to be filled.
48+
/// @param self The storage slot that controls the state of orders.
49+
/// @param executor The address of the executor to be checked.
50+
/// @return isExecutor True if the executor is valid for the async order, false otherwise.
4651
function isExecutor(AsyncOrder calldata order, State storage self, address executor) internal view returns (bool) {
4752
return self.setExecutor[order.owner][executor];
4853
}

test/Algorithm2Test.t.sol

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ contract Algorithm2Test is SetupHook {
1515
algorithm = hook.algorithm();
1616
}
1717

18+
/// this should test the intialized algoritm was selected
19+
function testCheckSetAlgorithm() public { }
20+
1821
function testExecuteOrderBuy() public {
1922
// Test setup for a buy order
2023
bool zeroForOne = true;

test/AsyncCSMM.t.sol

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ pragma solidity ^0.8.13;
44
import { SetupHook } from "./SetupHook.sol";
55
import { AsyncSwapCSMM } from "@async-swap/AsyncSwapCSMM.sol";
66
import { IRouter } from "@async-swap/interfaces/IRouter.sol";
7+
import { AsyncFiller } from "@async-swap/libraries/AsyncFiller.sol";
78
import { AsyncOrder } from "@async-swap/types/AsyncOrder.sol";
89
import { console } from "forge-std/Test.sol";
910
import { Currency, IHooks, IPoolManager } from "v4-core/interfaces/IPoolManager.sol";
@@ -15,6 +16,7 @@ import { PoolKey } from "v4-core/types/PoolKey.sol";
1516
contract AsyncCsmmTest is SetupHook {
1617

1718
using CurrencyLibrary for Currency;
19+
using AsyncFiller for AsyncOrder;
1820

1921
address asyncFiller = makeAddr("asyncFiller");
2022
address user = makeAddr("user");
@@ -62,7 +64,7 @@ contract AsyncCsmmTest is SetupHook {
6264

6365
function testFuzzAsyncSwapAndFillOrder(address _user, uint256 amountIn, bool zeroForOne) public {
6466
vm.assume(amountIn >= 1);
65-
vm.assume(amountIn < 2 ** 96 / 2);
67+
vm.assume(amountIn < 2 ** 96);
6668
user = _user;
6769
topUp(user, amountIn);
6870
topUp(user2, amountIn);
@@ -87,7 +89,7 @@ contract AsyncCsmmTest is SetupHook {
8789
assertEq(balance0Before, balance0After);
8890
}
8991
assertEq(hook.asyncOrder(poolId, user, zeroForOne), amountIn);
90-
assertEq(hook.setExecutor(user, asyncFiller), true);
92+
assertEq(hook.isExecutor(poolId, user, asyncFiller), true);
9193

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

0 commit comments

Comments
 (0)