Skip to content

Commit

Permalink
update fullRange and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jun1on committed Jun 26, 2024
1 parent 2d3586c commit fe5c0e0
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeAddInitialLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
311181
311621
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeAddLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
122990
125693
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeInitialize.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1015181
1015209
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeRemoveLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
110566
110668
2 changes: 1 addition & 1 deletion .forge-snapshots/FullRangeRemoveLiquidityAndRebalance.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
240044
240370
23 changes: 14 additions & 9 deletions contracts/hooks/examples/FullRange.sol
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ contract FullRange is BaseHook {
if (poolLiquidity == 0 && liquidity <= MINIMUM_LIQUIDITY) {
revert LiquidityDoesntMeetMinimum();
}

BalanceDelta addedDelta = modifyLiquidity(
key,
IPoolManager.ModifyLiquidityParams({
Expand All @@ -157,12 +158,14 @@ contract FullRange is BaseHook {

if (poolLiquidity == 0) {
// permanently lock the first MINIMUM_LIQUIDITY tokens
liquidity -= MINIMUM_LIQUIDITY;
UniswapV4ERC20(pool.liquidityToken).mint(address(0), MINIMUM_LIQUIDITY);
UniswapV4ERC20(pool.liquidityToken).mint(params.to, liquidity - MINIMUM_LIQUIDITY);
} else {
uint256 liquidityMinted = uint256(liquidity) * UniswapV4ERC20(pool.liquidityToken).totalSupply()
/ uint256(manager.getLiquidity(poolId) - liquidity);
UniswapV4ERC20(pool.liquidityToken).mint(params.to, liquidityMinted);
}

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

if (uint128(-addedDelta.amount0()) < params.amount0Min || uint128(-addedDelta.amount1()) < params.amount1Min) {
revert TooMuchSlippage();
}
Expand Down Expand Up @@ -281,10 +284,6 @@ contract FullRange is BaseHook {
PoolId poolId = key.toId();
PoolInfo storage pool = poolInfo[poolId];

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

uint256 liquidityToRemove = FullMath.mulDiv(
uint256(-params.liquidityDelta),
manager.getLiquidity(poolId),
Expand All @@ -293,13 +292,19 @@ contract FullRange is BaseHook {

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

function _unlockCallback(bytes calldata rawData) internal override returns (bytes memory) {
CallbackData memory data = abi.decode(rawData, (CallbackData));
BalanceDelta delta;

PoolId poolId = data.key.toId();
PoolInfo storage pool = poolInfo[data.key.toId()];
if (pool.hasAccruedFees) {
pool.hasAccruedFees = false;
rebalance(data.key);
}

if (data.params.liquidityDelta < 0) {
delta = _removeLiquidity(data.key, data.params);
_takeDeltas(data.sender, data.key, delta);
Expand All @@ -310,7 +315,7 @@ contract FullRange is BaseHook {
return abi.encode(delta);
}

function _rebalance(PoolKey memory key) public {
function rebalance(PoolKey memory key) public {
PoolId poolId = key.toId();
(BalanceDelta balanceDelta,) = manager.modifyLiquidity(
key,
Expand Down
5 changes: 2 additions & 3 deletions test/FullRange.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,8 @@ contract TestFullRange is Test, Deployers, GasSnapshot {
(hasAccruedFees,) = fullRange.poolInfo(id);
liquidityTokenBal = UniswapV4ERC20(liquidityToken).balanceOf(address(this));

assertEq(manager.getLiquidity(id), liquidityTokenBal + LOCKED_LIQUIDITY);
assertEq(liquidityTokenBal, 14546694553059925434 - LOCKED_LIQUIDITY);
assertEq(hasAccruedFees, true);
assertTrue(manager.getLiquidity(id) > liquidityTokenBal + LOCKED_LIQUIDITY);
assertEq(hasAccruedFees, false);
}

function testFullRange_addLiquidity_FailsIfTooMuchSlippage() public {
Expand Down

0 comments on commit fe5c0e0

Please sign in to comment.