Price Within Ticks
Make sure that the price is within the tick range
Use Case
In Automated Market Makers (AMMs), particularly those using concentrated liquidity models like Uniswap V3, the price is represented by ticks. A tick is a discrete unit of measurement for the price of an asset, and maintaining proper tick boundaries is critical for protocol security.
This assertion is particularly important for:
- Preventing price manipulation through tick boundary violations
- Ensuring trades execute at valid price points
- Maintaining protocol invariants around price representation
- Detecting potential exploits that could manipulate tick boundaries
For example, if an attacker could push the price outside the valid tick range, they could potentially:
- Execute trades at invalid prices
- Manipulate liquidity positions
- Cause protocol calculations to fail
Applicable Protocols
- AMMs using concentrated liquidity models (e.g., Uniswap V3, PancakeSwap V3)
- Protocols that rely on tick-based price calculations
- DeFi protocols that interact with tick-based AMMs
- Cross-chain bridges that use tick-based price feeds
Explanation
The assertion implements a focused approach to verify tick integrity:
-
Tick Range Validation:
- Verifies that ticks stay within global bounds (-887272 to 887272)
- Prevents tick manipulation outside valid ranges
- Ensures the AMM’s mathematical model remains valid
-
Tick Spacing Alignment:
- Ensures ticks align with the pool’s tick spacing
- Maintains proper concentrated liquidity calculations
- Prevents position management errors
The assertion uses the following cheatcodes and functions:
ph.forkPreState()
: Captures the initial tick stateph.forkPostState()
: Captures the final tick stateregisterStorageChangeTrigger()
: Monitors changes to tick-related storage slots
This focused approach ensures:
- Ticks remain within protocol-defined bounds
- Tick spacing rules are enforced
- Core AMM functionality remains secure
For more information about cheatcodes, see the Cheatcodes Documentation.
Code Example
Note: This code example is maintained in the Phylax Assertion Examples Repository. For a full examples with mock protocol code and tests please refer to the repository.
Testing
To test this assertion:
- Deploy a mock Uniswap V3 pool contract
- Create test scenarios that:
- Execute trades within valid tick ranges
- Attempt trades that would push ticks outside bounds
- Test tick spacing alignment
Assertion Best Practices
- Combine this assertion with other AMM-related assertions like Fee Calculations
- Consider protocol-specific parameters for:
- Tick spacing requirements
- Global tick bounds (if different from Uniswap V3)