Skip to content

Commit

Permalink
flip delta signs
Browse files Browse the repository at this point in the history
  • Loading branch information
saucepoint committed Mar 14, 2024
1 parent f6717d5 commit bc1685a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
28 changes: 14 additions & 14 deletions contracts/hooks/examples/FullRange.sol
Original file line number Diff line number Diff line change
Expand Up @@ -117,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 @@ -153,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 @@ -174,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 @@ -253,8 +253,8 @@ contract FullRange is BaseHook, ILockCallback {
}

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 @@ -271,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 Down Expand Up @@ -330,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,
sqrtPriceLimitX96: newSqrtPriceX96
}),
ZERO_BYTES
Expand All @@ -350,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 @@ -365,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
6 changes: 3 additions & 3 deletions test/FullRange.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ contract TestFullRange is Test, Deployers, GasSnapshot {

function testFullRange_addLiquidity_InitialAddFuzz(uint256 amount) public {
manager.initialize(key, SQRT_RATIO_1_1, ZERO_BYTES);
if (amount < LOCKED_LIQUIDITY) {
if (amount <= LOCKED_LIQUIDITY) {
vm.expectRevert(FullRange.LiquidityDoesntMeetMinimum.selector);
fullRange.addLiquidity(
FullRange.AddLiquidityParams(
Expand Down Expand Up @@ -265,11 +265,11 @@ contract TestFullRange is Test, Deployers, GasSnapshot {

vm.expectEmit(true, true, true, true);
emit Swap(
id, address(router), 1 ether, -906610893880149131, 72045250990510446115798809072, 10 ether, -1901, 3000
id, address(router), -1 ether, 906610893880149131, 72045250990510446115798809072, 10 ether, -1901, 3000
);

IPoolManager.SwapParams memory params =
IPoolManager.SwapParams({zeroForOne: true, amountSpecified: 1 ether, sqrtPriceLimitX96: SQRT_RATIO_1_2});
IPoolManager.SwapParams({zeroForOne: true, amountSpecified: -1 ether, sqrtPriceLimitX96: SQRT_RATIO_1_2});
HookEnabledSwapRouter.TestSettings memory settings =
HookEnabledSwapRouter.TestSettings({withdrawTokens: true, settleUsingTransfer: true});

Expand Down

0 comments on commit bc1685a

Please sign in to comment.