primitivefinance / portfolio

Portfolio is an automated market making protocol for implementing custom strategies at the lowest cost possible.

Home Page:https://www.primitive.xyz/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Fix: Chaining deallocates before swaps can lead to reverts because of swap computations

Alexangelj opened this issue · comments

Description

The swap operation accepts the input and output of a swap. This output is an absolute value in native token decimals, which has to be computed pre-emptively using getAmountOut. However, the getAmountOut function will use the pool.liquidity for computing how many tokens are being swapped in PER WAD of liquidity.

Therefore, if there is a deallocate operation before a swap operation for the same pool, it will most likely fail because the contract's default getAmountOut uses the previous liquidity, not the liquidity amount after the deallocate call.

It's possible to avoid this by using the RMM01Lib's getAmountOut function using an edited PortfolioPool struct that applies the expected liquidity change.

The fix for this is not easy - in the mean time it should at least be documented somewhere.

Could be remediated with an additional deltaLiquidity parameter in the getAmountOut function

Don't you expect swaps to fail if LPs are pulling liquidity before the swap? It also was like this before:

virtualY -= deltaOutput / liquidity

The less liquidity the more my desired output impacts the reserves per liquidity

TODO:

  • Add a liquidityDelta parameter to getAmountOut and getSwapData

PR was merged.