The feed

Calc scripts & the guard band

The pricing formula and the on-chain price band are not hard-coded — they are sandboxed scripts, stored in Redis and hot-reloaded within seconds. Edit and backtest them in the console.

Two engines, one contract

The Rust feed runs a sandboxed Rhai engine; the TypeScript twin runs a QuickJS sandbox. Both are op-count limited, side-effect free, and return a map. Scripts are Rhai in the Rust stack and JavaScript in the twin — the same inputs and output shape.

Selectors

A script is addressed by source:symbol:chain with * wildcards; the empty / *:*:* selector is the global default. The chain component is the decimal chain id (e.g. 8453). Resolution walks a fixed fallback order, most specific first:

source:symbol:chain  →  *:symbol:chain  →  *:symbol:*  →  *:*:chain  →  *:*:*
A source:symbol:* override never resolves — the candidate list only ever emits a source-specific selector with a real chain. Use source:symbol:<chain>.

Pricing scripts

A pricing script reads the tick and window and returns a map. It can set the confidence, the bid/ask half-spreads (as codebook indices), and even override the price or pack the whole slot word.

// inputs: price, spread, sigma, prices[], bid_qty, ask_qty,
//         samples, window, source, symbol, chain, exponent
let vol = realized_vol(prices);
#{ confidence: price * vol, spread0: codebook(3), spread1: codebook(3) }

Rhai shown; the TypeScript twin is the same expression in JavaScript.

Script reference

Every script — pricing or guard — is a single expression that ends in a map. Rhai uses #{ … } and intermediate let …; bindings; the JS twin uses { … } wrapped in an IIFE for statements.

Inputs (read-only)

price · spread · sigma · prices[] · bid_qty · ask_qty · samples · window · warmup · exponent · source · symbol · chain.

Library helpers

sma · ema · rma · wma · stdev · rsi · roc · realized_vol · ewma_vol · mean · depth_imb · micro_price · bps_of · feed_price.

Native builtins

clampf · fmin · fmax · pow · abs · is_finite · codebook · u64x32 · pack · feed · log.

Pricing output map

Confidence only, plus optional spreads or a fully packed slot word:

#{ confidence: … }                                   // minimum
#{ confidence: …, spread0: codebook(u), spread1: codebook(u) }
#{ packed: pack(u64x32(price), spread0, spread1) }   // script owns the whole slot

Spread bytes are codebook indices — produce them with codebook(units). A script may also override price (components mode only).

Guard output map

#{ min: real_low, max: real_high }   // REAL price units, 0 <= min < max

Per-chain pricing

Confidence is computed for each configured chain. Only a full packed word is carried per chain as packed:{chain} — a chain-specific script shapes only that chain's push, while the pusher prefers packed:{chain} over the base word. Per-chain component fields are not on the wire, so use packed for a per-chain effect.

The guard band

A guard script is a second family (calc:guard) with the same selector rules. It returns a min/max band in real price units; the feed converts to mantissa and publishes guard_min:{chain} / guard_max:{chain}. The compressed pusher's guard service tracks that band on-chain — see Pushers.

let real = price * pow(10, exponent);
#{ min: real * 0.9, max: real * 1.1 }   // ±10% envelope, real units

Backtest

The console replays a feed's recorded history through the real engine — never touching the live bus — so you see the exact confidence a script would have produced. A pricing | guard toggle switches families; guard replay draws the min/max envelope over the price. The shared script library and the repo baseline (for reset) live on the bus alongside the scripts.