Skip to content

M2 #24: Crank service: Monitor and execute matching#64

Merged
joaquinbejar merged 1 commit into
mainfrom
M2/issue-24-crank-service
Feb 1, 2026
Merged

M2 #24: Crank service: Monitor and execute matching#64
joaquinbejar merged 1 commit into
mainfrom
M2/issue-24-crank-service

Conversation

@joaquinbejar

Copy link
Copy Markdown
Owner

Summary

Implements the crank service that monitors order books for crossing orders and submits MatchOrders transactions. This is a permissionless service that earns priority fees for executing matches.

Changes

New Crate: crank/

File Description
Cargo.toml Crank crate dependencies
src/lib.rs Crate root with module exports
src/config.rs CrankConfig with priority fees, poll interval, retries
src/detector.rs CrossDetector for detecting bid >= ask conditions
src/builder.rs TransactionBuilder for MatchOrders/ConsumeEvents
src/submitter.rs TransactionSubmitter with retries and backoff
src/service.rs CrankService orchestrating the full workflow
src/metrics.rs CrankMetrics for monitoring

Cross Detection

  • Monitor best bid/ask from BookBuilder
  • Detect crossing condition (bid >= ask)
  • Estimate number of matches possible
  • CrossInfo struct with market, prices, estimated matches

Transaction Building

  • Build MatchOrders instruction with configurable limit
  • Build ConsumeEvents instruction for settlement
  • Build bundled transactions when possible
  • Estimate compute units per transaction
  • BuiltTransaction struct with data, priority fee, compute units

Priority Fee Management

  • Dynamic fee calculation based on estimated matches
  • Configurable min/max priority fee range (default 1000-100000 lamports)
  • Profitability check (fees earned > tx cost + threshold)

Transaction Submission

  • Send with configurable retries (default 3)
  • Exponential backoff on failures
  • Confirmation tracking
  • SubmitResult enum: Confirmed, Failed, Dropped

CrankMetrics Tracking

  • Matches executed
  • Transactions submitted/success/failed/dropped
  • Fees paid
  • Crosses detected
  • Poll cycles
  • Success rate and matches/sec

CrankService

  • Configurable poll interval (default 100ms)
  • Multiple markets support
  • Graceful stop mechanism
  • run() for continuous operation
  • poll_once() for single cycle execution

Technical Decisions

  • Placeholder transaction serialization: Real implementation will use Anchor/Solana SDK
  • Placeholder RPC submission: Real implementation will use solana-client
  • Profitability estimation: Simple fee calculation, can be enhanced with actual fee schedules
  • Backoff strategy: Exponential with configurable multiplier and max

Testing

  • 50+ unit tests for all components
  • Config validation tests
  • CrossInfo tests (spread, is_crossed)
  • TransactionBuilder tests (match_orders, consume_events, bundled)
  • SubmitResult tests
  • Submitter backoff calculation tests
  • CrankMetrics tests (recording, snapshot, reset)
  • CrankService tests (new, metrics, stop, poll_once)

Checklist

  • Code follows .internalDoc/09-rust-guidelines.md
  • Documentation added for all public items
  • No warnings from cargo clippy
  • cargo fmt passes
  • All tests pass

Closes #24

- Create new crank crate with core components:
  - config.rs: CrankConfig with priority fees, poll interval, retries
  - detector.rs: CrossDetector for detecting bid >= ask conditions
  - builder.rs: TransactionBuilder for MatchOrders/ConsumeEvents
  - submitter.rs: TransactionSubmitter with retries and backoff
  - service.rs: CrankService orchestrating the full workflow
  - metrics.rs: CrankMetrics for monitoring
- Cross detection:
  - Monitor best bid/ask from BookBuilder
  - Detect crossing condition (bid >= ask)
  - Estimate number of matches possible
- Transaction building:
  - Build MatchOrders instruction with configurable limit
  - Build ConsumeEvents instruction for settlement
  - Build bundled transactions when possible
  - Estimate compute units per transaction
- Priority fee management:
  - Dynamic fee calculation based on estimated matches
  - Configurable min/max priority fee range
  - Profitability check (fees earned > tx cost)
- Transaction submission:
  - Send with configurable retries
  - Exponential backoff on failures
  - Confirmation tracking
  - SubmitResult enum (Confirmed/Failed/Dropped)
- CrankMetrics tracking:
  - Matches executed
  - Transactions submitted/success/failed/dropped
  - Fees paid
  - Crosses detected
  - Poll cycles
  - Success rate and matches/sec
- CrankService:
  - Configurable poll interval
  - Multiple markets support
  - Graceful stop mechanism
  - poll_once() for single cycle execution
- 50+ unit tests for all components
@joaquinbejar joaquinbejar added this to the M2: Off-Chain Services milestone Feb 1, 2026
@joaquinbejar joaquinbejar merged commit 773839c into main Feb 1, 2026
5 checks passed
@joaquinbejar joaquinbejar deleted the M2/issue-24-crank-service branch February 2, 2026 09:55
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.

Crank service: Monitor and execute matching

1 participant