Skip to content

Commit

Permalink
merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
hensha256 committed May 13, 2024
2 parents d82090e + 2ca3f3b commit f577721
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .forge-snapshots/simple swap with native.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
131082
117482
2 changes: 1 addition & 1 deletion .forge-snapshots/simple swap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
149441
132678
2 changes: 1 addition & 1 deletion src/PoolManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ contract PoolManager is IPoolManager, ProtocolFees, NoDelegateCall, ERC6909Claim

key.hooks.afterInitialize(key, sqrtPriceX96, tick, hookData);

// On intitalize we emit the key's fee, which tells us all fee settings a pool can have: either a static swap fee or dynamic swap fee and if the hook has enabled swap or withdraw fees.
// On initialize we emit the key's fee, which tells us all fee settings a pool can have: either a static swap fee or dynamic swap fee and if the hook has enabled swap or withdraw fees.
emit Initialize(id, key.currency0, key.currency1, key.fee, key.tickSpacing, key.hooks);
}

Expand Down
2 changes: 0 additions & 2 deletions src/test/PoolSwapTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import {PoolKey} from "../types/PoolKey.sol";
import {IHooks} from "../interfaces/IHooks.sol";
import {Hooks} from "../libraries/Hooks.sol";
import {PoolTestBase} from "./PoolTestBase.sol";
import {Hooks} from "../libraries/Hooks.sol";
import {IHooks} from "../interfaces/IHooks.sol";
import {CurrencySettleTake} from "../libraries/CurrencySettleTake.sol";

contract PoolSwapTest is PoolTestBase {
Expand Down
47 changes: 47 additions & 0 deletions src/test/SwapRouterNoChecks.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.24;

import {CurrencyLibrary, Currency} from "../types/Currency.sol";
import {IPoolManager} from "../interfaces/IPoolManager.sol";
import {BalanceDelta, BalanceDeltaLibrary} from "../types/BalanceDelta.sol";
import {PoolKey} from "../types/PoolKey.sol";
import {IHooks} from "../interfaces/IHooks.sol";
import {Hooks} from "../libraries/Hooks.sol";
import {PoolTestBase} from "./PoolTestBase.sol";
import {CurrencySettleTake} from "../libraries/CurrencySettleTake.sol";

contract SwapRouterNoChecks is PoolTestBase {
using CurrencyLibrary for Currency;
using CurrencySettleTake for Currency;
using Hooks for IHooks;

constructor(IPoolManager _manager) PoolTestBase(_manager) {}

error NoSwapOccurred();

struct CallbackData {
address sender;
PoolKey key;
IPoolManager.SwapParams params;
}

function swap(PoolKey memory key, IPoolManager.SwapParams memory params) external payable {
manager.unlock(abi.encode(CallbackData(msg.sender, key, params)));
}

function unlockCallback(bytes calldata rawData) external returns (bytes memory) {
require(msg.sender == address(manager));

CallbackData memory data = abi.decode(rawData, (CallbackData));

BalanceDelta delta = manager.swap(data.key, data.params, new bytes(0));

if (data.params.zeroForOne) {
data.key.currency0.settle(manager, data.sender, uint256(int256(-delta.amount0())), false);
data.key.currency1.take(manager, data.sender, uint256(int256(delta.amount1())), false);
} else {
data.key.currency1.settle(manager, data.sender, uint256(int256(-delta.amount1())), false);
data.key.currency0.take(manager, data.sender, uint256(int256(delta.amount0())), false);
}
}
}
18 changes: 16 additions & 2 deletions test/PoolManager.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -638,25 +638,39 @@ contract PoolManagerTest is Test, Deployers, GasSnapshot {
swapRouter.swap(key, swapParams, testSettings, ZERO_BYTES);
}

function test_swap_gas() public {
function test_swap_succeeds() public {
IPoolManager.SwapParams memory swapParams =
IPoolManager.SwapParams({zeroForOne: true, amountSpecified: -100, sqrtPriceLimitX96: SQRT_PRICE_1_2});

PoolSwapTest.TestSettings memory testSettings =
PoolSwapTest.TestSettings({takeClaims: false, settleUsingBurn: false});

swapRouter.swap(key, swapParams, testSettings, ZERO_BYTES);
}

function test_swap_gas() public {
IPoolManager.SwapParams memory swapParams =
IPoolManager.SwapParams({zeroForOne: true, amountSpecified: -100, sqrtPriceLimitX96: SQRT_PRICE_1_2});

swapRouterNoChecks.swap(key, swapParams);
snapLastCall("simple swap");
}

function test_swap_withNative_gas() public {
function test_swap_withNative_succeeds() public {
IPoolManager.SwapParams memory swapParams =
IPoolManager.SwapParams({zeroForOne: true, amountSpecified: -100, sqrtPriceLimitX96: SQRT_PRICE_1_2});

PoolSwapTest.TestSettings memory testSettings =
PoolSwapTest.TestSettings({takeClaims: false, settleUsingBurn: false});

swapRouter.swap{value: 100}(nativeKey, swapParams, testSettings, ZERO_BYTES);
}

function test_swap_withNative_gas() public {
IPoolManager.SwapParams memory swapParams =
IPoolManager.SwapParams({zeroForOne: true, amountSpecified: -100, sqrtPriceLimitX96: SQRT_PRICE_1_2});

swapRouterNoChecks.swap{value: 100}(nativeKey, swapParams);
snapLastCall("simple swap with native");
}

Expand Down
6 changes: 5 additions & 1 deletion test/utils/Deployers.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {Constants} from "../utils/Constants.sol";
import {SortTokens} from "./SortTokens.sol";
import {PoolModifyLiquidityTest} from "../../src/test/PoolModifyLiquidityTest.sol";
import {PoolSwapTest} from "../../src/test/PoolSwapTest.sol";
import {SwapRouterNoChecks} from "../../src/test/SwapRouterNoChecks.sol";
import {PoolDonateTest} from "../../src/test/PoolDonateTest.sol";
import {PoolNestedActionsTest} from "../../src/test/PoolNestedActionsTest.sol";
import {PoolTakeTest} from "../../src/test/PoolTakeTest.sol";
Expand Down Expand Up @@ -55,6 +56,7 @@ contract Deployers {
Currency internal currency1;
IPoolManager manager;
PoolModifyLiquidityTest modifyLiquidityRouter;
SwapRouterNoChecks swapRouterNoChecks;
PoolSwapTest swapRouter;
PoolDonateTest donateRouter;
PoolTakeTest takeRouter;
Expand Down Expand Up @@ -93,6 +95,7 @@ contract Deployers {
function deployFreshManagerAndRouters() internal {
deployFreshManager();
swapRouter = new PoolSwapTest(manager);
swapRouterNoChecks = new SwapRouterNoChecks(manager);
modifyLiquidityRouter = new PoolModifyLiquidityTest(manager);
donateRouter = new PoolDonateTest(manager);
takeRouter = new PoolTakeTest(manager);
Expand Down Expand Up @@ -122,8 +125,9 @@ contract Deployers {
function deployMintAndApproveCurrency() internal returns (Currency currency) {
MockERC20 token = deployTokens(1, 2 ** 255)[0];

address[6] memory toApprove = [
address[7] memory toApprove = [
address(swapRouter),
address(swapRouterNoChecks),
address(modifyLiquidityRouter),
address(donateRouter),
address(takeRouter),
Expand Down

0 comments on commit f577721

Please sign in to comment.