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

add liquidity view #270

Merged
merged 3 commits into from
Aug 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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/PositionManager_burn_empty.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
47167
47158
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_burn_empty_native.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
46984
46976
Original file line number Diff line number Diff line change
@@ -1 +1 @@
122980
122971
Original file line number Diff line number Diff line change
@@ -1 +1 @@
122689
122680
Original file line number Diff line number Diff line change
@@ -1 +1 @@
130058
130049
Original file line number Diff line number Diff line change
@@ -1 +1 @@
129768
129759
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_decrease_burnEmpty.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
134137
134128
Original file line number Diff line number Diff line change
@@ -1 +1 @@
126876
126868
Original file line number Diff line number Diff line change
@@ -1 +1 @@
416486
416464
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_permit.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
79467
79445
Original file line number Diff line number Diff line change
@@ -1 +1 @@
62355
62333
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_permit_twice.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
45243
45221
22 changes: 11 additions & 11 deletions src/PositionManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ contract PositionManager is
uint128 amount1Min,
bytes calldata hookData
) internal onlyIfApproved(msgSender(), tokenId) onlyValidConfig(tokenId, config) {
uint256 liquidity = uint256(_getPositionLiquidity(config, tokenId));
uint256 liquidity = uint256(getPositionLiquidity(tokenId, config));

BalanceDelta liquidityDelta;
// Can only call modify if there is non zero liquidity.
Expand Down Expand Up @@ -338,16 +338,6 @@ contract PositionManager is
}
}

function _getPositionLiquidity(PositionConfig calldata config, uint256 tokenId)
internal
view
returns (uint128 liquidity)
{
bytes32 positionId =
Position.calculatePositionKey(address(this), config.tickLower, config.tickUpper, bytes32(tokenId));
liquidity = poolManager.getPositionLiquidity(config.poolKey.toId(), positionId);
}

/// @notice Sweeps the entire contract balance of specified currency to the recipient
function _sweep(Currency currency, address to) internal {
uint256 balance = currency.balanceOfSelf();
Expand All @@ -370,6 +360,16 @@ contract PositionManager is
if (positionConfigs.hasSubscriber(id)) _notifyTransfer(id, from, to);
}

function getPositionLiquidity(uint256 tokenId, PositionConfig calldata config)
public
view
returns (uint128 liquidity)
{
bytes32 positionId =
Position.calculatePositionKey(address(this), config.tickLower, config.tickUpper, bytes32(tokenId));
liquidity = poolManager.getPositionLiquidity(config.poolKey.toId(), positionId);
}

/// @inheritdoc IPositionManager
function getPositionConfigId(uint256 tokenId) external view returns (bytes32) {
return positionConfigs.getConfigId(tokenId);
Expand Down
9 changes: 9 additions & 0 deletions src/interfaces/IPositionManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pragma solidity ^0.8.24;

import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol";
import {Currency} from "@uniswap/v4-core/src/types/Currency.sol";
import {PositionConfig} from "../libraries/PositionConfig.sol";

import {INotifier} from "./INotifier.sol";

Expand All @@ -22,4 +23,12 @@ interface IPositionManager is INotifier {
/// @return configId a truncated hash of the position's poolkey, tickLower, and tickUpper
/// @dev truncates the least significant bit of the hash
function getPositionConfigId(uint256 tokenId) external view returns (bytes32 configId);

/// @param tokenId the ERC721 tokenId
/// @return liquidity the position's liquidity, as a liquidityAmount
hensha256 marked this conversation as resolved.
Show resolved Hide resolved
/// @dev this value can be processed as an amount0 and amount1 by using the LiquidityAmounts library
function getPositionLiquidity(uint256 tokenId, PositionConfig calldata config)
external
view
returns (uint128 liquidity);
}
26 changes: 7 additions & 19 deletions test/position-managers/Execute.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ contract ExecuteTest is Test, PosmTestSetup, LiquidityFuzzers {

increaseLiquidity(tokenId, config, liquidityToAdd, ZERO_BYTES);

bytes32 positionId =
Position.calculatePositionKey(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenId));
(uint256 liquidity,,) = manager.getPositionInfo(config.poolKey.toId(), positionId);
uint256 liquidity = lpm.getPositionLiquidity(tokenId, config);

assertEq(liquidity, initialLiquidity + liquidityToAdd);
}
Expand Down Expand Up @@ -104,9 +102,7 @@ contract ExecuteTest is Test, PosmTestSetup, LiquidityFuzzers {
bytes memory calls = planner.finalizeModifyLiquidityWithClose(config.poolKey);
lpm.modifyLiquidities(calls, _deadline);

bytes32 positionId =
Position.calculatePositionKey(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenId));
(uint256 liquidity,,) = manager.getPositionInfo(config.poolKey.toId(), positionId);
uint256 liquidity = lpm.getPositionLiquidity(tokenId, config);

assertEq(liquidity, initialLiquidity + liquidityToAdd + liquidityToAdd2);
}
Expand Down Expand Up @@ -136,9 +132,7 @@ contract ExecuteTest is Test, PosmTestSetup, LiquidityFuzzers {
bytes memory calls = planner.finalizeModifyLiquidityWithSettlePair(config.poolKey);
lpm.modifyLiquidities(calls, _deadline);

bytes32 positionId =
Position.calculatePositionKey(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenId));
(uint256 liquidity,,) = manager.getPositionInfo(config.poolKey.toId(), positionId);
uint256 liquidity = lpm.getPositionLiquidity(tokenId, config);

assertEq(liquidity, initialLiquidity + liquidityToAdd + liquidityToAdd2);
}
Expand Down Expand Up @@ -171,9 +165,7 @@ contract ExecuteTest is Test, PosmTestSetup, LiquidityFuzzers {
bytes memory calls = planner.finalizeModifyLiquidityWithClose(config.poolKey);
lpm.modifyLiquidities(calls, _deadline);

bytes32 positionId =
Position.calculatePositionKey(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenId));
(uint256 liquidity,,) = manager.getPositionInfo(config.poolKey.toId(), positionId);
uint256 liquidity = lpm.getPositionLiquidity(tokenId, config);

assertEq(liquidity, initialLiquidity + liquidityToAdd);
}
Expand Down Expand Up @@ -242,20 +234,16 @@ contract ExecuteTest is Test, PosmTestSetup, LiquidityFuzzers {

{
// old position has no liquidity
bytes32 positionId =
Position.calculatePositionKey(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenId));
uint128 liquidity = manager.getPositionLiquidity(config.poolKey.toId(), positionId);
uint128 liquidity = lpm.getPositionLiquidity(tokenId, config);
assertEq(liquidity, 0);

// new token was minted
uint256 newTokenId = lpm.nextTokenId() - 1;
assertEq(lpm.ownerOf(newTokenId), address(this));

// new token has expected liquidity
positionId = Position.calculatePositionKey(
address(lpm), newConfig.tickLower, newConfig.tickUpper, bytes32(newTokenId)
);
liquidity = manager.getPositionLiquidity(config.poolKey.toId(), positionId);

liquidity = lpm.getPositionLiquidity(newTokenId, newConfig);
assertEq(liquidity, newLiquidity);
}
}
Expand Down
12 changes: 4 additions & 8 deletions test/position-managers/IncreaseLiquidity.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -305,16 +305,14 @@ contract IncreaseLiquidityTest is Test, PosmTestSetup, Fuzzers {
mint(config, liquidityAlice, alice, ZERO_BYTES);
vm.stopPrank();

bytes32 positionId =
Position.calculatePositionKey(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenIdAlice));
uint128 oldLiquidity = StateLibrary.getPositionLiquidity(manager, config.poolKey.toId(), positionId);
uint128 oldLiquidity = lpm.getPositionLiquidity(tokenIdAlice, config);

// bob can increase liquidity for alice even though he is not the owner / not approved
vm.startPrank(bob);
increaseLiquidity(tokenIdAlice, config, 100e18, ZERO_BYTES);
vm.stopPrank();

uint128 newLiquidity = StateLibrary.getPositionLiquidity(manager, config.poolKey.toId(), positionId);
uint128 newLiquidity = lpm.getPositionLiquidity(tokenIdAlice, config);

// assert liqudity increased by the correct amount
assertEq(newLiquidity, oldLiquidity + uint128(100e18));
Expand Down Expand Up @@ -645,9 +643,7 @@ contract IncreaseLiquidityTest is Test, PosmTestSetup, Fuzzers {
mint(config, liquidityAlice, alice, ZERO_BYTES);
uint256 tokenIdAlice = lpm.nextTokenId() - 1;

bytes32 positionId =
Position.calculatePositionKey(address(lpm), config.tickLower, config.tickUpper, bytes32(tokenIdAlice));
(uint256 liquidity,,) = manager.getPositionInfo(config.poolKey.toId(), positionId);
uint256 liquidity = lpm.getPositionLiquidity(tokenIdAlice, config);
assertEq(liquidity, liquidityAlice);

// alice increases with the balance in the position manager
Expand Down Expand Up @@ -682,7 +678,7 @@ contract IncreaseLiquidityTest is Test, PosmTestSetup, Fuzzers {
uint256 amount0 = uint128(-delta.amount0());
uint256 amount1 = uint128(-delta.amount1());

(liquidity,,) = manager.getPositionInfo(config.poolKey.toId(), positionId);
liquidity = lpm.getPositionLiquidity(tokenIdAlice, config);
assertEq(liquidity, 2 * liquidityAlice);

// The balances were swept back to this address.
Expand Down
Loading
Loading