Rivisk

On-chain

Consuming Rivisk on-chain

How another Clarity contract reads a wallet's risk and acts on it. The two rules at the top of this page matter more than everything below them.

Two rules#

1. A reading has an age#

get-latest-risk will return a snapshot published hours ago, describing chain state older still. Acting on it is how a protocol liquidates someone on stale data. Use get-risk-if-fresh, which returns none instead of a stale reading. The check cannot be forgotten, because there is no way to get the value without passing the bound.

2. A debt-free wallet publishes the safest values, not zero#

With no debt, health-factor-e4 is the maximum uint and liquidation-distance-bps is u10000. So the natural check (>= health-factor u12500) treats that wallet as safe. Had zero been published, the same check would reject it as the riskiest position on the chain.

The integration pattern#

your-contract.clar
(use-trait risk-provider 'ST2F3J1PK46D6XVRBB9SQ66PY89P8G0EBDW5E05M7.risk-provider-trait.risk-provider-trait) (define-constant ERR_NO_FRESH_RISK (err u1))(define-constant ERR_UNHEALTHY (err u2)) (define-public (borrow-more (provider <risk-provider>) (amount uint))  (let (    (risk (unwrap!            (try! (contract-call? provider get-risk-if-fresh tx-sender u375))  ;; ~1 hour            ERR_NO_FRESH_RISK))  )    (asserts! (>= (get health-factor-e4 risk) u15000) ERR_UNHEALTHY)       ;; 1.50    ;; ... your logic. Keep (get report-hash risk) to make the decision auditable.    (ok true)  ))
  • Take the provider as an argument (<risk-provider>) rather than hardcoding the registry. The same code then works against a test double in your own suite, a future registry version, or any conforming provider, and your tests do not need Rivisk deployed.
  • Callers pass 'ST2F3J1PK46D6XVRBB9SQ66PY89P8G0EBDW5E05M7.risk-registry as the provider on testnet.
  • Record the report hash with the decision. It ties the decision to the exact report it was made from.

Choosing a freshness bound#

Freshness is counted in Stacks blocks, which arrive every few seconds, not every ten minutes. On testnet on 2026-09-21 we measured 9.6 seconds per block:

WindowBlocks at ~9.6 s
5 minutes~31
1 hour~375
1 day~9,000

A snapshot is only refreshed when its wallet is re-indexed. A tight window therefore needs frequent indexing (a scheduled refresh, or Chainhook triggers) to stay satisfiable. Too tight a bound and your contract refuses everyone.

The read-only limitation#

Clarity will not let a define-read-only function dispatch on a trait. The analyzer cannot prove a dynamically resolved callee is read-only, so it treats the whole function as writing. You will hit this as soon as you try to build a “would this be allowed?” preview.

Split the two halves. get-risk-if-fresh on the registry is itself read-only, so a UI or your own read-only function can call the registry directly and apply thresholds with a pure helper:

clarity
(define-read-only (meets-policy (health-factor-e4 uint) (concentration-bps uint))  (and (>= health-factor-e4 u15000)       (<= concentration-bps u6000)))

Verifying a report#

Fetch the report from the API, canonicalise it (sort object keys recursively), serialise it as JSON, SHA-256 it, and compare with report-hash. A match proves the report is the one that was attested and has not been edited since.

What the registry does not tell you#

  • That the numbers are right. It attests what Rivisk computed and that it has not changed since. The trust model is “an authorised publisher said this at this block”, not “this is objectively true”.
  • That a protocol endorses Rivisk. protocol-registry records adapter support, nothing more.
  • Exact parity with a protocol's oracle. Rivisk marks to its own price source, so its liquidation distances differ from a protocol's own by the spread between the two.

Every function and error code is on Smart contracts.