Skip to content

Commit

Permalink
internal-s30 use notifyBurn (#60)
Browse files Browse the repository at this point in the history
* internal-s30 use notifyBurn

* clean up console2
  • Loading branch information
ChefMist authored Dec 3, 2024
1 parent 05b4574 commit 297be6f
Show file tree
Hide file tree
Showing 55 changed files with 161 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
794890
794997
Original file line number Diff line number Diff line change
@@ -1 +1 @@
660315
660422
Original file line number Diff line number Diff line change
@@ -1 +1 @@
730597
730704
Original file line number Diff line number Diff line change
@@ -1 +1 @@
844797
844904
Original file line number Diff line number Diff line change
@@ -1 +1 @@
712751
712858
Original file line number Diff line number Diff line change
@@ -1 +1 @@
753592
753699
Original file line number Diff line number Diff line change
@@ -1 +1 @@
794890
794997
Original file line number Diff line number Diff line change
@@ -1 +1 @@
660315
660422
Original file line number Diff line number Diff line change
@@ -1 +1 @@
730597
730704
Original file line number Diff line number Diff line change
@@ -1 +1 @@
842779
842886
Original file line number Diff line number Diff line change
@@ -1 +1 @@
710733
710840
Original file line number Diff line number Diff line change
@@ -1 +1 @@
751574
751681
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPositionManager_burn_empty.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
60328
60603
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPositionManager_burn_empty_native.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
60328
60603
Original file line number Diff line number Diff line change
@@ -1 +1 @@
173976
174388
Original file line number Diff line number Diff line change
@@ -1 +1 @@
173294
173706
Original file line number Diff line number Diff line change
@@ -1 +1 @@
182555
182967
Original file line number Diff line number Diff line change
@@ -1 +1 @@
181873
182285
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPositionManager_collect_native.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
205506
205613
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPositionManager_collect_sameRange.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
214085
214192
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPositionManager_collect_withClose.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
214085
214192
Original file line number Diff line number Diff line change
@@ -1 +1 @@
213415
213522
Original file line number Diff line number Diff line change
@@ -1 +1 @@
170799
170906
Original file line number Diff line number Diff line change
@@ -1 +1 @@
179378
179485
Original file line number Diff line number Diff line change
@@ -1 +1 @@
178708
178815
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPositionManager_decrease_burnEmpty.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
186260
186642
Original file line number Diff line number Diff line change
@@ -1 +1 @@
177681
178063
Original file line number Diff line number Diff line change
@@ -1 +1 @@
192003
192110
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPositionManager_decrease_take_take.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
180045
180152
Original file line number Diff line number Diff line change
@@ -1 +1 @@
220737
220844
Original file line number Diff line number Diff line change
@@ -1 +1 @@
219660
219767
Original file line number Diff line number Diff line change
@@ -1 +1 @@
204085
204192
Original file line number Diff line number Diff line change
@@ -1 +1 @@
165603
165710
Original file line number Diff line number Diff line change
@@ -1 +1 @@
239098
239205
Original file line number Diff line number Diff line change
@@ -1 +1 @@
209968
210075
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPositionManager_mint_native.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
540268
540375
Original file line number Diff line number Diff line change
@@ -1 +1 @@
548858
548965
Original file line number Diff line number Diff line change
@@ -1 +1 @@
548065
548172
Original file line number Diff line number Diff line change
@@ -1 +1 @@
393704
393811
Original file line number Diff line number Diff line change
@@ -1 +1 @@
394141
394248
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPositionManager_mint_sameRange.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
302929
303036
Original file line number Diff line number Diff line change
@@ -1 +1 @@
593126
593233
Original file line number Diff line number Diff line change
@@ -1 +1 @@
399416
399523
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPositionManager_mint_withClose.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
594160
594267
Original file line number Diff line number Diff line change
@@ -1 +1 @@
593225
593332
Original file line number Diff line number Diff line change
@@ -1 +1 @@
652143
652250
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPositionManager_subscribe.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
87830
87852
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPositionManager_unsubscribe.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
62678
62765
21 changes: 18 additions & 3 deletions src/pool-cl/CLPositionManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -299,20 +299,34 @@ contract CLPositionManager is

uint256 liquidity = uint256(_getLiquidity(tokenId, poolKey, info.tickLower(), info.tickUpper()));

address owner = ownerOf(tokenId);

// Clear the position info.
positionInfo[tokenId] = CLPositionInfoLibrary.EMPTY_POSITION_INFO;
// Burn the token.
_burn(tokenId);

// Can only call modify if there is non zero liquidity.
BalanceDelta feesAccrued;
BalanceDelta liquidityDelta;
if (liquidity > 0) {
(BalanceDelta liquidityDelta, BalanceDelta feesAccrued) =
_modifyLiquidity(info, poolKey, -(liquidity.toInt256()), bytes32(tokenId), hookData);
(liquidityDelta, feesAccrued) = clPoolManager.modifyLiquidity(
poolKey,
ICLPoolManager.ModifyLiquidityParams({
tickLower: info.tickLower(),
tickUpper: info.tickUpper(),
liquidityDelta: -(liquidity.toInt256()),
salt: bytes32(tokenId)
}),
hookData
);
// Slippage checks should be done on the principal liquidityDelta which is the liquidityDelta - feesAccrued
(liquidityDelta - feesAccrued).validateMinOut(amount0Min, amount1Min);

emit ModifyLiquidity(tokenId, -(liquidity.toInt256()), feesAccrued);
}

if (info.hasSubscriber()) _unsubscribe(tokenId);
if (info.hasSubscriber()) _removeSubscriberAndNotifyBurn(tokenId, owner, info, liquidity, feesAccrued);
}

function _settlePair(Currency currency0, Currency currency1) internal {
Expand Down Expand Up @@ -360,6 +374,7 @@ contract CLPositionManager is
if (balance > 0) currency.transfer(to, balance);
}

/// @dev if there is a subscriber attached to the position, this function will notify the subscriber
function _modifyLiquidity(
CLPositionInfo info,
PoolKey memory poolKey,
Expand Down
25 changes: 25 additions & 0 deletions src/pool-cl/base/CLNotifier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,31 @@ abstract contract CLNotifier is ICLNotifier {
emit Unsubscription(tokenId, address(_subscriber));
}

/// @dev note this function also deletes the subscriber address from the mapping
function _removeSubscriberAndNotifyBurn(
uint256 tokenId,
address owner,
CLPositionInfo info,
uint256 liquidity,
BalanceDelta feesAccrued
) internal {
ICLSubscriber _subscriber = subscriber[tokenId];

// remove the subscriber
delete subscriber[tokenId];

bool success = _call(
address(_subscriber),
abi.encodeCall(ICLSubscriber.notifyBurn, (tokenId, owner, info, liquidity, feesAccrued))
);

if (!success) {
address(_subscriber).bubbleUpAndRevertWith(
ICLSubscriber.notifyBurn.selector, BurnNotificationReverted.selector
);
}
}

function _notifyModifyLiquidity(uint256 tokenId, int256 liquidityChange, BalanceDelta feesAccrued) internal {
ICLSubscriber _subscriber = subscriber[tokenId];

Expand Down
2 changes: 2 additions & 0 deletions src/pool-cl/interfaces/ICLNotifier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ interface ICLNotifier {
error SubscriptionReverted(address subscriber, bytes reason);
/// @notice Wraps the revert message of the subscriber contract on a reverting modify liquidity notification
error ModifyLiquidityNotificationReverted(address subscriber, bytes reason);
/// @notice Wraps the revert message of the subscriber contract on a reverting burn notification
error BurnNotificationReverted(address subscriber, bytes reason);
/// @notice Wraps the revert message of the subscriber contract on a reverting transfer notification
error TransferNotificationReverted(address subscriber, bytes reason);
/// @notice Thrown when a tokenId already has a subscriber
Expand Down
14 changes: 14 additions & 0 deletions src/pool-cl/interfaces/ICLSubscriber.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
pragma solidity ^0.8.0;

import {BalanceDelta} from "pancake-v4-core/src/types/BalanceDelta.sol";
import {CLPositionInfo} from "../libraries/CLPositionInfoLibrary.sol";

/// @notice Interface that a Subscriber contract should implement to receive updates from the v4 cl pool position manager
interface ICLSubscriber {
Expand All @@ -13,6 +14,19 @@ interface ICLSubscriber {
/// @dev Because of EIP-150, solidity may only allocate 63/64 of gasleft()
/// @param tokenId the token ID of the position
function notifyUnsubscribe(uint256 tokenId) external;
/// @notice Called when a position is burned
/// @param tokenId the token ID of the position
/// @param owner the current owner of the tokenId
/// @param info information about the position
/// @param liquidity the amount of liquidity decreased in the position, may be 0
/// @param feesAccrued the fees accrued by the position if liquidity was decreased
function notifyBurn(
uint256 tokenId,
address owner,
CLPositionInfo info,
uint256 liquidity,
BalanceDelta feesAccrued
) external;
/// @param tokenId the token ID of the position
/// @param liquidityChange the change in liquidity on the underlying position
/// @param feesAccrued the fees to be collected from the position as a result of the modifyLiquidity call
Expand Down
10 changes: 10 additions & 0 deletions test/pool-cl/mocks/MockCLBadSubscribers.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pragma solidity ^0.8.20;
import {ICLSubscriber} from "../../../src/pool-cl/interfaces/ICLSubscriber.sol";
import {CLPositionManager} from "../../../src/pool-cl/CLPositionManager.sol";
import {BalanceDelta} from "pancake-v4-core/src/types/BalanceDelta.sol";
import {CLPositionInfo} from "../../../src/pool-cl/libraries/CLPositionInfoLibrary.sol";

/// @notice A subscriber contract that returns values from the subscriber entrypoints
contract MockCLReturnDataSubscriber is ICLSubscriber {
Expand All @@ -13,6 +14,7 @@ contract MockCLReturnDataSubscriber is ICLSubscriber {
uint256 public notifyUnsubscribeCount;
uint256 public notifyModifyLiquidityCount;
uint256 public notifyTransferCount;
uint256 public notifyBurnCount;

error NotAuthorizedNotifer(address sender);

Expand Down Expand Up @@ -52,6 +54,10 @@ contract MockCLReturnDataSubscriber is ICLSubscriber {
notifyTransferCount++;
}

function notifyBurn(uint256, address, CLPositionInfo, uint256, BalanceDelta) external {
notifyBurnCount++;
}

function setReturnDataSize(uint256 _value) external {
memPtr = _value;
}
Expand Down Expand Up @@ -86,6 +92,10 @@ contract MockCLRevertSubscriber is ICLSubscriber {
revert TestRevert("notifyUnsubscribe");
}

function notifyBurn(uint256, address, CLPositionInfo, uint256, BalanceDelta) external {
revert TestRevert("notifyBurn");
}

function notifyModifyLiquidity(uint256, int256, BalanceDelta) external view onlyByPosm {
revert TestRevert("notifyModifyLiquidity");
}
Expand Down
Loading

0 comments on commit 297be6f

Please sign in to comment.