Skip to content

mock-amm.initialize doesn't validate rate_bps bounds - #68

Closed
Johnpii1 wants to merge 4 commits into
Pi-Defi-world:mainfrom
Johnpii1:main
Closed

mock-amm.initialize doesn't validate rate_bps bounds#68
Johnpii1 wants to merge 4 commits into
Pi-Defi-world:mainfrom
Johnpii1:main

Conversation

@Johnpii1

Copy link
Copy Markdown
Contributor

Summary

Adds validation for the mock AMM pool rate during initialization to prevent invalid configurations that could result in zero-value swap outputs or arithmetic overflows.

The initialization flow now enforces explicit minimum and maximum rate bounds and returns a clear error when the provided rate is outside the supported range.

Closes #22

Type of change

  • Bug fix
  • New feature
  • Refactor (no behavioral change)
  • Test-only
  • Docs only
  • CI/workflow change

Checklist

  • Branch is based on the latest main and the PR targets main.

  • Local checks pass:

    • cargo test -p mock-amm
    • Full Rust workspace checks not run.
    • Relayer checks not applicable to this change.
  • No secrets, keys, or .env files are committed.

  • Tests cover the new validation behavior.

  • CHANGELOG.md updated if required.

  • COMPATIBILITY.md / protocol-versions.json not applicable.

  • Policy documentation not applicable.

Impact

This change improves mock AMM safety by preventing invalid pool-rate configuration at initialization.

  • rate_bps = 0 is rejected to prevent swaps from producing consistently zero outputs.
  • Rates above 1,000,000 BPS are rejected to prevent unreasonable configurations and potential arithmetic overflow scenarios.
  • Valid rates continue to initialize the existing Admin, TokenIn, TokenOut, and Rate storage entries normally.
  • No changes are made to bridge mint/redeem behavior, eligibility policy, protocol compatibility, or operational tooling.

Changes

  • Added MIN_RATE_BPS: u32 = 1.

  • Added MAX_RATE_BPS: u32 = 1_000_000.

  • Added the Error::InvalidRate error variant.

  • Updated initialize to return Result<(), Error>.

  • Added rate validation before persisting initialization state.

  • Invalid rates now return Err(Error::InvalidRate).

  • Valid rates return Ok(()) and preserve the existing storage behavior.

  • Added unit tests for:

    • Zero-rate rejection.
    • Excessive-rate rejection.
  • Added Soroban test snapshots covering invalid initialization cases.

Testing

Test Command

cargo test -p mock-amm

Test Results

2 passed; 0 failed

The new tests verify:

client.try_initialize(..., &0)
→ Err(Ok(Error::InvalidRate))

client.try_initialize(..., &(MAX_RATE_BPS + 1))
→ Err(Ok(Error::InvalidRate))

Screenshots / logs

No UI changes. Test output is provided above.

Additional context

The validation is performed during initialization so invalid pool configuration cannot be persisted. Existing valid initialization behavior remains unchanged.

@coderabbitai

coderabbitai Bot commented Aug 19, 2026

Copy link
Copy Markdown

Important

  • 🔍 Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 3e38c7ba-54dc-46e1-a214-92e9131b617d


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@Johnpii1
Johnpii1 requested a review from likableai as a code owner August 20, 2026 11:34
@Johnpii1 Johnpii1 closed this by deleting the head repository Aug 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Issue 22 — mock-amm.initialize doesn't validate rate_bps bounds

1 participant