Skip to content

Latest commit

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..

README.md

sluice-client (Python)

Minimal Python client for the Sluice matcher API, on-chain event subscriptions for Casper.

pip install sluice-client
# optional, only if you need the live WebSocket stream:
pip install 'sluice-client[stream]'

Mirrors the @sluice/client TypeScript surface so the mental model is portable across languages.

Five-minute tour

fromsluice_clientimportSluiceClientsluice=SluiceClient() # https://sluice.unitynodes.com/api# 1. inspect subssubs=sluice.subs.list()
print(f"{len(subs)} subs, {sum(1forsinsubsifs['active'])} active")
# 2. dry-run a predicate before paying CSPRprobe=sluice.predicate.validate({"and": [
{"field": "amount", "op": "gte", "value": "100000000000000"}
]})
print(f"would match {probe['matches']}/{probe['total_scanned']} (~{probe['estimated_per_day']}/day)")
# 3. why didn't this match?, per-condition tracewhy=sluice.predicate.explain(predicate, event)
forstepinwhy["trace"]:
print("✓"ifstep["pass"] else"✗", step["reason"])
# 4. fire synthetic webhooks at your receiver, no CSPR spentsluice.sandbox.dispatch("https://my.app/webhook", count=5)
# 5. live stream of every delivery the matcher fans out (requires `pip install websockets`)forenvinsluice.stream.subscribe(sub_id=42):
print(env["type"], env["data"])

Receiver-side HMAC verify

fromsluice_clientimportverify_hmac_signatureifnotverify_hmac_signature(raw_body, request.headers["X-Sluice-Signature"], SECRET):
returnResponse(status=401)

Works in FastAPI / Flask / Django, wherever you have access to the raw bytes (FastAPI: raw = await request.body(); Flask: request.get_data(cache=True, as_text=False)).

Self-host

sluice=SluiceClient(base_url="http://localhost:8080/api")

Reference

NamespaceMethod
sluicehealth(), snapshot(), metrics_text(), badge_url(metric), openapi_url()
sluice.subslist(), get(id), replay_last(id, n=10), ics(id), og(id)
sluice.predicatevalidate(predicate), explain(predicate, event)
sluice.sandboxdispatch(webhook, predicate=None, count=3)
sluice.chainhead()
sluice.streamsubscribe(sub_id=None), yields envelope dicts
sluice.txtest_webhook(sub_id), replay(event_hash)

Errors raise SluiceApiError with .status and .body attributes.

License

MIT