Skip to content

Commit a9f4661

Browse files
committed
reusable aysnc ordering algo
1 parent 14720f3 commit a9f4661

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

script/01_DeployHook.s.sol

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ pragma solidity ^0.8.13;
33

44
import { FFIHelper } from "./FFIHelper.sol";
55
import { AsyncSwap } from "@async-swap/AsyncSwap.sol";
6+
import { CLVR } from "@async-swap/algorithms/clvr.sol";
7+
import { IAlgorithm } from "@async-swap/interfaces/IAlgorithm.sol";
68
import { Router } from "@async-swap/router.sol";
79
import { IPoolManager } from "v4-core/interfaces/IPoolManager.sol";
810
import { Hooks } from "v4-core/libraries/Hooks.sol";
@@ -14,6 +16,7 @@ contract DeployHookScript is FFIHelper {
1416
IPoolManager manager;
1517
AsyncSwap public hook;
1618
Router router;
19+
IAlgorithm algo;
1720

1821
function setUp() public {
1922
manager = IPoolManager(_getDeployedPoolManager());
@@ -33,7 +36,8 @@ contract DeployHookScript is FFIHelper {
3336
HookMiner.find(CREATE2_FACTORY, hookFlags, type(AsyncSwap).creationCode, abi.encode(address(manager)));
3437

3538
/// @dev deploy hook
36-
hook = new AsyncSwap{ salt: salt }(manager);
39+
algo = new CLVR(hookAddress);
40+
hook = new AsyncSwap{ salt: salt }(manager, algo);
3741
assert(address(hook) == hookAddress);
3842

3943
router = new Router(manager, hook);

src/AsyncSwap.sol

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// SPDX-License-Identifier: GPL-2.0-or-later
22
pragma solidity 0.8.26;
33

4-
import { CLVR } from "@async-swap/algorithms/clvr.sol";
54
import { IAlgorithm } from "@async-swap/interfaces/IAlgorithm.sol";
65
import { IAsyncSwapAMM } from "@async-swap/interfaces/IAsyncSwapAMM.sol";
76
import { AsyncFiller } from "@async-swap/libraries/AsyncFiller.sol";
@@ -18,8 +17,7 @@ import { PoolIdLibrary, PoolKey } from "v4-core/types/PoolKey.sol";
1817
import { BaseHook } from "v4-periphery/src/utils/BaseHook.sol";
1918

2019
/// @title Async Swap Contract
21-
/// @author Async Labs
22-
/// @notice Async swap AMM
20+
/// @author Asyncswap Labs
2321
contract AsyncSwap is BaseHook, IAsyncSwapAMM {
2422

2523
using SafeCast for *;
@@ -53,16 +51,18 @@ contract AsyncSwap is BaseHook, IAsyncSwapAMM {
5351

5452
/// Initializes the Async Swap Hook contract with the PoolManager address and sets an transaction ordering algorithm.
5553
/// @param poolManager The address of the PoolManager contract.
56-
constructor(IPoolManager poolManager) BaseHook(poolManager) {
57-
ALGORITHM = new CLVR(address(this));
54+
/// @param orderingAlgorithm The address of the ordering algorithm
55+
constructor(IPoolManager poolManager, IAlgorithm orderingAlgorithm) BaseHook(poolManager) {
56+
ALGORITHM = orderingAlgorithm;
5857
}
5958

6059
/// @inheritdoc BaseHook
6160
function _beforeInitialize(address, PoolKey calldata key, uint160) internal virtual override returns (bytes4) {
62-
require(key.fee == LPFeeLibrary.DYNAMIC_FEE_FLAG, "Dude use dynamic fees flag");
63-
/// set algorithm for the pool being initialized
61+
require(key.fee == LPFeeLibrary.DYNAMIC_FEE_FLAG, "Use dynamic fees flag");
62+
/// Set library state for the pool being initialized
6463
asyncOrders[key.toId()].algorithm = ALGORITHM;
6564
asyncOrders[key.toId()].poolManager = poolManager;
65+
asyncOrders[key.toId()].asyncOrder;
6666
return this.beforeInitialize.selector;
6767
}
6868

test/SetupHook.t.sol

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
pragma solidity ^0.8.13;
33

44
import { AsyncSwap } from "@async-swap/AsyncSwap.sol";
5+
import { CLVR } from "@async-swap/algorithms/clvr.sol";
6+
import { IAlgorithm } from "@async-swap/interfaces/IAlgorithm.sol";
57
import { Router } from "@async-swap/router.sol";
68
import { Test } from "forge-std/Test.sol";
79
import { MockERC20 } from "solmate/src/test/utils/mocks/MockERC20.sol";
@@ -26,6 +28,7 @@ contract SetupHook is Test {
2628
Currency currency1;
2729
PoolId poolId;
2830
Router router;
31+
IAlgorithm algo;
2932

3033
function setUp() public virtual {
3134
deployPoolManager();
@@ -73,7 +76,9 @@ contract SetupHook is Test {
7376
| Hooks.BEFORE_SWAP_RETURNS_DELTA_FLAG
7477
);
7578
vm.startPrank(owner);
76-
deployCodeTo("AsyncSwap.sol", abi.encode(manager), address(hookFlags));
79+
80+
algo = new CLVR(address(hookFlags));
81+
deployCodeTo("AsyncSwap.sol", abi.encode(manager, algo), address(hookFlags));
7782
hook = AsyncSwap(address(hookFlags));
7883
}
7984

0 commit comments

Comments
 (0)