Skip to content

CLOB Phase 0: market identity, typed units, price conversions, and event schema - #1095

Merged
IbrahimIjai merged 6 commits into
SO4-Markets:mainfrom
victor-olamide:feat/clob-phase0-foundations
Oct 21, 2026
Merged

IbrahimIjai merged 6 commits into
SO4-Markets:mainfrom
victor-olamide:feat/clob-phase0-foundations

Conversation

@victor-olamide

Copy link
Copy Markdown
Contributor

Summary

Implements the four Phase 0 foundation issues assigned to me in one branch:

  • [CLOB-004] Define canonical market identity and base/quote asset ordering #880 Canonical market identity. MarketId is sha256(domain || version || xdr(lo) || xdr(hi)) over the unordered asset pair. Reversed pairs derive the same id, and base == quote is rejected. Base/quote orientation stays in the market definition. (libs/clob_settlement_core/src/market_id.rs)
  • [CLOB-005] Introduce typed wrappers for atoms, lots, ticks, and sequence numbers #881 Typed units. Newtypes for base/quote atoms (i128, non-negative), base/quote lots, ticks and sequence numbers (u64). No From impls between units, checked arithmetic only, and explicit atoms-to-lots conversions with dust handling. The module documents the width and overflow analysis, including an 18-decimal cap. (libs/clob_settlement_core/src/units.rs)
  • [CLOB-006] Implement checked tick/lot to quote conversions with documented rounding #882 Checked tick and lot to quote conversions. scaled_price = tick * tick_size (exact), then quote = base * scaled_price / 10^8. Every division takes an explicit Rounding (Up for trader payments in, Down for protocol payments out). No floating point. (libs/clob_settlement_core/src/conversion.rs)
  • [CLOB-009] Specify the canonical event set and identifier fields #885 Canonical event set. Deposit, withdrawal, order accepted/rested/reduced/canceled/expired, fill, fee accrued/withdrawn, market status changed, and config changed. Each record carries MarketId and a gap-free per-market EventIndex. Orders are identified by their acceptance SequenceNumber. Topic names are stable. (libs/clob_settlement_core/src/events.rs)
  • Spec. docs/clob-foundations-spec.md documents the id preimage, unit rules, price model, rounding policy and event catalogue, so SDK and indexer code can reproduce them.

One commit per issue, plus the spec commit. Each commit is linked to its issue.

Verification

  • Not compiled or executed. I did not run any build, cargo, or npm command. The repo has no Cargo.toml, and the AGENTS.md verification gate that the issues reference is not in this repository. The unit tests are written but have not been run.
  • I checked the soroban-sdk 28 APIs used here (Address: Ord, ToXdr, Crypto::sha256, Bytes::from_slice/from_array, BytesN::to_array, register_stellar_asset_contract_v2) by reading the crate source in the local cargo registry.
  • Tests cover: identical and reversed pairs, identical-pair rejection, SAC and account addresses, negative and max atoms, u64::MAX lot boundaries, one-unit dust on both sides of a lot, rounding bracket checks over a small grid, overflow to None, topic stability, and rebuilding an order's lifecycle from events alone.

Notes for reviewers

  • lib.rs already has use soroban_sdk::{Address, Env, i128}; and uses Vec without importing it. Those lines predate this PR and I left them alone. They may need fixing before the crate compiles.
  • Placeholder enum variants to confirm before SDK codegen: MarketStatus (Open, Paused, Closed) and ConfigField (TickSize, LotSize, FeeRate).

Closes #880
Closes #881
Closes #882
Closes #885

Derive MarketId from the unordered asset pair and a version, hashed
over a domain separator, the version, and the XDR of the lower and
higher addresses. Reversed pairs map to the same id, identical base and
quote are rejected, and base/quote orientation stays in the market
definition.

Closes SO4-Markets#880
Introduce newtypes for base and quote atoms (i128, non-negative), base
and quote lots, ticks, and sequence numbers (u64). No From impls exist
between units, arithmetic is checked, and conversions between atoms and
lots are explicit. Document the width and overflow analysis, including
the 18-decimal cap, and cover max values and one-unit dust cases.

Closes SO4-Markets#881
Price a tick as tick * tick_size in quote atoms per base atom, scaled
by 10^8. Expose scaled_price, quote_for_base_atoms, and quote_for_lots.
Every division takes an explicit Rounding: Up for amounts the trader
pays in, Down for amounts the protocol pays out. Lot-to-atom conversion
is exact, so rounding happens only at the price step. No floating point
is used, and all arithmetic is checked.

Closes SO4-Markets#882
Define the market event schema: deposit, withdrawal, order accepted,
rested, reduced, canceled, expired, fill, fee accrued, fee withdrawn,
market status changed, and config changed. Every record carries its
MarketId and a strictly increasing, gap-free EventIndex, and orders are
identified by the SequenceNumber assigned at acceptance. Topic names
are stable. Tests cover topic stability, field values, index ordering,
and rebuilding order lifecycle from events alone.

Closes SO4-Markets#885
…ents

Document the market id preimage, unit widths and overflow rules, the
tick-to-quote price model with rounding policy, and the event topic
catalogue, so SDK and indexer code can reproduce the consensus-critical
definitions.
@drips-wave

drips-wave Bot commented Oct 20, 2026

Copy link
Copy Markdown

@victor-olamide Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

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

Labels

None yet

Projects

None yet

2 participants