API v0.1 · pre-release
Quickstart
Receive Guard’s stream, check every message and print the state and signals. About fifteen minutes.
Before you start
From us, after the pilot terms are agreed:
- the stream address (
wss://…/v1/stream) and your access details; - the signer address to pin in your code.
From you: a staleness limit, the maximum age of a message you will act on. The examples use 2 seconds; choose your own.
Python
Install
pip install websockets rfc8785 eth-account
guard_client.py
import asyncio, json, time
import rfc8785, websockets
from eth_account import Account
from eth_account.messages import encode_defunct
STREAM_URL = "wss://..." # sent to you with the pilot terms
GUARD_SIGNER = "0x..." # the signer address we confirm to you
MAX_AGE_MS = 2_000 # your staleness limit
last_seq = {}
def accept(msg: dict) -> bool:
body = rfc8785.dumps({k: v for k, v in msg.items() if k != "sig"})
if Account.recover_message(encode_defunct(body), signature=msg["sig"]) != GUARD_SIGNER:
return False # not signed by Guard
if msg["seq"] <= last_seq.get(msg["asset"], -1):
return False # old or repeated
if time.time() * 1000 - msg["t"] > MAX_AGE_MS:
return False # stale
last_seq[msg["asset"]] = msg["seq"]
return True
async def main():
async with websockets.connect(STREAM_URL) as ws:
async for frame in ws:
msg = json.loads(frame)
if not accept(msg):
continue
g = msg["guard"]
print(msg["asset"], msg["state"], msg["price"], (msg["lo"], msg["hi"]),
"auction" if g["auction"] else "", "pause" if g["pause"] else "")
asyncio.run(main())
JavaScript
Install (Node.js 22 or later)
npm install ethers canonicalize
guard-client.mjs
import canonicalize from "canonicalize";
import { verifyMessage } from "ethers";
const STREAM_URL = "wss://..."; // sent to you with the pilot terms
const GUARD_SIGNER = "0x..."; // the signer address we confirm to you
const MAX_AGE_MS = 2000; // your staleness limit
const lastSeq = {};
function accept(msg) {
const { sig, ...body } = msg;
if (verifyMessage(canonicalize(body), sig) !== GUARD_SIGNER) return false;
if (msg.seq <= (lastSeq[msg.asset] ?? -1)) return false;
if (Date.now() - msg.t > MAX_AGE_MS) return false;
lastSeq[msg.asset] = msg.seq;
return true;
}
const ws = new WebSocket(STREAM_URL);
ws.onmessage = (ev) => {
const msg = JSON.parse(ev.data);
if (!accept(msg)) return;
console.log(msg.asset, msg.state, msg.price, [msg.lo, msg.hi], msg.guard);
};
What you should see
One line per asset every 500 ms. While the underlying market is closed, state is CLOSED and the band is wider. At a reopening, state turns REOPENING for 15 minutes, and guard.auction is true on the first message of the reopening.
Next steps
- Decide what each state and signal does on your platform: Integration by venue type.
- Handle reconnects and stale data: Stale data and fallbacks.
- Verify on-chain: Verifying signatures.