<!-- canonical: https://0xsimao.com/findings/nftperp-ii-flag-decrease-index-heuristic -->

# Use a flag to increase or decrease the index of the pool instead of a heuristic

Medium · Three Sigma · NFT perpetuals · 29th January, 2024

Finding 3S-NFTPerp-L03 of the Nftperp Matching Engine security review.

- Report: /reports/nftperp-ii
- Source: https://cdn.sanity.io/files/qoqld077/production/87f617e82d5468500e950a669f30607376b37c32.pdf

---

### Description

[AmmRouter:_shitPool()](https://github.com/nftperp/NFTPerp-V2-Contracts/blob/8ea05fc0ecf299ca8374b571486846652c8725f8/src/AMMRouter.sol#L757) increases the index if the price is within 100 of the upper bound of
the current pool price. This is error prone as a fill could for example increase the price of a
pool, but not be close enough to the upper bound, making the index decrease, entering in a
wrong state.

### Recommendation

Use the direction flag to increase or decrease the index, as in
[AmmRouter:_getQuoteValue()](https://github.com/nftperp/NFTPerp-V2-Contracts/blob/8ea05fc0ecf299ca8374b571486846652c8725f8/src/AMMRouter.sol#L809).

### Status

Addressed in [#68c722c](https://github.com/nftperp/NFTPerp-V2-Contracts/commit/68c722ccb3e71532ac8642e97be45c072dba4e05)

---

Related findings:

- [Adapters could store the index instead of true in the `VaultV2` to remove the O(n) search](https://0xsimao.com/findings/morpho-vault-v2-adapters-store-index-search): Morpho Vault V2
- [safeApprove(...) reverts if approval is different than 0, use safeIncreaseAllowance(...) instead](https://0xsimao.com/findings/fuji-finance-approve-reverts-approval-allowance): Fuji Finance
