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

posm slippage #225

Merged
merged 16 commits into from
Aug 2, 2024
Merged

posm slippage #225

merged 16 commits into from
Aug 2, 2024

Conversation

saucepoint
Copy link
Collaborator

@saucepoint saucepoint commented Jul 30, 2024

Related Issue

Closes #177

Description of changes

Slippage parameters and checks on:

  • mint
  • increase
  • decrease (and collect)
  • burn

Note: observing off-by-one wei #192 for decrease and burn. i.e. minting and burning the position (without moving price) is returning original delta - 1 wei

@saucepoint saucepoint added the posm position manager label Jul 30, 2024
@saucepoint saucepoint requested a review from snreynolds July 30, 2024 02:05

if (positionConfigs[tokenId] != config.toId()) revert IncorrectPositionConfigForTokenId(tokenId);
// Note: The tokenId is used as the salt for this position, so every minted position has unique storage in the pool manager.
BalanceDelta liquidityDelta = _modifyLiquidity(config, liquidity.toInt256(), bytes32(tokenId), hookData);
if (liquidityDelta.amount0() < 0 && amount0Max < uint128(-liquidityDelta.amount0())) revert SlippageExceeded();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

helper func! same code is happening twice

_checkSlippage(amount, amountMax);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a case where liquidityDelta < 0 ? I guess a weird hook?

Copy link
Collaborator Author

@saucepoint saucepoint Jul 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

liqDelta < 0 -- majority of cases, users paying tokens to increase liquidity. where we want to ensure slippage

0 < liqDelta -- cases where fee revenue spillover needs to be collected

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right i forgot about spillover case

Comment on lines 162 to 163
if (amount0Max < uint128(-liquidityDelta.amount0())) revert SlippageExceeded();
if (amount1Max < uint128(-liquidityDelta.amount1())) revert SlippageExceeded();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this slippage check different from _increase? shouldn't mint and increase be the same?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_mint is guaranteed to be negative delta so the conditional is cleaner

_increase can be positive (spill over fee revenue to be collected), where a slippage check is irrelevant

src/PositionManager.sol Outdated Show resolved Hide resolved
@@ -114,7 +114,9 @@ contract IncreaseLiquidityTest is Test, PosmTestSetup, Fuzzers {

// TODO: Can we make this easier to re-invest fees, so that you don't need to know the exact collect amount?
Plan memory planner = Planner.init();
planner.add(Actions.INCREASE_LIQUIDITY, abi.encode(tokenIdAlice, config, liquidityDelta, ZERO_BYTES));
planner.add(
Actions.INCREASE_LIQUIDITY, abi.encode(tokenIdAlice, config, liquidityDelta, 0 wei, 0 wei, ZERO_BYTES)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how come this is 0 and not MAX_SLIPPAGE_INCREASE?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we're ~perfectly autocompounding fees in this test so a strict slippage (no tokens paid by the caller) is an additional assurance

test/position-managers/PositionManager.gas.t.sol Outdated Show resolved Hide resolved
Copy link
Member

@snreynolds snreynolds left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 comment ow looks good

src/libraries/SlippageCheck.sol Outdated Show resolved Hide resolved
@snreynolds snreynolds self-requested a review August 2, 2024 03:02
Copy link
Member

@snreynolds snreynolds left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏

@saucepoint saucepoint merged commit 83ca829 into main Aug 2, 2024
3 checks passed
@saucepoint saucepoint deleted the posm-slippage branch August 2, 2024 03:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
posm position manager
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[posm]: Add slippage checks to increase/decrease liquidity
2 participants