-1) when they do not. You check it for free before paying for a round, then request one and read its outcome off-chain and on-chain. For how policies work, see Policies. For the source format itself, see Sources.
The snippets use @molpha/sdk 0.3.0 (ESM only, Node.js 20.19 or later). Steps 1 to 4 need no wallet and cost nothing.
1. Write the source
- Candidates. Every input is a candidate here. A candidate whose fetch or decode fails is disqualified (
onErrordefaults todisqualify). Addrulesto disqualify a candidate on its own data. For example, if an input also decoded atsfield (unix seconds),rules: [sx.lte(sx.sub(sx.ref("$ts"), sx.ref("kraken.ts")), sx.cst("5"))]would disqualify a quote older than 5 seconds. - Combine.
mediansigns the lower median of the qualifying values. Fewer than 2 qualifying venues is INCONCLUSIVE0x01; a spread above 100 bps is INCONCLUSIVE0x02. - Independence. With two or more candidates
defineSourcesetsdistinctHosts: the three URLs must have different host names. - Fallback.
-1is signed for an INCONCLUSIVE result. The assertion$out >= 0makes it impossible for a real price to encode to the same word. - Tolerance. Nodes read live prices at slightly different moments, so the source uses tolerance aggregation. In exact mode every signer would have to read byte-identical prices from all three venues.
defineSource validates the whole document and throws a SourceError naming the path of the first problem, before any request.
2. Analyze it
analyzeSource describes the source without fetching anything. fallbackWords lists the 32-byte words the fallback can sign:
-1 at 2 decimals is signed as -100 in two’s complement. Keep FALLBACK_WORDS: it is what you compare a verified value with.
Check two things before going further:
policy.fallback.reachableisfalse. If it istrue(warningfallbackReachable), a conclusive result can be the same word as the fallback and nobody can tell them apart. Add or tighten an assertion.- Each warning is expected. Here
fallbackTolerancesays that a signed fallback is still a value: written to a Solana feed, it replaces the feed’s last price. Step 7 shows how to avoid that.
analyze_source returns the same analysis plus the fallback words, locally and without a wallet.
3. Preview it against live data
POST /v1/source/preview fetches the inputs and evaluates the source the way a node would, twice, a second apart. Nothing is signed or charged.
stable: false means the second run gave another value or failed. For a live price that is normal; for an exact-mode source it means nodes would disagree. If the preview fails, data.error gives the stage, input and message, and data.inputs holds each response it fetched. Previews are rate limited per client address (HTTP 429 with Retry-After).
4. Simulate failures
A preview shows today’s data. To see what the policy does when a venue is down, stale or out of line, evaluate it over inputs you choose withsimulateSource. It calls POST /v1/source/simulate, which runs the nodes’ own evaluator. Nothing is fetched, signed or charged.
Give every input exactly one of a response body (decoded with the source’s own decode block), decoded fields, or an error (fetch or decode):
Pass
{ timestampMs } as the fourth argument to evaluate at a chosen round time (unix milliseconds on the 100 ms grid), which matters for rules that read $ts or $tsMs. A malformed request, such as a missing input, throws SimulateError with the HTTP status. With a MolphaSDK instance, sdk.gateway.simulateSource(source.doc, inputs) does the same on the client’s first gateway.
From an agent, the MCP tool simulate_source takes the same source and inputs and asks the configured gateway.
A good set of cases before you pay: all venues healthy, each venue down alone, all but one down, one venue far from the others, and a value that should trip each rule.
5. Request a round
A policy round is requested and priced like any other. The snippets from here usesdk, set up with a Solana devnet wallet that has a subscription (see the backend quickstart):
400 and is never retried:
signaturesRequiredmust be above the registry’s redundancy buffer (2 on the current devnet deployment, so any quorum of 3 or more).requireProvidercannot be set.
6. Read the outcome
Read the outcome from the signed value, never from a label:readOutcome returns null when the attestation’s sourceId is not this source’s, or its value is not one 32-byte word. Otherwise a value among fallbackWords(source) is fallback, and anything else is conclusive.
The SDK also fills result.outcome the same way, from the signed value. result.reason is the reason code the nodes reported. It is not signed: show it, log it, but do not settle anything on it.
See why
result.roundId is the round’s ID. The round archive serves the round and each node’s receipt under it, free and without a key:
bodyHash for an http body), which candidates qualified and why not, and the outcome the node reached. Receipts are unsigned and arrive a few seconds after the round. Private and paid-source rounds have none.
7. Consume it on-chain
A verifier checks the signature, not the outcome. Your consumer must compare the verified value with the fallback words.EVM and Starknet
Hard-code the fallback words of your pinned source, and branch on them after verification:$reason or $n has several words: check against all of them. On Starknet, compare the verified value with the same words. See EVM verifier and Starknet verifier for the rest of the consumer duties.
If the outcome settles something (a market, a payout), also close the door on outcome shopping: someone can request rounds until one gives the outcome they want. Accept only attestations whose timestamp / 1000 falls inside your resolution window, and only the first one you accept.
Solana
The program writes any verified value to the submitter’s feed, so a submitted fallback replaces the feed’s last price. Feeds are keyed by submitter, so only your own submits reach your feed. Check the outcome before you submit:sdk.requestAndSubmit for such a source: it submits whatever the round returned. A program that reads another submitter’s feed should compare the feed’s value with the fallback words as well.
A continuously updated price feed is usually better served by a policy with no fallback: an INCONCLUSIVE round is then refused, and the feed keeps its last good value. Keep a fallback for consumers that must act on “no answer”, such as settlement and verdict sources.
Checklist
analyzeSource(source).policy.fallback.reachableisfalse, and every warning is understood.- Simulated: all venues healthy, each venue down, all but one down, a divergent venue.
signaturesRequiredis above the redundancy buffer if the source has a fallback;maxRetriesis low if it has none.- The outcome is read from the signed value (
readOutcome, or a comparison with the fallback words on-chain), never fromreasonor the archive’sresult. - Fallbacks are not written to Solana feeds that consumers read as prices.
Policies
Combinators, reason codes, signing modes and receipts.
Sources
Inputs, decoders, transforms and outputs.
MCP tools
analyze_source and simulate_source for agents.