Skip to content

feat: batch-auction DEX contract - #169

Open
SIDDHANTCOOKIE wants to merge 1 commit into
feature/generalized-tx-cli-docsfrom
feature/dex-batch-auction
Open

SIDDHANTCOOKIE wants to merge 1 commit into
feature/generalized-tx-cli-docsfrom
feature/dex-batch-auction

Conversation

@SIDDHANTCOOKIE

Copy link
Copy Markdown
Member

Summary

Phase 3, PR 1 (stacked on #168). This adds a DEX as a Python contract, using a batch auction instead of an AMM or a continuous order book. Orders collect in a batch, and clear settles every crossing order at one uniform price. Everyone in the batch trades at that same price, so the order in which orders arrived, or were placed in a block, doesn't change what anyone gets.

There are no consensus changes. It uses the existing multi-asset msg['value'] and transfer_out.

Contract (examples/batch_auction.py)

  • init:<base_asset>:<quote_asset> sets up one market, once. The native coin is "".
  • buy:<amount>:<price> requires sending exactly amount × price of the quote asset. sell:<amount>:<price> requires sending exactly amount of the base asset. Prices are integers: quote units per one base unit.
  • cancel:<order_id> is owner only, and refunds the escrow.
  • clear can be called by anyone:
    • sort buys by price descending and sells ascending, oldest first on ties
    • try each order price as a candidate, keeping demand and supply as running sums, so this is O(k log k) for k orders
    • pick the price that trades the most base; ties go to the smaller demand/supply gap, then to the lower price
    • fill by price priority and then age, with the marginal order partly filled
    • pay buyers the filled base plus any unused quote, and pay sellers filled × price quote plus any unsold base
    • unmatched orders are fully refunded, and the book is emptied
    • base and quote are conserved by construction, and the contract holds nothing after a clear
  • MAX_ORDERS = 100 per batch keeps every call's gas bounded.

Sandbox (minichain/contract.py)

  • Adds int and sorted to the allowed builtins. Both are pure and add nothing new: range/sum/list already run in C without gas metering.
  • The sandbox still rejects *, ** and @, so the contract multiplies integers with a small doubling helper (O(log n) additions).

Measurements

A full batch of 100 random orders:

  • each order call used 2,257–92,629 gas (it grows with batch size, because each call is charged for the contract's whole storage)
  • the 101st order was refused
  • clear used 46,137 gas and left a zero balance in the contract

Known limits

  • Whoever calls clear chooses when the batch ends, and can add their own orders before calling it. Uniform pricing limits what that can gain, but a fixed batch schedule needs block height in msg, which would be a separate change.
  • One market per deployed contract.

Test Plan

  • Added tests/test_batch_auction.py (12 tests, all run end to end through State.apply_transaction):
    • a crossing book clears at one price with the correct fills and refunds
    • a book that doesn't cross refunds everyone
    • the marginal order is partly filled
    • cancel works for the owner only
    • 5 kinds of bad orders are rejected and the funds stay put
    • a second init is rejected
    • the order cap is enforced
    • random books conserve both assets
  • Full suite: 434 passed.

examples/batch_auction.py runs one market as a contract: orders escrow
their funds, and clear settles every crossing order at a single uniform
price (the one that trades the most), filling by price then age and
refunding the rest. Orders per batch are capped so every call has bounded
gas. The sandbox now allows int and sorted.
@coderabbitai

coderabbitai Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

🗂️ Base branches to auto review: 2
  • main
  • develop

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration
  • Configuration used: Repository: StabilityNexus/MiniChain/.coderabbit.yaml
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 09de847c-3dcd-4090-ae58-ccec11c9e5bc

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

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.

1 participant