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

Settle pair #242

Merged
merged 6 commits into from
Aug 2, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
151341
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
345169
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
344710
Original file line number Diff line number Diff line change
@@ -1 +1 @@
370923
370969
1 change: 1 addition & 0 deletions .forge-snapshots/PositionManager_mint_withSettlePair.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
371342
10 changes: 10 additions & 0 deletions src/PositionManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ contract PositionManager is
} else if (action == Actions.SETTLE) {
(Currency currency, uint256 amount, bool payerIsUser) = params.decodeCurrencyUint256AndBool();
_settle(currency, _mapPayer(payerIsUser), _mapSettleAmount(amount, currency));
} else if (action == Actions.SETTLE_PAIR) {
(Currency currency0, Currency currency1) = params.decodeCurrencyPair();
_settlePair(currency0, currency1);
} else if (action == Actions.SWEEP) {
(Currency currency, address to) = params.decodeCurrencyAndAddress();
_sweep(currency, _mapRecipient(to));
Expand Down Expand Up @@ -214,6 +217,13 @@ contract PositionManager is
poolManager.clear(currency, uint256(currencyDelta));
}

function _settlePair(Currency currency0, Currency currency1) internal {
// the locker is the payer when settling
address caller = _msgSender();
_settle(currency0, caller, _getFullSettleAmount(currency0));
_settle(currency1, caller, _getFullSettleAmount(currency1));
}

/// @dev this is overloaded with ERC721Permit._burn
function _burn(
uint256 tokenId,
Expand Down
22 changes: 11 additions & 11 deletions src/libraries/Actions.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ library Actions {

// closing deltas on the pool manager
// settling
uint256 constant SETTLE_ALL = 0x10;
uint256 constant SETTLE = 0x11;
uint256 constant SETTLE_ALL = 0x09;
uint256 constant SETTLE = 0x10;
uint256 constant SETTLE_PAIR = 0x11;
// taking
uint256 constant TAKE = 0x13;
uint256 constant TAKE_ALL = 0x14;
uint256 constant TAKE_PORTION = 0x15;
uint256 constant TAKE = 0x12;
uint256 constant TAKE_ALL = 0x13;
uint256 constant TAKE_PORTION = 0x14;

uint256 constant CLOSE_CURRENCY = 0x16;
uint256 constant CLOSE_PAIR = 0x17;
uint256 constant CLEAR = 0x18;
uint256 constant SWEEP = 0x19;
uint256 constant CLOSE_CURRENCY = 0x15;
uint256 constant CLEAR = 0x16;
uint256 constant SWEEP = 0x17;

// minting/burning 6909s to close deltas
uint256 constant MINT_6909 = 0x20;
uint256 constant BURN_6909 = 0x21;
uint256 constant MINT_6909 = 0x18;
uint256 constant BURN_6909 = 0x19;
}
8 changes: 8 additions & 0 deletions src/libraries/CalldataDecoder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,14 @@ library CalldataDecoder {
}
}

/// @dev equivalent to: abi.decode(params, (Currency, Currency)) in calldata
function decodeCurrencyPair(bytes calldata params) internal pure returns (Currency currency0, Currency currency1) {
assembly ("memory-safe") {
currency0 := calldataload(params.offset)
currency1 := calldataload(add(params.offset, 0x20))
}
}

/// @dev equivalent to: abi.decode(params, (Currency, address)) in calldata
function decodeCurrencyAndAddress(bytes calldata params)
internal
Expand Down
42 changes: 0 additions & 42 deletions src/libraries/CurrencyDeltas.sol

This file was deleted.

8 changes: 8 additions & 0 deletions test/libraries/CalldataDecoder.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,14 @@ contract CalldataDecoderTest is Test {
assertEq(Currency.unwrap(currency), Currency.unwrap(_currency));
}

function test_fuzz_decodeCurrencyPair(Currency _currency0, Currency _currency1) public view {
bytes memory params = abi.encode(_currency0, _currency1);
(Currency currency0, Currency currency1) = decoder.decodeCurrencyPair(params);

assertEq(Currency.unwrap(currency0), Currency.unwrap(_currency0));
assertEq(Currency.unwrap(currency1), Currency.unwrap(_currency1));
}

function test_fuzz_decodeCurrencyAndUint256(Currency _currency, uint256 _amount) public view {
bytes memory params = abi.encode(_currency, _amount);
(Currency currency, uint256 amount) = decoder.decodeCurrencyAndUint256(params);
Expand Down
82 changes: 0 additions & 82 deletions test/libraries/CurrencyDeltas.t.sol

This file was deleted.

4 changes: 4 additions & 0 deletions test/mocks/MockCalldataDecoder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ contract MockCalldataDecoder {
return params.decodeCurrency();
}

function decodeCurrencyPair(bytes calldata params) external pure returns (Currency currency0, Currency currency1) {
return params.decodeCurrencyPair();
}

function decodeCurrencyAndUint256(bytes calldata params) external pure returns (Currency currency, uint256 _uint) {
return params.decodeCurrencyAndUint256();
}
Expand Down
71 changes: 0 additions & 71 deletions test/mocks/MockCurrencyDeltaReader.sol

This file was deleted.

40 changes: 36 additions & 4 deletions test/position-managers/Execute.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ contract ExecuteTest is Test, PosmTestSetup, LiquidityFuzzers {
assertEq(liquidity, initialLiquidity + liquidityToAdd);
}

function test_fuzz_execute_increaseLiquidity_twice(
function test_fuzz_execute_increaseLiquidity_twice_withClose(
uint256 initialLiquidity,
uint256 liquidityToAdd,
uint256 liquidityToAdd2
Expand All @@ -101,7 +101,39 @@ contract ExecuteTest is Test, PosmTestSetup, LiquidityFuzzers {
abi.encode(tokenId, config, liquidityToAdd2, MAX_SLIPPAGE_INCREASE, MAX_SLIPPAGE_INCREASE, ZERO_BYTES)
);

bytes memory calls = planner.finalizeModifyLiquidity(config.poolKey);
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);

assertEq(liquidity, initialLiquidity + liquidityToAdd + liquidityToAdd2);
}

function test_fuzz_execute_increaseLiquidity_twice_withSettlePair(
uint256 initialLiquidity,
uint256 liquidityToAdd,
uint256 liquidityToAdd2
) public {
initialLiquidity = bound(initialLiquidity, 1e18, 1000e18);
liquidityToAdd = bound(liquidityToAdd, 1e18, 1000e18);
liquidityToAdd2 = bound(liquidityToAdd2, 1e18, 1000e18);
uint256 tokenId = lpm.nextTokenId();
mint(config, initialLiquidity, address(this), ZERO_BYTES);

Plan memory planner = Planner.init();

planner.add(
Actions.INCREASE_LIQUIDITY,
abi.encode(tokenId, config, liquidityToAdd, MAX_SLIPPAGE_INCREASE, MAX_SLIPPAGE_INCREASE, ZERO_BYTES)
);
planner.add(
Actions.INCREASE_LIQUIDITY,
abi.encode(tokenId, config, liquidityToAdd2, MAX_SLIPPAGE_INCREASE, MAX_SLIPPAGE_INCREASE, ZERO_BYTES)
);

bytes memory calls = planner.finalizeModifyLiquidityWithSettlePair(config.poolKey);
lpm.modifyLiquidities(calls, _deadline);

bytes32 positionId =
Expand Down Expand Up @@ -131,7 +163,7 @@ contract ExecuteTest is Test, PosmTestSetup, LiquidityFuzzers {
abi.encode(tokenId, config, liquidityToAdd, MAX_SLIPPAGE_INCREASE, MAX_SLIPPAGE_INCREASE, ZERO_BYTES)
);

bytes memory calls = planner.finalizeModifyLiquidity(config.poolKey);
bytes memory calls = planner.finalizeModifyLiquidityWithClose(config.poolKey);
lpm.modifyLiquidities(calls, _deadline);

bytes32 positionId =
Expand Down Expand Up @@ -178,7 +210,7 @@ contract ExecuteTest is Test, PosmTestSetup, LiquidityFuzzers {
newConfig, newLiquidity, MAX_SLIPPAGE_INCREASE, MAX_SLIPPAGE_INCREASE, Constants.MSG_SENDER, ZERO_BYTES
)
);
bytes memory calls = planner.finalizeModifyLiquidity(config.poolKey);
bytes memory calls = planner.finalizeModifyLiquidityWithClose(config.poolKey);

lpm.modifyLiquidities(calls, _deadline);
{
Expand Down
2 changes: 1 addition & 1 deletion test/position-managers/IncreaseLiquidity.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ contract IncreaseLiquidityTest is Test, PosmTestSetup, Fuzzers {
planner.add(
Actions.INCREASE_LIQUIDITY, abi.encode(tokenIdAlice, config, liquidityDelta, 0 wei, 0 wei, ZERO_BYTES)
);
bytes memory calls = planner.finalizeModifyLiquidity(config.poolKey);
bytes memory calls = planner.finalizeModifyLiquidityWithClose(config.poolKey);
vm.startPrank(alice);
lpm.modifyLiquidities(calls, _deadline);
vm.stopPrank();
Expand Down
Loading
Loading