Back
Challenge 20 - Secure CPMM Swap Flow
HardCPMMEnd to End20 / 20

Challenge 20: Secure CPMM Swap Flow

This challenge combines the most important ideas in the track.

Task

Implement secure_swap_output.

Required behavior

1. State guard

If the pool is paused, return:

Err("pool paused")

2. Fee-adjusted input

Use:

effective_input = x_in * 9_970 / 10_000

3. Constant-product quote

Use:

k = x_reserve * y_reserve
new_x = x_reserve + effective_input
new_y = k / new_x
output = y_reserve - new_y

4. Slippage guard

If output < min_out, return:

Err("slippage exceeded")

5. Success case

Return:

Ok(output)

Sample

For (Active, 1000, 2000, 100, 180), the correct result is:

181
LanguageRust
Loading editor...
Run works without login. Submit requires sign-in so completion can be saved.
Run cases to compare your output against each configured input. Submit saves progress only when every case passes.
    Challenge 20 - Secure CPMM Swap Flow · CPMM Exercise | learn.sol