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 #100

Merged
merged 19 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from 14 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 @@
392772
384735
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeAddLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
187139
179102
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeFirstSwap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
136542
128152
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeInitialize.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1041060
1017530
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeRemoveLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
175903
169304
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeRemoveLiquidityAndRebalance.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
363995
345804
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeSecondSwap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
97295
89081
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeSwap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
134817
126954
2 changes: 1 addition & 1 deletion .forge-snapshots/TWAMMSubmitOrder.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
122753
122845
7 changes: 1 addition & 6 deletions contracts/BaseHook.sol
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,7 @@ abstract contract BaseHook is IHooks {
Hooks.validateHookPermissions(_this, getHookPermissions());
}

function lockAcquired(address, /*sender*/ bytes calldata data)
external
virtual
poolManagerOnly
returns (bytes memory)
{
function lockAcquired(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
40 changes: 17 additions & 23 deletions contracts/hooks/examples/FullRange.sol
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ contract FullRange is BaseHook, ILockCallback {
beforeSwap: true,
afterSwap: false,
beforeDonate: false,
afterDonate: false,
noOp: false,
accessLock: false
afterDonate: false
});
}

Expand All @@ -119,7 +117,7 @@ contract FullRange is BaseHook, ILockCallback {

PoolId poolId = key.toId();

(uint160 sqrtPriceX96,,) = poolManager.getSlot0(poolId);
(uint160 sqrtPriceX96,,,) = poolManager.getSlot0(poolId);

if (sqrtPriceX96 == 0) revert PoolNotInitialized();

Expand Down Expand Up @@ -155,7 +153,7 @@ contract FullRange is BaseHook, ILockCallback {

UniswapV4ERC20(pool.liquidityToken).mint(params.to, liquidity);

if (uint128(addedDelta.amount0()) < params.amount0Min || uint128(addedDelta.amount1()) < params.amount1Min) {
if (uint128(-addedDelta.amount0()) < params.amount0Min || uint128(-addedDelta.amount1()) < params.amount1Min) {
revert TooMuchSlippage();
}
}
Expand All @@ -176,7 +174,7 @@ contract FullRange is BaseHook, ILockCallback {

PoolId poolId = key.toId();

(uint160 sqrtPriceX96,,) = poolManager.getSlot0(poolId);
(uint160 sqrtPriceX96,,,) = poolManager.getSlot0(poolId);

if (sqrtPriceX96 == 0) revert PoolNotInitialized();

Expand Down Expand Up @@ -251,14 +249,12 @@ contract FullRange is BaseHook, ILockCallback {
internal
returns (BalanceDelta delta)
{
delta = abi.decode(
poolManager.lock(address(this), abi.encode(CallbackData(msg.sender, key, params))), (BalanceDelta)
);
delta = abi.decode(poolManager.lock(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()));
_settleDelta(sender, key.currency0, uint128(-delta.amount0()));
_settleDelta(sender, key.currency1, uint128(-delta.amount1()));
}

function _settleDelta(address sender, Currency currency, uint128 amount) internal {
Expand All @@ -275,8 +271,8 @@ contract FullRange is BaseHook, ILockCallback {
}

function _takeDeltas(address sender, PoolKey memory key, BalanceDelta delta) internal {
poolManager.take(key.currency0, sender, uint256(uint128(-delta.amount0())));
poolManager.take(key.currency1, sender, uint256(uint128(-delta.amount1())));
poolManager.take(key.currency0, sender, uint256(uint128(delta.amount0())));
poolManager.take(key.currency1, sender, uint256(uint128(delta.amount1())));
}

function _removeLiquidity(PoolKey memory key, IPoolManager.ModifyLiquidityParams memory params)
Expand All @@ -301,14 +297,12 @@ contract FullRange is BaseHook, ILockCallback {
pool.hasAccruedFees = false;
}

function lockAcquired(address sender, bytes calldata rawData)
function lockAcquired(bytes calldata rawData)
external
override(ILockCallback, BaseHook)
poolManagerOnly
returns (bytes memory)
{
// Now that manager can be called by EOAs with a lock target, it's necessary for lockAcquired to check the original sender if it wants to trust the data passed through.
if (sender != address(this)) revert SenderMustBeHook();
CallbackData memory data = abi.decode(rawData, (CallbackData));
BalanceDelta delta;

Expand Down Expand Up @@ -336,17 +330,17 @@ contract FullRange is BaseHook, ILockCallback {

uint160 newSqrtPriceX96 = (
FixedPointMathLib.sqrt(
FullMath.mulDiv(uint128(-balanceDelta.amount1()), FixedPoint96.Q96, uint128(-balanceDelta.amount0()))
FullMath.mulDiv(uint128(balanceDelta.amount1()), FixedPoint96.Q96, uint128(balanceDelta.amount0()))
) * FixedPointMathLib.sqrt(FixedPoint96.Q96)
).toUint160();

(uint160 sqrtPriceX96,,) = poolManager.getSlot0(poolId);
(uint160 sqrtPriceX96,,,) = poolManager.getSlot0(poolId);

poolManager.swap(
key,
IPoolManager.SwapParams({
zeroForOne: newSqrtPriceX96 < sqrtPriceX96,
amountSpecified: MAX_INT,
amountSpecified: -MAX_INT,
saucepoint marked this conversation as resolved.
Show resolved Hide resolved
sqrtPriceLimitX96: newSqrtPriceX96
}),
ZERO_BYTES
Expand All @@ -356,8 +350,8 @@ contract FullRange is BaseHook, ILockCallback {
newSqrtPriceX96,
TickMath.getSqrtRatioAtTick(MIN_TICK),
TickMath.getSqrtRatioAtTick(MAX_TICK),
uint256(uint128(-balanceDelta.amount0())),
uint256(uint128(-balanceDelta.amount1()))
uint256(uint128(balanceDelta.amount0())),
uint256(uint128(balanceDelta.amount1()))
);

BalanceDelta balanceDeltaAfter = poolManager.modifyLiquidity(
Expand All @@ -371,8 +365,8 @@ contract FullRange is BaseHook, ILockCallback {
);

// Donate any "dust" from the sqrtRatio change as fees
uint128 donateAmount0 = uint128(-balanceDelta.amount0() - balanceDeltaAfter.amount0());
uint128 donateAmount1 = uint128(-balanceDelta.amount1() - balanceDeltaAfter.amount1());
uint128 donateAmount0 = uint128(balanceDelta.amount0() + balanceDeltaAfter.amount0());
uint128 donateAmount1 = uint128(balanceDelta.amount1() + balanceDeltaAfter.amount1());

poolManager.donate(key, donateAmount0, donateAmount1, ZERO_BYTES);
}
Expand Down
8 changes: 3 additions & 5 deletions contracts/hooks/examples/GeomeanOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ contract GeomeanOracle is BaseHook {
beforeSwap: true,
afterSwap: false,
beforeDonate: false,
afterDonate: false,
noOp: false,
accessLock: false
afterDonate: false
});
}

Expand Down Expand Up @@ -105,7 +103,7 @@ contract GeomeanOracle is BaseHook {
/// @dev Called before any action that potentially modifies pool price or liquidity, such as swap or modify position
function _updatePool(PoolKey calldata key) private {
PoolId id = key.toId();
(, int24 tick,) = poolManager.getSlot0(id);
(, int24 tick,,) = poolManager.getSlot0(id);

uint128 liquidity = poolManager.getLiquidity(id);

Expand Down Expand Up @@ -158,7 +156,7 @@ contract GeomeanOracle is BaseHook {

ObservationState memory state = states[id];

(, int24 tick,) = poolManager.getSlot0(id);
(, int24 tick,,) = poolManager.getSlot0(id);

uint128 liquidity = poolManager.getLiquidity(id);

Expand Down
56 changes: 23 additions & 33 deletions contracts/hooks/examples/LimitOrder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ contract LimitOrder is BaseHook {
beforeSwap: false,
afterSwap: true,
beforeDonate: false,
afterDonate: false,
noOp: false,
accessLock: false
afterDonate: false
});
}

Expand All @@ -111,7 +109,7 @@ contract LimitOrder is BaseHook {
}

function getTick(PoolId poolId) private view returns (int24 tick) {
(, tick,) = poolManager.getSlot0(poolId);
(, tick,,) = poolManager.getSlot0(poolId);
}

function getTickLower(int24 tick, int24 tickSpacing) private pure returns (int24) {
Expand All @@ -131,7 +129,7 @@ contract LimitOrder is BaseHook {
}

function afterSwap(
address,
address sender,
saucepoint marked this conversation as resolved.
Show resolved Hide resolved
PoolKey calldata key,
IPoolManager.SwapParams calldata params,
BalanceDelta,
Expand All @@ -158,13 +156,8 @@ contract LimitOrder is BaseHook {

epochInfo.filled = true;

(uint256 amount0, uint256 amount1) = abi.decode(
poolManager.lock(
address(this),
abi.encodeCall(this.lockAcquiredFill, (key, lower, -int256(uint256(epochInfo.liquidityTotal))))
),
(uint256, uint256)
);
(uint256 amount0, uint256 amount1) =
_lockAcquiredFill(key, lower, -int256(uint256(epochInfo.liquidityTotal)));

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

function lockAcquiredFill(PoolKey calldata key, int24 tickLower, int256 liquidityDelta)
external
selfOnly
function _lockAcquiredFill(PoolKey calldata key, int24 tickLower, int256 liquidityDelta)
private
poolManagerOnly
returns (uint128 amount0, uint128 amount1)
{
BalanceDelta delta = poolManager.modifyLiquidity(
Expand All @@ -209,11 +202,11 @@ contract LimitOrder is BaseHook {
ZERO_BYTES
);

if (delta.amount0() < 0) {
poolManager.mint(address(this), key.currency0.toId(), amount0 = uint128(-delta.amount0()));
if (delta.amount0() > 0) {
poolManager.mint(address(this), key.currency0.toId(), amount0 = uint128(delta.amount0()));
}
if (delta.amount1() < 0) {
poolManager.mint(address(this), key.currency1.toId(), amount1 = uint128(-delta.amount1()));
if (delta.amount1() > 0) {
poolManager.mint(address(this), key.currency1.toId(), amount1 = uint128(delta.amount1()));
}
}

Expand All @@ -224,7 +217,6 @@ contract LimitOrder is BaseHook {
if (liquidity == 0) revert ZeroLiquidity();

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

Expand Down Expand Up @@ -270,20 +262,20 @@ contract LimitOrder is BaseHook {
ZERO_BYTES
);

if (delta.amount0() > 0) {
if (delta.amount0() < 0) {
if (delta.amount1() != 0) revert InRange();
if (!zeroForOne) revert CrossedRange();
// TODO use safeTransferFrom
IERC20Minimal(Currency.unwrap(key.currency0)).transferFrom(
owner, address(poolManager), uint256(uint128(delta.amount0()))
owner, address(poolManager), uint256(uint128(-delta.amount0()))
);
poolManager.settle(key.currency0);
} else {
if (delta.amount0() != 0) revert InRange();
if (zeroForOne) revert CrossedRange();
// TODO use safeTransferFrom
IERC20Minimal(Currency.unwrap(key.currency1)).transferFrom(
owner, address(poolManager), uint256(uint128(delta.amount1()))
owner, address(poolManager), uint256(uint128(-delta.amount1()))
);
poolManager.settle(key.currency1);
}
Expand All @@ -306,7 +298,6 @@ contract LimitOrder is BaseHook {
uint256 amount1Fee;
(amount0, amount1, amount0Fee, amount1Fee) = abi.decode(
poolManager.lock(
address(this),
abi.encodeCall(
this.lockAcquiredKill,
(key, tickLower, -int256(uint256(liquidity)), to, liquidity == epochInfo.liquidityTotal)
Expand Down Expand Up @@ -343,11 +334,11 @@ contract LimitOrder is BaseHook {
ZERO_BYTES
);

if (deltaFee.amount0() < 0) {
poolManager.mint(address(this), key.currency0.toId(), amount0Fee = uint128(-deltaFee.amount0()));
if (deltaFee.amount0() > 0) {
poolManager.mint(address(this), key.currency0.toId(), amount0Fee = uint128(deltaFee.amount0()));
}
if (deltaFee.amount1() < 0) {
poolManager.mint(address(this), key.currency1.toId(), amount1Fee = uint128(-deltaFee.amount1()));
if (deltaFee.amount1() > 0) {
poolManager.mint(address(this), key.currency1.toId(), amount1Fee = uint128(deltaFee.amount1()));
}
}

Expand All @@ -361,11 +352,11 @@ contract LimitOrder is BaseHook {
ZERO_BYTES
);

if (delta.amount0() < 0) {
poolManager.take(key.currency0, to, amount0 = uint128(-delta.amount0()));
if (delta.amount0() > 0) {
poolManager.take(key.currency0, to, amount0 = uint128(delta.amount0()));
}
if (delta.amount1() < 0) {
poolManager.take(key.currency1, to, amount1 = uint128(-delta.amount1()));
if (delta.amount1() > 0) {
poolManager.take(key.currency1, to, amount1 = uint128(delta.amount1()));
}
}

Expand All @@ -388,7 +379,6 @@ contract LimitOrder is BaseHook {
epochInfo.liquidityTotal = liquidityTotal - liquidity;

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

Expand Down
Loading
Loading