For the complete documentation index, see llms.txt. This page is also available as Markdown.

Medusa Services

Medusa is an off-chain coordination layer that serves as the primary interface between market makers (solvers) and Khalani's liquidity focused coordination market.

1. Overview

Core Functions

Function
Description

Intent Broadcasting

Receives intents from users and broadcasts to connected market makers

Solution Relay

Accepts solutions from market makers and posts them on-chain

High-Performance Cache

Mirrors the on-chain IntentBook for low-latency access

RFQ Service

Enables request-for-quote flows for price discovery

Metrics & Monitoring

Exposes Prometheus metrics at /metrics

Communication Channels

Channel
Port
Purpose

JSON-RPC HTTP

8001

Client operations (submit intents, query status)

WebSocket

8002

Market maker operations (receive intents, submit solutions)

Prometheus

8200

Metrics scraping

2. Getting Started

Prerequisites

  1. Private Key — Ethereum-compatible key for signing solutions

  2. Medusa Endpoint — WebSocket URL for your environment

  3. Chain Configuration — Chain ID and IntentBook contract address

Connection Flow

  1. Connect to WebSocket endpoint (ws://host:8002)

  2. Send AddSolver message with signed authentication payload (required first message)

  3. Request open intents via RequestOpenIntents

  4. Listen for new intents and submit solutions

EIP-712 Domain Configuration

All signatures use this domain:


3. WebSocket API

Endpoint: ws://{host}:8002

Outgoing Messages (Solver → Medusa)

Message
Description

AddSolver

Required first message. Authenticate with signed payload

RequestOpenIntents

Request all currently open intents

ProposeSolution

Submit a signed solution to settle intents

IntentRefinement

Respond to a refinement request with a quote

GetSolutionsForIntent

Query solutions for a specific intent

GetSolutionsForSolver

Query your submitted solutions

AddSolver

ProposeSolution

IntentRefinement

Incoming Messages (Medusa → Solver)

Message
Description

ExistingOpenIntents

Response to RequestOpenIntents

NewIntent

Broadcast when a new intent is published

IntentStatusUpdated

Broadcast when an intent's state changes

IntentsSolved

Broadcast when intents are successfully solved

SolutionRejected

Sent when your solution fails on-chain

RefinementNeededForIntent

Request for price quote

Solutions

Response to solutions query

Intent States

State
Meaning

NonExistent

Intent has never existed or was purged

Open

Intent is active and available for matching

Solved

Intent has been successfully settled

Cancelled

Intent was cancelled by the author

Expired

Intent validity period has passed

Error

Intent encountered an error during processing


4. JSON-RPC API

Endpoint: POST http://{host}:8001

Intent Operations

Method
Description

proposeIntent

Publish a signed intent → returns [txHash, intentId]

getIntent

Get intent by ID

getIntentStatus

Get intent state

getHistory

Get full intent history

cancelIntent

Cancel an open intent

getNonce

Get next nonce for address

getIntentIdsByAuthor

Get all intent IDs by author

getActiveIntentsByAuthor

Get active intents by author

getLiquidityIntentsByAuthor

Get liquidity intents by author

Solution Operations

Method
Description

getSolution

Get solution that solved an intent

getSolutionsForSolver

Get all solutions by solver address

getConnectedSolvers

List connected solver addresses

Refinement (RFQ) Operations

Method
Description

createRefinement

Request quotes from solvers → returns intentId

queryRefinement

Get quote response

Example Request

5. Trading Strategies

Simple Swap Filling

Match user swap intents against your inventory. If you can source the output token cheaper than the user's minimum, fill the order and keep the difference.

Fee-Based Liquidity Provision

Publish liquidity intents to earn fees on swaps. With PercentageFilled, mAmounts represents fees in basis points (0–1000 = 0%–100%).

Two-Way Market Making

Publish opposing liquidity intents for both directions to capture bid-ask spreads.

Just-In-Time (JIT) Liquidity

  1. Receive NewIntent for a user swap

  2. Create a matching liquidity intent

  3. Submit both in a single solution with JIT intents array populated

6. Building Solutions

Solution Structure

Field
Description

intentIds

Array of intent hashes being settled

intentOutputs

Outputs that go to other intents (multi-hop)

receiptOutputs

Final outputs that create receipts for users

spendGraph

How tokens flow from intents to outputs

fillGraph

Which outputs satisfy which intents

Spend Graph Entry

Fill Graph Entry

Example: Two-Intent Match

User A: 1000 TokenX → 950 TokenY User B: 1000 TokenY → 950 TokenX


7. Best Practices

Connection Management

  • Implement ping/pong handling to keep WebSocket connections alive

  • Use exponential backoff for reconnection (start at 1s, max 60s)

  • Colocate with Medusa when possible for lowest latency

State Tracking

  • Remove intents in terminal states: Solved, Cancelled, Expired

  • Periodically call RequestOpenIntents to resync state

  • Track intents by ID in a Set/Map to prevent duplicates

Validation

  • Check validAfter and validBefore timestamps with 60s buffer

  • Verify you support the intent's tokens before attempting match

  • Validate solution structure before submission

Performance

  • Pre-compute common routes

  • Maintain in-memory order books

  • Batch database writes

  • Use efficient data structures (graphs, heaps)


8. Troubleshooting

Connection Issues

Problem
Cause
Solution

Connection refused

Medusa not running

Check http://localhost:8001/metrics

Immediate disconnect

Missing AddSolver

First message must be authenticated AddSolver

Auth failure

Wrong signature

Verify EIP-712 domain parameters

Solution Rejections

Reason
Solution

Intent already solved

Race condition — another solver was faster

Intent expired

Add time buffers, process faster

Invalid solution structure

Verify spendGraph/fillGraph consistency

Signature mismatch

Ensure solution signed by connected solver

Common Errors

Error
Fix

"First WS message is not AddSolver"

Send AddSolver as the very first message

"Nonce check failed"

Use unique nonces (timestamp + random)

"Signature check failed"

Verify key and EIP-712 domain

Please reach out to our team for solver onboarding and support.

Last updated