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

gas optimizations for the FullRange hook #85

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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 @@
412696
411324
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeAddLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
206962
205590
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeFirstSwap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
154763
150901
1 change: 1 addition & 0 deletions .forge-snapshots/FullRangeLargeSwap.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
149976
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeRemoveLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
200095
200462
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeRemoveLiquidityAndRebalance.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
379287
374704
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeSecondSwap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
112303
112734
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeSwap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
153038
149176
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ If you’re interested in contributing please see the [contribution guidelines](
contracts/
----hooks/
----examples/
| FullRange.sol
| GeomeanOracle.sol
| LimitOrder.sol
| TWAMM.sol
Expand Down
41 changes: 21 additions & 20 deletions contracts/hooks/examples/FullRange.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,19 @@ contract FullRange is BaseHook, ILockCallback {

bytes internal constant ZERO_BYTES = bytes("");

/// @dev Min tick for full range with tick spacing of 60
int24 internal constant MIN_TICK = -887220;
/// @dev Max tick for full range with tick spacing of 60
/// @dev Set tick spacing to a large number that's <= type(int16).max
int24 internal constant TICK_SPACING = 0x7000;

/// @dev Min tick for full range with tick spacing of TICK_SPACING
int24 internal constant MIN_TICK = (TickMath.MIN_TICK / TICK_SPACING + 1) * TICK_SPACING;
/// @dev Max tick for full range with tick spacing of TICK_SPACING
int24 internal constant MAX_TICK = -MIN_TICK;

/// @dev TickMath.getSqrtRatioAtTick(MIN_TICK), cached for optimization
uint160 internal immutable MIN_SQRT_RATIO = TickMath.getSqrtRatioAtTick(MIN_TICK);
/// @dev TickMath.getSqrtRatioAtTick(MIN_TICK), cached for optimization
uint160 internal immutable MAX_SQRT_RATIO = TickMath.getSqrtRatioAtTick(MAX_TICK);

int256 internal constant MAX_INT = type(int256).max;
uint16 internal constant MINIMUM_LIQUIDITY = 1000;

Expand Down Expand Up @@ -109,7 +117,7 @@ contract FullRange is BaseHook, ILockCallback {
currency0: params.currency0,
currency1: params.currency1,
fee: params.fee,
tickSpacing: 60,
tickSpacing: TICK_SPACING,
hooks: IHooks(address(this))
});

Expand All @@ -124,11 +132,7 @@ contract FullRange is BaseHook, ILockCallback {
uint128 poolLiquidity = poolManager.getLiquidity(poolId);

liquidity = LiquidityAmounts.getLiquidityForAmounts(
sqrtPriceX96,
TickMath.getSqrtRatioAtTick(MIN_TICK),
TickMath.getSqrtRatioAtTick(MAX_TICK),
params.amount0Desired,
params.amount1Desired
sqrtPriceX96, MIN_SQRT_RATIO, MAX_SQRT_RATIO, params.amount0Desired, params.amount1Desired
);

if (poolLiquidity == 0 && liquidity <= MINIMUM_LIQUIDITY) {
Expand Down Expand Up @@ -166,7 +170,7 @@ contract FullRange is BaseHook, ILockCallback {
currency0: params.currency0,
currency1: params.currency1,
fee: params.fee,
tickSpacing: 60,
tickSpacing: TICK_SPACING,
hooks: IHooks(address(this))
});

Expand Down Expand Up @@ -195,7 +199,7 @@ contract FullRange is BaseHook, ILockCallback {
override
returns (bytes4)
{
if (key.tickSpacing != 60) revert TickSpacingNotDefault();
if (key.tickSpacing != TICK_SPACING) revert TickSpacingNotDefault();

PoolId poolId = key.toId();

Expand Down Expand Up @@ -234,11 +238,7 @@ contract FullRange is BaseHook, ILockCallback {
returns (bytes4)
{
PoolId poolId = key.toId();

if (!poolInfo[poolId].hasAccruedFees) {
PoolInfo storage pool = poolInfo[poolId];
pool.hasAccruedFees = true;
}
poolInfo[poolId].hasAccruedFees = true;

return IHooks.beforeSwap.selector;
}
Expand Down Expand Up @@ -282,6 +282,7 @@ contract FullRange is BaseHook, ILockCallback {

if (pool.hasAccruedFees) {
_rebalance(key);
pool.hasAccruedFees = false;
}

uint256 liquidityToRemove = FullMath.mulDiv(
Expand All @@ -292,7 +293,6 @@ contract FullRange is BaseHook, ILockCallback {

params.liquidityDelta = -(liquidityToRemove.toInt256());
delta = poolManager.modifyPosition(key, params, ZERO_BYTES);
pool.hasAccruedFees = false;
}

function lockAcquired(bytes calldata rawData)
Expand Down Expand Up @@ -326,10 +326,11 @@ contract FullRange is BaseHook, ILockCallback {
ZERO_BYTES
);

// The final shift by 48 is equal to multiplying by sqrt(Q96) using unchecked math
uint160 newSqrtPriceX96 = (
FixedPointMathLib.sqrt(
FullMath.mulDiv(uint128(-balanceDelta.amount1()), FixedPoint96.Q96, uint128(-balanceDelta.amount0()))
) * FixedPointMathLib.sqrt(FixedPoint96.Q96)
) << 48
).toUint160();

(uint160 sqrtPriceX96,,,) = poolManager.getSlot0(poolId);
Expand All @@ -346,8 +347,8 @@ contract FullRange is BaseHook, ILockCallback {

uint128 liquidity = LiquidityAmounts.getLiquidityForAmounts(
newSqrtPriceX96,
TickMath.getSqrtRatioAtTick(MIN_TICK),
TickMath.getSqrtRatioAtTick(MAX_TICK),
MIN_SQRT_RATIO,
MAX_SQRT_RATIO,
uint256(uint128(-balanceDelta.amount0())),
uint256(uint128(-balanceDelta.amount1()))
);
Expand Down
Loading