Testing-suite enhancements for liquidity-pool stability
In this section, we note enhancements that have been made to the testing suite.
1. Circular trading test
The initial test suite lacked validation of complex trading scenarios that simulate real-world activity. To address this, a circular trading test was implemented, executing multiple rounds of back-and-forth trades (10 rounds of A → B followed by B → A swaps). This test verifies that the protocol maintains stable liquidity and reasonable balance ratios under repeated trading pressure.
Key validations
Final liquidity matches initial liquidity exactly.
Token balances stay within a 5% deviation.
Test breakdown
Initial setup. The pool is created with fixed liquidity and starting token balances.
Circular trading. A series of 10 small round-trip trades is performed within a single price band (tick). Each trade causes minor shifts in token balances, but the price (tick) remains stable since all liquidity is provided in that tick range.
Final state. After all trades, total liquidity remains unchanged. Token balances shift slightly due to accumulated fees and rounding adjustments.
Deviations analysis. Small increase in balance A (+3%) and decrease in balance B (-5%). Deviations fall within acceptable system limits.
2. Cross-tick trading test
Building on this, another test was developed to evaluate cross-tick scenarios, focusing on
Tick-boundary transitions
Fee accumulation during cross-boundary swaps
Handling of varying liquidity depths
These tests stress test the protocol’s ability to handle real-world trading patterns while maintaining a consistent state.
Test breakdown
Pool initialization. The pool starts with a price at tick 12344. Sufficient liquidity ensures deep markets. Token A and B balances are predefined.
Forward swaps (token A → token B). Swaps gradually move the tick downward. Each swap (tiny, small, medium, large) shifts the tick step by step, consuming liquidity at different price ranges. Larger swaps cause bigger tick movements, as confirmed in debug outputs.
Reverse swaps (token B → token A). Reverse swaps push the tick back up. The final tick (12199) is close to the initial value, showing minimal drift after a round trip.
Invariants and deviations. Liquidity remains unchanged.
Observed core invariants
Liquidity is preserved.
Tick changes remain within acceptable bounds.
Balance deviations, though high (up to 70%), align with expectations due to fees and rounding effects.
We provided these tests to Magma Finance at the end of the audit.