Skip to content

Support variable fee per UniswapV2 pool#536

Open
Haikane wants to merge 2 commits intomainfrom
feat/v2-variable-fee
Open

Support variable fee per UniswapV2 pool#536
Haikane wants to merge 2 commits intomainfrom
feat/v2-variable-fee

Conversation

@Haikane
Copy link
Copy Markdown
Contributor

@Haikane Haikane commented Feb 21, 2026

Summary

  • Add fee_bps field to UniswapV2State (defaults to 30, serde-compatible with existing serialized states)
  • Decoder reads fee from static_attributes on the component snapshot
  • All fee calculations use per-pool fee instead of hardcoded constant
  • Bounds validation: new_with_fee() asserts fee_bps <= 10000 to prevent arithmetic underflow in fee_numerator(). Decoder returns ValueError for out-of-range fees.

Test plan

  • 28/28 UniV2 unit tests pass (state + decoder)
  • 38/38 query_pool_swap tests pass
  • Custom fee decoding test (fee=25)
  • Edge case tests: fee=0, fee=10000, fee>10000 (rejected), missing fee (defaults to 30), empty fee bytes
  • Full crate compiles with all features

🤖 Generated with Claude Code

Read fee_bps from the component's static_attributes instead of using
a hardcoded 30 bps constant. Default to 30 if the attribute is absent
for backward compatibility with existing pools.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Prevent arithmetic underflow in fee_numerator() when fee_bps > 10000 by
adding an assertion in new_with_fee() and a ValueError check in the
decoder. Add tests for zero fee, max boundary, exceeds-max, missing fee
default, and empty fee bytes.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@Haikane
Copy link
Copy Markdown
Contributor Author

Haikane commented Feb 21, 2026

Review fixes pushed

Addressed findings from the multi-agent code review:

Bounds validation (d12e2af1)

  • state.rs:new_with_fee() — Added assert!(fee_bps <= 10000) to prevent arithmetic underflow in fee_numerator() where 10000 - fee_bps would wrap for values > 10000.
  • decoder.rs — Added InvalidSnapshotError::ValueError check when decoding fee from static_attributes, so invalid fee values from the indexer are caught at decode time instead of panicking later.

Edge case tests (d12e2af1)

Added 7 new tests:

  • test_new_with_fee_exceeds_max — verifies panic on fee > 10000
  • test_new_with_fee_zero — verifies zero-fee pool (numerator = 10000)
  • test_new_with_fee_boundary — verifies 100% fee edge (numerator = 0)
  • test_usv2_try_from_missing_fee_defaults_to_30 — verifies default path
  • test_usv2_try_from_zero_fee — verifies zero-fee decode
  • test_usv2_try_from_fee_exceeds_max — verifies decoder rejects > 10000
  • test_usv2_try_from_empty_fee_bytes — verifies empty bytes => fee=0

All 28 uniswap_v2 tests pass.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

1 participant