Skip to content

Commit

Permalink
Fix contracts and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
qiwu7 committed May 15, 2024
1 parent 923d4d7 commit f278297
Show file tree
Hide file tree
Showing 15 changed files with 41 additions and 41 deletions.
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeAddInitialLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
384735
384757
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeAddLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
179102
179124
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeFirstSwap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
128152
128196
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeRemoveLiquidityAndRebalance.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
345919
345941
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeSecondSwap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
89081
89125
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeSwap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
126954
126998
2 changes: 1 addition & 1 deletion .forge-snapshots/TWAMMSubmitOrder.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
122845
122867
2 changes: 1 addition & 1 deletion contracts/BaseHook.sol
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ abstract contract BaseHook is IHooks {
Hooks.validateHookPermissions(_this, getHookPermissions());
}

function lockAcquired(bytes calldata data) external virtual poolManagerOnly returns (bytes memory) {
function unlockCallback(bytes calldata data) external virtual poolManagerOnly returns (bytes memory) {
(bool success, bytes memory returnData) = address(this).call(data);
if (success) return returnData;
if (returnData.length == 0) revert LockFailure();
Expand Down
10 changes: 5 additions & 5 deletions contracts/hooks/examples/FullRange.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {CurrencyLibrary, Currency} from "@uniswap/v4-core/src/types/Currency.sol
import {TickMath} from "@uniswap/v4-core/src/libraries/TickMath.sol";
import {BalanceDelta} from "@uniswap/v4-core/src/types/BalanceDelta.sol";
import {IERC20Minimal} from "@uniswap/v4-core/src/interfaces/external/IERC20Minimal.sol";
import {ILockCallback} from "@uniswap/v4-core/src/interfaces/callback/ILockCallback.sol";
import {IUnlockCallback} from "@uniswap/v4-core/src/interfaces/callback/IUnlockCallback.sol";
import {PoolId, PoolIdLibrary} from "@uniswap/v4-core/src/types/PoolId.sol";
import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol";
import {FullMath} from "@uniswap/v4-core/src/libraries/FullMath.sol";
Expand All @@ -23,7 +23,7 @@ import {Strings} from "@openzeppelin/contracts/utils/Strings.sol";

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

contract FullRange is BaseHook, ILockCallback {
contract FullRange is BaseHook, IUnlockCallback {
using CurrencyLibrary for Currency;
using PoolIdLibrary for PoolKey;
using SafeCast for uint256;
Expand Down Expand Up @@ -249,7 +249,7 @@ contract FullRange is BaseHook, ILockCallback {
internal
returns (BalanceDelta delta)
{
delta = abi.decode(poolManager.lock(abi.encode(CallbackData(msg.sender, key, params))), (BalanceDelta));
delta = abi.decode(poolManager.unlock(abi.encode(CallbackData(msg.sender, key, params))), (BalanceDelta));
}

function _settleDeltas(address sender, PoolKey memory key, BalanceDelta delta) internal {
Expand Down Expand Up @@ -297,9 +297,9 @@ contract FullRange is BaseHook, ILockCallback {
pool.hasAccruedFees = false;
}

function lockAcquired(bytes calldata rawData)
function unlockCallback(bytes calldata rawData)
external
override(ILockCallback, BaseHook)
override(IUnlockCallback, BaseHook)
poolManagerOnly
returns (bytes memory)
{
Expand Down
22 changes: 11 additions & 11 deletions contracts/hooks/examples/LimitOrder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ contract LimitOrder is BaseHook {
epochInfo.filled = true;

(uint256 amount0, uint256 amount1) =
_lockAcquiredFill(key, lower, -int256(uint256(epochInfo.liquidityTotal)));
_unlockCallbackFill(key, lower, -int256(uint256(epochInfo.liquidityTotal)));

unchecked {
epochInfo.token0Total += amount0;
Expand Down Expand Up @@ -187,7 +187,7 @@ contract LimitOrder is BaseHook {
}
}

function _lockAcquiredFill(PoolKey calldata key, int24 tickLower, int256 liquidityDelta)
function _unlockCallbackFill(PoolKey calldata key, int24 tickLower, int256 liquidityDelta)
private
poolManagerOnly
returns (uint128 amount0, uint128 amount1)
Expand Down Expand Up @@ -216,8 +216,8 @@ contract LimitOrder is BaseHook {
{
if (liquidity == 0) revert ZeroLiquidity();

poolManager.lock(
abi.encodeCall(this.lockAcquiredPlace, (key, tickLower, zeroForOne, int256(uint256(liquidity)), msg.sender))
poolManager.unlock(
abi.encodeCall(this.unlockCallbackPlace, (key, tickLower, zeroForOne, int256(uint256(liquidity)), msg.sender))
);

EpochInfo storage epochInfo;
Expand Down Expand Up @@ -245,7 +245,7 @@ contract LimitOrder is BaseHook {
emit Place(msg.sender, epoch, key, tickLower, zeroForOne, liquidity);
}

function lockAcquiredPlace(
function unlockCallbackPlace(
PoolKey calldata key,
int24 tickLower,
bool zeroForOne,
Expand Down Expand Up @@ -297,9 +297,9 @@ contract LimitOrder is BaseHook {
uint256 amount0Fee;
uint256 amount1Fee;
(amount0, amount1, amount0Fee, amount1Fee) = abi.decode(
poolManager.lock(
poolManager.unlock(
abi.encodeCall(
this.lockAcquiredKill,
this.unlockCallbackKill,
(key, tickLower, -int256(uint256(liquidity)), to, liquidity == epochInfo.liquidityTotal)
)
),
Expand All @@ -314,7 +314,7 @@ contract LimitOrder is BaseHook {
emit Kill(msg.sender, epoch, key, tickLower, zeroForOne, liquidity);
}

function lockAcquiredKill(
function unlockCallbackKill(
PoolKey calldata key,
int24 tickLower,
int256 liquidityDelta,
Expand Down Expand Up @@ -378,14 +378,14 @@ contract LimitOrder is BaseHook {
epochInfo.token1Total -= amount1;
epochInfo.liquidityTotal = liquidityTotal - liquidity;

poolManager.lock(
abi.encodeCall(this.lockAcquiredWithdraw, (epochInfo.currency0, epochInfo.currency1, amount0, amount1, to))
poolManager.unlock(
abi.encodeCall(this.unlockCallbackWithdraw, (epochInfo.currency0, epochInfo.currency1, amount0, amount1, to))
);

emit Withdraw(msg.sender, epoch, liquidity);
}

function lockAcquiredWithdraw(
function unlockCallbackWithdraw(
Currency currency0,
Currency currency1,
uint256 token0Amount,
Expand Down
4 changes: 2 additions & 2 deletions contracts/hooks/examples/TWAMM.sol
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ contract TWAMM is BaseHook, ITWAMM {
);

if (sqrtPriceLimitX96 != 0 && sqrtPriceLimitX96 != sqrtPriceX96) {
poolManager.lock(abi.encode(key, IPoolManager.SwapParams(zeroForOne, type(int256).max, sqrtPriceLimitX96)));
poolManager.unlock(abi.encode(key, IPoolManager.SwapParams(zeroForOne, type(int256).max, sqrtPriceLimitX96)));
}
}

Expand Down Expand Up @@ -298,7 +298,7 @@ contract TWAMM is BaseHook, ITWAMM {
IERC20Minimal(Currency.unwrap(token)).safeTransfer(to, amountTransferred);
}

function lockAcquired(bytes calldata rawData) external override poolManagerOnly returns (bytes memory) {
function unlockCallback(bytes calldata rawData) external override poolManagerOnly returns (bytes memory) {
(PoolKey memory key, IPoolManager.SwapParams memory swapParams) =
abi.decode(rawData, (PoolKey, IPoolManager.SwapParams));

Expand Down
2 changes: 1 addition & 1 deletion contracts/interfaces/IQuoter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {PathKey} from "../libraries/PathKey.sol";
/// @dev These functions are not marked view because they rely on calling non-view functions and reverting
/// to compute the result. They are also not gas efficient and should not be called on-chain.
interface IQuoter {
error InvalidLockAcquiredSender();
error InvalidUnlockCallbackSender();
error InvalidLockCaller();
error InvalidQuoteBatchParams();
error InsufficientAmountOut();
Expand Down
18 changes: 9 additions & 9 deletions contracts/lens/Quoter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pragma solidity ^0.8.20;
import {Hooks} from "@uniswap/v4-core/src/libraries/Hooks.sol";
import {TickMath} from "@uniswap/v4-core/src/libraries/TickMath.sol";
import {IHooks} from "@uniswap/v4-core/src/interfaces/IHooks.sol";
import {ILockCallback} from "@uniswap/v4-core/src/interfaces/callback/ILockCallback.sol";
import {IUnlockCallback} from "@uniswap/v4-core/src/interfaces/callback/IUnlockCallback.sol";
import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol";
import {BalanceDelta} from "@uniswap/v4-core/src/types/BalanceDelta.sol";
import {Currency} from "@uniswap/v4-core/src/types/Currency.sol";
Expand All @@ -14,7 +14,7 @@ import {IQuoter} from "../interfaces/IQuoter.sol";
import {PoolTicksCounter} from "../libraries/PoolTicksCounter.sol";
import {PathKey, PathKeyLib} from "../libraries/PathKey.sol";

contract Quoter is IQuoter, ILockCallback {
contract Quoter is IQuoter, IUnlockCallback {
using Hooks for IHooks;
using PoolIdLibrary for PoolKey;
using PathKeyLib for PathKey;
Expand Down Expand Up @@ -62,7 +62,7 @@ contract Quoter is IQuoter, ILockCallback {
override
returns (int128[] memory deltaAmounts, uint160 sqrtPriceX96After, uint32 initializedTicksLoaded)
{
try manager.lock(abi.encodeWithSelector(this._quoteExactInputSingle.selector, params)) {}
try manager.unlock(abi.encodeWithSelector(this._quoteExactInputSingle.selector, params)) {}
catch (bytes memory reason) {
return _handleRevertSingle(reason);
}
Expand All @@ -77,7 +77,7 @@ contract Quoter is IQuoter, ILockCallback {
uint32[] memory initializedTicksLoadedList
)
{
try manager.lock(abi.encodeWithSelector(this._quoteExactInput.selector, params)) {}
try manager.unlock(abi.encodeWithSelector(this._quoteExactInput.selector, params)) {}
catch (bytes memory reason) {
return _handleRevert(reason);
}
Expand All @@ -89,7 +89,7 @@ contract Quoter is IQuoter, ILockCallback {
override
returns (int128[] memory deltaAmounts, uint160 sqrtPriceX96After, uint32 initializedTicksLoaded)
{
try manager.lock(abi.encodeWithSelector(this._quoteExactOutputSingle.selector, params)) {}
try manager.unlock(abi.encodeWithSelector(this._quoteExactOutputSingle.selector, params)) {}
catch (bytes memory reason) {
if (params.sqrtPriceLimitX96 == 0) delete amountOutCached;
return _handleRevertSingle(reason);
Expand All @@ -106,16 +106,16 @@ contract Quoter is IQuoter, ILockCallback {
uint32[] memory initializedTicksLoadedList
)
{
try manager.lock(abi.encodeWithSelector(this._quoteExactOutput.selector, params)) {}
try manager.unlock(abi.encodeWithSelector(this._quoteExactOutput.selector, params)) {}
catch (bytes memory reason) {
return _handleRevert(reason);
}
}

/// @inheritdoc ILockCallback
function lockAcquired(bytes calldata data) external returns (bytes memory) {
/// @inheritdoc IUnlockCallback
function unlockCallback(bytes calldata data) external returns (bytes memory) {
if (msg.sender != address(manager)) {
revert InvalidLockAcquiredSender();
revert InvalidUnlockCallbackSender();
}

(bool success, bytes memory returnData) = address(this).call(data);
Expand Down
6 changes: 3 additions & 3 deletions test/Quoter.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,11 @@ contract QuoterTest is Test, Deployers {
assertEq(initializedTicksLoaded, 2);
}

// nested self-call into lockAcquired reverts
function testQuoter_callLockAcquired_reverts() public {
// nested self-call into unlockCallback reverts
function testQuoter_callUnlockCallback_reverts() public {
vm.expectRevert(IQuoter.LockFailure.selector);
vm.prank(address(manager));
quoter.lockAcquired(abi.encodeWithSelector(quoter.lockAcquired.selector, address(this), "0x"));
quoter.unlockCallback(abi.encodeWithSelector(quoter.unlockCallback.selector, address(this), "0x"));
}

function testQuoter_quoteExactInput_0to2_2TicksLoaded() public {
Expand Down
4 changes: 2 additions & 2 deletions test/utils/HookEnabledSwapRouter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ contract HookEnabledSwapRouter is PoolTestBase {
bytes memory hookData
) external payable returns (BalanceDelta delta) {
delta = abi.decode(
manager.lock(abi.encode(CallbackData(msg.sender, testSettings, key, params, hookData))), (BalanceDelta)
manager.unlock(abi.encode(CallbackData(msg.sender, testSettings, key, params, hookData))), (BalanceDelta)
);

uint256 ethBalance = address(this).balance;
if (ethBalance > 0) CurrencyLibrary.NATIVE.transfer(msg.sender, ethBalance);
}

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

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

0 comments on commit f278297

Please sign in to comment.