Skip to content

Commit

Permalink
fix beforeSwap return signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
saucepoint committed May 17, 2024
1 parent ffdc162 commit 50d3469
Show file tree
Hide file tree
Showing 14 changed files with 26 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeAddInitialLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
312768
312405
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeAddLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
124239
123876
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeFirstSwap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
83956
83810
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeInitialize.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1016591
1016569
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeRemoveLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
111650
111118
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeRemoveLiquidityAndRebalance.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
245587
244054
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeSecondSwap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
48918
48806
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeSwap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
82712
82505
2 changes: 1 addition & 1 deletion .forge-snapshots/TWAMMSubmitOrder.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
122544
122417
3 changes: 2 additions & 1 deletion contracts/BaseHook.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol";
import {IHooks} from "@uniswap/v4-core/src/interfaces/IHooks.sol";
import {BalanceDelta} from "@uniswap/v4-core/src/types/BalanceDelta.sol";
import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol";
import {BeforeSwapDelta} from "@uniswap/v4-core/src/types/BeforeSwapDelta.sol";

abstract contract BaseHook is IHooks {
error NotPoolManager();
Expand Down Expand Up @@ -112,7 +113,7 @@ abstract contract BaseHook is IHooks {
function beforeSwap(address, PoolKey calldata, IPoolManager.SwapParams calldata, bytes calldata)
external
virtual
returns (bytes4, int128)
returns (bytes4, BeforeSwapDelta, uint24)
{
revert HookNotImplemented();
}
Expand Down
5 changes: 3 additions & 2 deletions contracts/hooks/examples/FullRange.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {FixedPointMathLib} from "solmate/utils/FixedPointMathLib.sol";
import {IERC20Metadata} from "@openzeppelin/contracts/interfaces/IERC20Metadata.sol";
import {Strings} from "@openzeppelin/contracts/utils/Strings.sol";
import {StateLibrary} from "@uniswap/v4-core/src/libraries/StateLibrary.sol";
import {BeforeSwapDelta, BeforeSwapDeltaLibrary} from "@uniswap/v4-core/src/types/BeforeSwapDelta.sol";

import "../../libraries/LiquidityAmounts.sol";

Expand Down Expand Up @@ -243,7 +244,7 @@ contract FullRange is BaseHook, IUnlockCallback {
function beforeSwap(address, PoolKey calldata key, IPoolManager.SwapParams calldata, bytes calldata)
external
override
returns (bytes4, int128)
returns (bytes4, BeforeSwapDelta, uint24)
{
PoolId poolId = key.toId();

Expand All @@ -252,7 +253,7 @@ contract FullRange is BaseHook, IUnlockCallback {
pool.hasAccruedFees = true;
}

return (IHooks.beforeSwap.selector, 0);
return (IHooks.beforeSwap.selector, BeforeSwapDeltaLibrary.ZERO_DELTA, 0);
}

function modifyLiquidity(PoolKey memory key, IPoolManager.ModifyLiquidityParams memory params)
Expand Down
5 changes: 3 additions & 2 deletions contracts/hooks/examples/GeomeanOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {Oracle} from "../../libraries/Oracle.sol";
import {BaseHook} from "../../BaseHook.sol";
import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol";
import {StateLibrary} from "@uniswap/v4-core/src/libraries/StateLibrary.sol";
import {BeforeSwapDelta, BeforeSwapDeltaLibrary} from "@uniswap/v4-core/src/types/BeforeSwapDelta.sol";

/// @notice A hook for a pool that allows a Uniswap pool to act as an oracle. Pools that use this hook must have full range
/// tick spacing and liquidity is always permanently locked in these pools. This is the suggested configuration
Expand Down Expand Up @@ -146,10 +147,10 @@ contract GeomeanOracle is BaseHook {
external
override
poolManagerOnly
returns (bytes4, int128)
returns (bytes4, BeforeSwapDelta, uint24)
{
_updatePool(key);
return (GeomeanOracle.beforeSwap.selector, 0);
return (GeomeanOracle.beforeSwap.selector, BeforeSwapDeltaLibrary.ZERO_DELTA, 0);
}

/// @notice Observe the given pool for the timestamps
Expand Down
5 changes: 3 additions & 2 deletions contracts/hooks/examples/TWAMM.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {PoolGetters} from "../../libraries/PoolGetters.sol";
import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol";
import {CurrencySettleTake} from "@uniswap/v4-core/src/libraries/CurrencySettleTake.sol";
import {StateLibrary} from "@uniswap/v4-core/src/libraries/StateLibrary.sol";
import {BeforeSwapDelta, BeforeSwapDeltaLibrary} from "@uniswap/v4-core/src/types/BeforeSwapDelta.sol";

contract TWAMM is BaseHook, ITWAMM {
using TransferHelper for IERC20Minimal;
Expand Down Expand Up @@ -109,10 +110,10 @@ contract TWAMM is BaseHook, ITWAMM {
external
override
poolManagerOnly
returns (bytes4, int128)
returns (bytes4, BeforeSwapDelta, uint24)
{
executeTWAMMOrders(key);
return (BaseHook.beforeSwap.selector, 0);
return (BaseHook.beforeSwap.selector, BeforeSwapDeltaLibrary.ZERO_DELTA, 0);
}

function lastVirtualOrderTimestamp(PoolId key) external view returns (uint256) {
Expand Down
11 changes: 6 additions & 5 deletions test/LimitOrder.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ contract TestLimitOrder is Test, Deployers {
uint128 liquidity = 1000000;
limitOrder.place(key, tickLower, zeroForOne, liquidity);
assertTrue(EpochLibrary.equals(limitOrder.getEpoch(key, tickLower, zeroForOne), Epoch.wrap(1)));
assertEq(manager.getLiquidity(id, address(limitOrder), tickLower, tickLower + 60, 0), liquidity);

assertEq(manager.getPosition(id, address(limitOrder), tickLower, tickLower + 60, 0).liquidity, liquidity);
}

function testZeroForOneLeftBoundaryOfCurrentRange() public {
Expand All @@ -93,7 +94,7 @@ contract TestLimitOrder is Test, Deployers {
uint128 liquidity = 1000000;
limitOrder.place(key, tickLower, zeroForOne, liquidity);
assertTrue(EpochLibrary.equals(limitOrder.getEpoch(key, tickLower, zeroForOne), Epoch.wrap(1)));
assertEq(manager.getLiquidity(id, address(limitOrder), tickLower, tickLower + 60, 0), liquidity);
assertEq(manager.getPosition(id, address(limitOrder), tickLower, tickLower + 60, 0).liquidity, liquidity);
}

function testZeroForOneCrossedRangeRevert() public {
Expand All @@ -119,7 +120,7 @@ contract TestLimitOrder is Test, Deployers {
uint128 liquidity = 1000000;
limitOrder.place(key, tickLower, zeroForOne, liquidity);
assertTrue(EpochLibrary.equals(limitOrder.getEpoch(key, tickLower, zeroForOne), Epoch.wrap(1)));
assertEq(manager.getPosition(id, address(limitOrder), tickLower, tickLower + 60, 0), liquidity);
assertEq(manager.getPosition(id, address(limitOrder), tickLower, tickLower + 60, 0).liquidity, liquidity);
}

function testNotZeroForOneCrossedRangeRevert() public {
Expand Down Expand Up @@ -153,7 +154,7 @@ contract TestLimitOrder is Test, Deployers {
limitOrder.place(key, tickLower, zeroForOne, liquidity);
vm.stopPrank();
assertTrue(EpochLibrary.equals(limitOrder.getEpoch(key, tickLower, zeroForOne), Epoch.wrap(1)));
assertEq(manager.getLiquidity(id, address(limitOrder), tickLower, tickLower + 60, 0), liquidity * 2);
assertEq(manager.getPosition(id, address(limitOrder), tickLower, tickLower + 60, 0).liquidity, liquidity * 2);

(
bool filled,
Expand Down Expand Up @@ -207,7 +208,7 @@ contract TestLimitOrder is Test, Deployers {
assertTrue(filled);
assertEq(token0Total, 0);
assertEq(token1Total, 2996 + 17); // 3013, 2 wei of dust
assertEq(manager.getLiquidity(id, address(limitOrder), tickLower, tickLower + 60, 0), 0);
assertEq(manager.getPosition(id, address(limitOrder), tickLower, tickLower + 60, 0).liquidity, 0);

vm.expectEmit(true, true, true, true, address(token1));
emit Transfer(address(manager), new GetSender().sender(), 2996 + 17);
Expand Down

0 comments on commit 50d3469

Please sign in to comment.