Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update v4-core:latest #105

Merged
merged 20 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeAddInitialLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
384735
312768
saucepoint marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeAddLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
179102
124239
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeFirstSwap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
128152
83956
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeInitialize.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1017530
1016591
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeRemoveLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
169304
111650
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeRemoveLiquidityAndRebalance.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
345919
245587
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeSecondSwap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
89081
48918
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeSwap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
126954
82712
2 changes: 1 addition & 1 deletion .forge-snapshots/TWAMMSubmitOrder.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
122845
122544
10 changes: 5 additions & 5 deletions 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 Expand Up @@ -95,7 +95,7 @@ abstract contract BaseHook is IHooks {
IPoolManager.ModifyLiquidityParams calldata,
BalanceDelta,
bytes calldata
) external virtual returns (bytes4) {
) external virtual returns (bytes4, BalanceDelta) {
revert HookNotImplemented();
}

Expand All @@ -105,22 +105,22 @@ abstract contract BaseHook is IHooks {
IPoolManager.ModifyLiquidityParams calldata,
BalanceDelta,
bytes calldata
) external virtual returns (bytes4) {
) external virtual returns (bytes4, BalanceDelta) {
revert HookNotImplemented();
}

function beforeSwap(address, PoolKey calldata, IPoolManager.SwapParams calldata, bytes calldata)
external
virtual
returns (bytes4)
returns (bytes4, int128)
{
revert HookNotImplemented();
}

function afterSwap(address, PoolKey calldata, IPoolManager.SwapParams calldata, BalanceDelta, bytes calldata)
external
virtual
returns (bytes4)
returns (bytes4, int128)
{
revert HookNotImplemented();
}
Expand Down
59 changes: 28 additions & 31 deletions contracts/hooks/examples/FullRange.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import {BaseHook} from "../../BaseHook.sol";
import {SafeCast} from "@uniswap/v4-core/src/libraries/SafeCast.sol";
import {IHooks} from "@uniswap/v4-core/src/interfaces/IHooks.sol";
import {CurrencyLibrary, Currency} from "@uniswap/v4-core/src/types/Currency.sol";
import {CurrencySettleTake} from "@uniswap/v4-core/src/libraries/CurrencySettleTake.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,8 +24,9 @@ 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 CurrencySettleTake for Currency;
using PoolIdLibrary for PoolKey;
using SafeCast for uint256;
using SafeCast for uint128;
Expand Down Expand Up @@ -98,7 +100,11 @@ contract FullRange is BaseHook, ILockCallback {
beforeSwap: true,
afterSwap: false,
beforeDonate: false,
afterDonate: false
afterDonate: false,
beforeSwapReturnDelta: false,
afterSwapReturnDelta: false,
afterAddLiquidityReturnDelta: false,
afterRemoveLiquidityReturnDelta: false
});
}

Expand Down Expand Up @@ -141,7 +147,8 @@ contract FullRange is BaseHook, ILockCallback {
IPoolManager.ModifyLiquidityParams({
tickLower: MIN_TICK,
tickUpper: MAX_TICK,
liquidityDelta: liquidity.toInt256()
liquidityDelta: liquidity.toInt256(),
salt: 0
})
);

Expand Down Expand Up @@ -185,7 +192,8 @@ contract FullRange is BaseHook, ILockCallback {
IPoolManager.ModifyLiquidityParams({
tickLower: MIN_TICK,
tickUpper: MAX_TICK,
liquidityDelta: -(params.liquidity.toInt256())
liquidityDelta: -(params.liquidity.toInt256()),
salt: 0
})
);

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

Expand All @@ -242,32 +250,19 @@ contract FullRange is BaseHook, ILockCallback {
pool.hasAccruedFees = true;
}

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

function modifyLiquidity(PoolKey memory key, IPoolManager.ModifyLiquidityParams memory params)
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 {
_settleDelta(sender, key.currency0, uint128(-delta.amount0()));
_settleDelta(sender, key.currency1, uint128(-delta.amount1()));
}

function _settleDelta(address sender, Currency currency, uint128 amount) internal {
if (currency.isNative()) {
poolManager.settle{value: amount}(currency);
} else {
if (sender == address(this)) {
currency.transfer(address(poolManager), amount);
} else {
IERC20Minimal(Currency.unwrap(currency)).transferFrom(sender, address(poolManager), amount);
}
poolManager.settle(currency);
}
key.currency0.settle(poolManager, sender, uint256(int256(-delta.amount0())), false);
key.currency1.settle(poolManager, sender, uint256(int256(-delta.amount1())), false);
}

function _takeDeltas(address sender, PoolKey memory key, BalanceDelta delta) internal {
Expand All @@ -293,13 +288,13 @@ contract FullRange is BaseHook, ILockCallback {
);

params.liquidityDelta = -(liquidityToRemove.toInt256());
delta = poolManager.modifyLiquidity(key, params, ZERO_BYTES);
(delta,) = poolManager.modifyLiquidity(key, params, ZERO_BYTES);
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 All @@ -310,20 +305,21 @@ contract FullRange is BaseHook, ILockCallback {
delta = _removeLiquidity(data.key, data.params);
_takeDeltas(data.sender, data.key, delta);
} else {
delta = poolManager.modifyLiquidity(data.key, data.params, ZERO_BYTES);
(delta,) = poolManager.modifyLiquidity(data.key, data.params, ZERO_BYTES);
_settleDeltas(data.sender, data.key, delta);
}
return abi.encode(delta);
}

function _rebalance(PoolKey memory key) public {
PoolId poolId = key.toId();
BalanceDelta balanceDelta = poolManager.modifyLiquidity(
(BalanceDelta balanceDelta,) = poolManager.modifyLiquidity(
key,
IPoolManager.ModifyLiquidityParams({
tickLower: MIN_TICK,
tickUpper: MAX_TICK,
liquidityDelta: -(poolManager.getLiquidity(poolId).toInt256())
liquidityDelta: -(poolManager.getLiquidity(poolId).toInt256()),
salt: 0
}),
ZERO_BYTES
);
Expand Down Expand Up @@ -354,12 +350,13 @@ contract FullRange is BaseHook, ILockCallback {
uint256(uint128(balanceDelta.amount1()))
);

BalanceDelta balanceDeltaAfter = poolManager.modifyLiquidity(
(BalanceDelta balanceDeltaAfter,) = poolManager.modifyLiquidity(
key,
IPoolManager.ModifyLiquidityParams({
tickLower: MIN_TICK,
tickUpper: MAX_TICK,
liquidityDelta: liquidity.toInt256()
liquidityDelta: liquidity.toInt256(),
salt: 0
}),
ZERO_BYTES
);
Expand Down
10 changes: 7 additions & 3 deletions contracts/hooks/examples/GeomeanOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ contract GeomeanOracle is BaseHook {
beforeSwap: true,
afterSwap: false,
beforeDonate: false,
afterDonate: false
afterDonate: false,
beforeSwapReturnDelta: false,
afterSwapReturnDelta: false,
afterAddLiquidityReturnDelta: false,
afterRemoveLiquidityReturnDelta: false
});
}

Expand Down Expand Up @@ -140,10 +144,10 @@ contract GeomeanOracle is BaseHook {
external
override
poolManagerOnly
returns (bytes4)
returns (bytes4, int128)
{
_updatePool(key);
return GeomeanOracle.beforeSwap.selector;
return (GeomeanOracle.beforeSwap.selector, 0);
}

/// @notice Observe the given pool for the timestamps
Expand Down
Loading
Loading