Skip to content

feat: read a market's details from a create_market transaction#165

Merged
MicBun merged 1 commit into
mainfrom
feat/decode-create-market-tx
Jul 9, 2026
Merged

feat: read a market's details from a create_market transaction#165
MicBun merged 1 commit into
mainfrom
feat/decode-create-market-tx

Conversation

@MicBun

@MicBun MicBun commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

resolves: https://github.com/trufnetwork/trufscan/issues/173

A block explorer reading a create_market transaction gets back its five raw action arguments, one of which (query_components) is a nested double-encoding — an ABI tuple wrapping a kwil-native argument blob. Decoding that meant knowing create_market's argument order and stitching two decoders together by hand. The SDK owns the encode side (OrderbookAction.createMarket, encodeQueryComponents), so it should own the symmetric decode.

Adds decodeCreateMarketPayload(payload), the transaction-level counterpart to createMarket. Given a decoded execute payload (from TransactionAction.getTransactionInput / decodeTransactionPayload) it returns a structured CreateMarketPayload:

const payload = await client.loadTransactionAction().getTransactionInput({ txId });
const created = decodeCreateMarketPayload(payload);
if (created) {
  // { bridge, queryComponents, market: { type, thresholds, streamId, dataProvider },
  //   settleTime, maxSpread, minOrderSize }
}
  • Returns null when the transaction isn't a create_market call, so callers branch cleanly.
  • Throws when it is a create_market but doesn't carry its five arguments.
  • The nested query_components are decoded to MarketData (type + thresholds) via the existing decodeMarketData. Decode is non-fatal: a committed-but-execution-failed create_market can carry empty/garbage components, so market is left null rather than crashing an explorer that's iterating a block.

The argument order ($bridge, $query_components, $settle_time, $max_spread, $min_order_size) matches the on-chain create_market signature in node migration 032-order-book-actions.sql.

Test-first: four cases watched fail before the code existed — structured fields, the nested market decode, the null-when-not-create_market path, and the throw-on-truncated path. Also verified against the exact query_components byte vector from trufscan#173. test:unit (292), typecheck, and build are green.

Summary by CodeRabbit

  • New Features

    • Added support for decoding create_market transaction details into a structured, easy-to-use format.
    • Exposed additional market-related fields, including bridge, settlement timing, spread, minimum order size, and nested market data when available.
  • Bug Fixes

    • Improved handling for malformed market details by preserving the main payload and safely returning no nested market data when decoding fails.
    • Returns no result for unrelated transaction actions and validates required inputs more strictly.
  • Tests

    • Added coverage for successful decoding, missing fields, invalid actions, and fallback behavior.

@MicBun MicBun self-assigned this Jul 9, 2026
@holdex

holdex Bot commented Jul 9, 2026

Copy link
Copy Markdown

Time Submission Status

Member # Time Running Total Status Last Update
MicBun 4h ✅ Submitted Jul 9, 2026, 3:38 PM

Submit or update total time with:

@holdex pr submit-time 2h

Add time on top of previous submission with:

@holdex pr add-time 1h30m

See available commands to help comply with our Guidelines.

@coderabbitai

coderabbitai Bot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Adds a new CreateMarketPayload interface and decodeCreateMarketPayload function in orderbookHelpers.ts that decode create_market transaction payloads, including nested market data via decodeMarketData. Re-exports these from internal.ts and adds corresponding test coverage.

Changes

decodeCreateMarketPayload decoder

Layer / File(s) Summary
Decoder implementation
src/util/orderbookHelpers.ts
Adds CreateMarketPayload interface and decodeCreateMarketPayload function that validates the create_market action, checks argument count (throwing if insufficient), converts query_components to bytes and decodes nested market data (setting market to null on decode failure), and maps remaining arguments to typed fields.
Public export
src/internal.ts
Re-exports decodeCreateMarketPayload and expands the type re-export to include CreateMarketPayload alongside MarketData.
Test coverage
src/util/orderbookHelpers.test.ts
Adds tests for decoding top-level and nested market fields, handling undecodable query_components, returning null for non-create_market actions, and throwing on missing arguments.

Estimated code review effort: 2 (Simple) | ~10 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Caller
  participant decodeCreateMarketPayload
  participant decodeMarketData

  Caller->>decodeCreateMarketPayload: payload (DecodedTransactionPayload)
  decodeCreateMarketPayload->>decodeCreateMarketPayload: check action === "create_market"
  decodeCreateMarketPayload->>decodeCreateMarketPayload: validate call arguments length
  decodeCreateMarketPayload->>decodeMarketData: query_components bytes
  decodeMarketData-->>decodeCreateMarketPayload: market or decode error
  decodeCreateMarketPayload-->>Caller: CreateMarketPayload or null
Loading

Possibly related PRs

  • trufnetwork/sdk-js#149: Both PRs extend the orderbook helpers module and its public exports via src/internal.ts.
  • trufnetwork/sdk-js#153: Both PRs update src/internal.ts to re-export additional decoding utilities from ./util/orderbookHelpers.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: decoding market details from a create_market transaction.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/decode-create-market-tx

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.

@MicBun

MicBun commented Jul 9, 2026

Copy link
Copy Markdown
Contributor Author

@holdex pr submit-time 4h

@MicBun
MicBun merged commit 4aae8b4 into main Jul 9, 2026
6 checks passed
@MicBun
MicBun deleted the feat/decode-create-market-tx branch July 9, 2026 15:39
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