Skip to content

fix: decimals-aware market-core with 256-bit mul_div intermediates - #164

Open
Emmazlee wants to merge 12 commits into
OracleDesk:developfrom
Emmazlee:fix/issue-3-support-collateral-of-any-decimals-with-256-bit-r2
Open

Emmazlee wants to merge 12 commits into
OracleDesk:developfrom
Emmazlee:fix/issue-3-support-collateral-of-any-decimals-with-256-bit-r2

Conversation

@Emmazlee

@Emmazlee Emmazlee commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Overview

This PR makes market-core decimals-aware: collateral decimals() is read once at construction, the economic bounds (min_seed, max_sets) are derived from those decimals, and the fixed-point multiply-divide helpers move to 256-bit intermediates so correctness no longer depends on the 1e30 headroom argument. The Python and TypeScript mirrors are kept in lockstep so off-chain quoting matches on-chain behavior.

Related Issue

Changes

🔢 Decimals-aware bounds

  • [MODIFY] contracts/market-core/src/math.rs
    • Added Bounds { min_seed, max_sets } and bounds_for_decimals(d) deriving min_seed = 100 * 10^d and max_sets = 1e8 * 10^d via checked_pow, with d in 0..=18.
    • Kept math::MAX_SETS as a #[deprecated] 7-decimal alias for tests only.
    • Updated the module header conventions to describe decimals handling instead of the 1e30 headroom argument.

🧮 256-bit widening arithmetic (Env-free)

  • [MODIFY] contracts/market-core/src/math.rs
    • Added an Env-free wide module with U256 { hi, lo }, mul_u128, and divrem_u256_by_u128 (returns Overflow when the quotient exceeds u128::MAX).
    • Rewrote mul_div_floor / mul_div_ceil to use 256-bit intermediates, preserving existing rounding directions; signatures unchanged so no caller changes.
    • math.rs still has no soroban_sdk imports and its tests run natively.

🐍 Python mirror

  • [MODIFY] model/fpmm_model.py
    • Added bounds_for_decimals and an explicit _fits_i128 assertion in mul_div_* so the model fails where the contract would.
    • Added 18-decimal fixtures matching the Rust outputs.

🟦 TypeScript mirror

  • [MODIFY] agents/core/fpmm.ts
    • Added boundsForDecimals(d) and I128_MAX checks in mulDivFloor / mulDivCeil.
    • Added 18-decimal fixtures matching the Rust outputs.

📚 Docs

  • [MODIFY] docs/architecture.md — updated the market size bound description to reflect decimals-derived max_sets.
  • [MODIFY] docs/STATUS.md — updated references that assumed 7 decimals.

Verification Results

cargo test -p market-core && cargo test --workspace
make lint
python3 model/fpmm_model.py
cd agents && npm test && npm run build
Acceptance Criteria Status
DataKey::Decimals set in __constructor, Error::UnsupportedDecimals added, Config.decimals returned by get_config ✅
MIN_SEED / MAX_SETS no longer used by contract logic; bounds come from bounds_for_decimals ✅
mul_div_floor / mul_div_ceil use 256-bit intermediates with bit-identical results on the legacy domain ✅
math.rs has no soroban_sdk imports and its tests run natively ✅
settle_value uses checked addition; redeem / claim_pool_remainder propagate errors ✅
Python and TS mirrors include 18-decimal fixtures matching Rust outputs ✅
CPU instruction delta for buy reported ✅ (see cost_buy_7dp_vs_18dp)
Vendored wasm copies and bindings packages regenerated and committed ✅
cargo test --workspace, make lint, python3 model/fpmm_model.py, agents npm test / npm run build pass ✅
math.rs header, docs/architecture.md and docs/STATUS.md updated to describe decimals handling ✅

Closes #3

@Emmazlee Emmazlee changed the title fix: support any collateral decimals with 256-bit mul_div in market-core fix: decimals-aware market-core bounds and 256-bit mul_div Oct 19, 2026
@Emmazlee Emmazlee changed the title fix: decimals-aware market-core bounds and 256-bit mul_div fix: decimals-aware market-core with 256-bit mul_div intermediates Oct 20, 2026
Emmazlee added a commit to Emmazlee/OracleDesk-SmartContract that referenced this pull request Oct 24, 2026
Emmazlee added a commit to Emmazlee/OracleDesk-SmartContract that referenced this pull request Oct 24, 2026
@Emmazlee
Emmazlee force-pushed the fix/issue-3-support-collateral-of-any-decimals-with-256-bit-r2 branch from e849c52 to 7d8cc01 Compare October 24, 2026 09:34
@Emmazlee
Emmazlee force-pushed the fix/issue-3-support-collateral-of-any-decimals-with-256-bit-r2 branch from 7d8cc01 to 660c69f Compare October 24, 2026 09:36
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.

Support collateral of any decimals with 256-bit mul_div intermediates in market-core

1 participant