<!-- canonical: https://0xsimao.com/findings/nftperp-i-calculates-margin-updating-payment -->

# removeLiquidity() in the amm calculates marginToRemove without updating the margin with the funding payment

Medium · Three Sigma · NFT perpetuals · 2nd January, 2024

Finding 3S-NFTPerp-L05 of the Nftperp Exchange security review.

- Report: /reports/nftperp-i
- Source: https://cdn.sanity.io/files/qoqld077/production/c19530de75e234ad15694b4563edb1fc9d2a3fd8.pdf

---

### Description

[removeLiquidity()](https://github.com/nftperp/NFTPerp-V2-Contracts/blob/1c16f2010a851ac23aec73822d55388901bbe5e7/src/AMM.sol#L645) removes the margin from the position pro-rata to the removed shares,
without considering the previous funding payment. The impact should be limited to the
case when a significant amount of shares is removed and the fundingPayment is applied
after the margin removal, which may lead to bad debt and [revert](https://github.com/nftperp/NFTPerp-V2-Contracts/blob/1c16f2010a851ac23aec73822d55388901bbe5e7/src/AMM.sol#L647).

### Recommendation

Add the fundingPayment to the margin and only then calculate marginToRemove.

### Status

Addressed in the AMMRouter

---

Related findings:

- [Notional difference in AmmRouter:removeLiquidty() may revert in certain cases](https://0xsimao.com/findings/nftperp-ii-notional-difference-liquidty-revert): Nftperp Matching Engine
- [Providing liquidity to the AMM does not check the return value of actually provided tokens leading to locked funds.](https://0xsimao.com/findings/cork-protocol-providing-actually-provided-locked): Cork Protocol
- [Note footers should not be 0 in curveRemoveLiquidity() if the corresponding assetOuts are non null](https://0xsimao.com/findings/singularity-footers-curve-corresponding-outs): Singularity
- [Unused noteCommitment parameter in UniswapRemoveLiquidityInputs struct](https://0xsimao.com/findings/singularity-parameter-uniswap-inputs-struct): Singularity
