clarity-oracles
Skill2 starsChanged 31 days agoMITView on GitHub
Read prices in Clarity: Pyth Lazer signed feeds, DIA, staleness and confidence checks, test fixtures, migrating from Pyth Core. Use for any price feed or oracle.
In this repository24 other files it gives its agents.
- AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- llms.txtllms.txt
- clarity-apisSkill
- clarity-assetsSkill
- clarity-auditorSkill
- clarity-bitcoin-proofsSkill
- clarity-deploymentSkill
- clarity-ecosystemSkill
- clarity-environmentsSkill
- clarity-epochsSkill
- clarity-fuzzingSkill
- clarity-governanceSkill
- clarity-optimizerSkill
- clarity-protocolsSkill
- clarity-scholarSkill
- clarity-securitySkill
- clarity-sip-complianceSkill
- clarity-stakingSkill
- clarity-startSkill
- clarity-system-integrationSkill
- clarity-testingSkill
- clarity-upgradesSkill
- clarity-walletsSkill
The file
We split this skill into the rules it gives an agent
10 rules. About 0 are common, 5 are distinctive, and 5 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈5
Distinctive · estimate
This author's own choices. Worth a look.
5
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 34
Verify signer rotation/expiry, fees charged to tx-sender, pause state and decoder governance at the target deployment.
- Very specific: line 38
After verify-price-feeds succeeds, require all of these for the feed you use:
- Distinctive: line 101
If the consumer caches prices, recheck freshness when reading them.
- Very specific: line 121
Trust Pyth's signer: Every verification fails with u2105 until you call (set-trusted-signers (list {pubkey: …
- Very specific: line 131
Before sending a transaction, check a fetched update with the decoder's read-only recover-signer and decode-lazer-payload.
- Distinctive: line 136
Verify payload bounds, signature recovery, field counts, endianness and trailing bytes.
- Distinctive: line 137
Check envelope and feed age, future timestamps and exact threshold boundaries: stale feeds in fresh envelopes, milliseconds mistaken for …
- Distinctive: line 139
Test the actual verifier/parser (Clarigen by default, or the project's harness), plus authentic signed vectors and isolated test signers …
- Distinctive: line 146
Start with golden fixture tests, [synthetic
- Very specific: line 151
Use clarity-testing for Clarigen setup.
≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Clarity Oracles
Use Stacks Pyth Lazer (Pyth Pro) for new Pyth integrations, and read a price only from a signed update that a Lazer instance verifies, with the checks below. Call the instance's real verify-price-feeds; do not invent a Pyth trait, a stored-price getter or other functions the verifier lacks. Your own adapter over the verified result is fine if it keeps every check below. Pyth Core no longer supports Stacks, as Pyth's unsupported-chains page states; prices stored in the Stacks Core contracts stopped updating. Keep the old Core/Wormhole bridge and its examples for legacy audits and migration. Upgraded Core remains available on other chains; its automatic-upgrade guidance does not establish Stacks compatibility.
Writing or changing contract code: also load clarity-scholar and clarity-security, and finish with the build checklist.
Key distinctions
- Lazer accepts different payloads from Core/Wormhole VAAs. Do not send Core VAAs/PNAU bundles to Lazer or reuse old feed identifiers without remapping.
- The current Stacks implementation returns verified feeds within the transaction and persists none, so a Lazer instance has no price to read without a signed update. Pyth Core's storage stopped updating (BTC/USD last published 2026-08-21), yet its
get-pricestill returns the stale value without an error. DIA's pushed keys are live stored prices (DIA). - Valid signatures do not establish the requested feed, positive price, confidence, channel or freshness.
- Lazer envelope and per-feed timestamps use microseconds; legacy Core used seconds; verifier age limits remain seconds (units).
- A local fixture parser can accept fields its mainnet consumer rejects.
- Relayer names and README examples can describe obsolete storage interfaces: the stx-labs relayer calls
verify-and-update-price-feedsand readspyth-lazer-storage.get-price, both absent now. Match executable source and ABI.
Lazer consumer rules
The current oracle source and decoder define the interface.
- Call the instance's
verify-price-feedswith the signed update ((buff 8192)), the decoder contract and an optionalmax-agein seconds (noneuses the governance configuration).max-agereplaces that threshold rather than tightening it:(some u86400)accepts a day-old update even where governance set 7,200 s, so enforce your own freshness on the per-feed timestamp. The result carries the envelopetimestamp(microseconds), itschannelandprice-feeds, a list of feed tuples. - The current implementation accepts an
evmenvelope: secp256k1 overkeccak256(payload). The verifier checks signature, trusted signer and envelope age; it does not apply your feed, channel, duplicate, future-time or per-feed freshness policy. - Decoder: Pass the instance's configured decoder:
verify-price-feedsrejects any other (ERR_INVALID_DECODER), and only the oracle may call the decoder's verification. Governance can rotate the decoder, so take it as a<decoder-trait>argument rather than a constant that stops every read after a rotation. - Each feed tuple is
{feed-id: uint, price: (optional int), exponent: (optional int), publisher-count: (optional uint), confidence: (optional uint), best-bid: (optional int), best-ask: (optional int), funding-rate: (optional int), funding-timestamp: (optional uint), funding-rate-interval: (optional uint), market-session: (optional uint), ema-price: (optional int), ema-confidence: (optional uint), feed-update-timestamp: (optional uint)}. A successful decode may leave optional properties empty: require price, exponent, confidence, publisher count and per-feed timestamp. - Instance: The documented model is one governed instance per consumer, not a public shared oracle; check current upstream service/access terms. Zest v2 (
SP1A27KFY4XERQCCRCARCYD1CC5N7M6688BSYADJ7.pyth-lazer-oracle) and Granite (SPMV5HDZ4EMB8XY7HAYT3XW0DF7DZ4E8XEG2J1T8.pyth-lazer-oracle) each run their own (addresses). Anyone can callverify-price-feedson any instance, but its governance decides which signers it trusts and can replace them at once, even while the instance is paused, and so sign any price. Calling another protocol's instance makes its governance your price authority; deploy your own (deploy your own instance). - Verify signer rotation/expiry, fees charged to
tx-sender, pause state and decoder governance at the target deployment; include oracle fees and caller authority in the full call graph.
Lazer price checks
After verify-price-feeds succeeds, require all of these for the feed you use:
- The configured feed ID is present (select it from
price-feeds; missing is an error). Feed 1 is BTC/USD, 7 USDC/USD and 45 STX/USD, each at exponent -8. - The exponent equals the configured one, and the price is present and positive; convert the signed price to uint only after that guard.
publisher-countat least a configured minimum.- Confidence within a bound of the price:
confidence * 10000 <= price * max-bps. - The per-feed
feed-update-timestampis no later than the envelopetimestamp(a fresh envelope can carry a stale feed) and fresh:feed-time-us / 1000000 + max-age >= stacks-block-time.
;; fragment: verify one Lazer feed inside the transaction that uses its price
(let ((decoded (try! (contract-call? .pyth-lazer-oracle verify-price-feeds
update .pyth-lazer-decoder-v1 (some MAX_AGE))))
(feed (unwrap! (element-at? (filter is-feed (get price-feeds decoded)) u0) ERR_FEED_MISSING))
(price (unwrap! (get price feed) ERR_BAD_PRICE))
(confidence (unwrap! (get confidence feed) ERR_WIDE_CONFIDENCE))
(feed-time-us (unwrap! (get feed-update-timestamp feed) ERR_FEED_STALE)))
(asserts! (is-eq (get exponent feed) (some EXPONENT)) ERR_BAD_EXPONENT)
(asserts! (> price 0) ERR_BAD_PRICE)
(asserts! (>= (default-to u0 (get publisher-count feed)) MIN_PUBLISHERS) ERR_LOW_PUBLISHERS)
(asserts! (<= feed-time-us (get timestamp decoded)) ERR_FEED_STALE)
(asserts! (>= (+ (/ feed-time-us u1000000) MAX_AGE) stacks-block-time) ERR_FEED_STALE)
(asserts! (<= (* confidence u10000) (* (to-uint price) MAX_CONF_BPS)) ERR_WIDE_CONFIDENCE)
(ok {price: (to-uint price), time: (/ feed-time-us u1000000)}))is-feed is a private predicate over the feed tuple above that compares feed-id with the configured ID. Choose and document your own bounds, such as MIN_PUBLISHERS u3, MAX_CONF_BPS u100 (1%), MAX_AGE u60 seconds and, for a mark, MAX_MARK_AGE u900 seconds. The fragment calls your own instance, deployed beside the consumer (deploy your own instance). Its hard-coded decoder is for brevity only (see the decoder rule above).
Two patterns:
- Update per call: Functions that use the price take the update as an argument and verify it in the same transaction (Zest's and Granite's markets do this).
- Verified mark: Anyone may call a
markfunction with an update; it stores{price, time}only from a verified update strictly newer than the stored one (replays and older updates are refused). Readers reject a mark older than a maximum mark age. Callers choose when, never the price. Staleness halts what depends on the mark; take updates per call if deposits must stay open.
Never value assets from a DEX pool's spot or active-bin price: it can be moved within the same transaction. Recommend a verified on-chain oracle. A price a keeper or admin supplies is a trust assumption the design may accept (no oracle for the asset, a fixed peg, a manual fallback): bound each change, reject stale values, record who set it and when, and tell the developer which party it trusts (vault valuation).
Liquid staking ratios
Pairs: stSTX/STX, stSTXbtc/STX (fixed 1:1) and stBTC/sBTC.
Price stSTX as STX/USD (Lazer feed 45) times (contract-call? 'SP4SZE494VC2YC5JYG7AYFQ44F5Q4PYV7DVMDPBG.data-stx-v2 get-stx-per-ststx): a bare uint (do not unwrap it), STX per stSTX at 6 decimals, rounded down (get-stx-per-ststx-up rounds up, for minting). stSTXbtc redeems 1:1 for STX, so it takes the STX price; stBTC uses data-stbtc-v1 get-sbtc-per-stbtc (8 decimals).
- Anyone can raise the ratio at once by sending STX to
stx-reserve-v2or burning stSTX (+1% costs about 1% of the STX backing stSTX), and StackingDAO governance can move it either way. - Migrations replace the data contract, and the old getters (
data-core-v1to-v3,data-stx-v1,stacking-dao-core-v1) now return wrong ratios or revert; the August 2026 cutover cut the ratio 1.24% in one block. Bound the ratio, cap its change per block, and keep a governed way to repoint and pause. - It is the redemption value, not the market price: slow withdrawals take at least 2,100 Bitcoin blocks, instant ones pay a governed fee and need idle STX, and a large DEX sale can clear well below the ratio. Size liquidation bonuses for selling seized stSTX.
DIA USDh/USD
DIA's oracle is the only on-chain source of a USDh/USD price. Read it with (contract-call? 'SP1G48FZ4Y7JY8G2Z0N51QTCYGBQ6F4J43J77BQC0.dia-oracle get-value "USDh/USD"), which returns (ok {value, timestamp}): the value at 8 decimals and the timestamp in Unix milliseconds (addresses).
- One updater key pushes both the value and the timestamp (checked against
tx-sender), so the price is that key's word. - The same contract stores other keys, such as
BTC/USDandSTX/USD, each updated on its own schedule, and some go stale for weeks: check each key's timestamp. - A missing key returns zeros, not an error: require a nonzero value and
(/ timestamp u1000)within your maximum age ofstacks-block-time.
Timestamp units
| Field / clock | Unit |
DIA get-value timestamp (USDh/USD) | Unix milliseconds |
Lazer envelope timestamp (timestampUs off-chain), per-feed feed-update-timestamp | Unix microseconds |
Stacks verifier max-age, stale threshold; signer expires-at; stacks-block-time | Seconds |
JavaScript Date.now(), channel names such as fixed_rate@200ms | Milliseconds, not the payload timestamp unit |
- Convert raw Lazer values with
(/ timestamp-us u1000000)for second-based comparisons (integer division rounds down), and DIA or JavaScript milliseconds with(/ timestamp-ms u1000); do not reuse one feed's conversion for another. - The verifier converts envelope time for its age check but returns raw decoded timestamps; do not assume the returned tuple was normalized.
- If the consumer caches prices, recheck freshness when reading them; a once-valid update eventually expires.
Sources: Pyth payload units, DIA oracle, Stacks verifier.
Protocol consumers
Each protocol's checks are its own design choice, not a minimum: apply the price checks above to your consumer.
- Zest v2
v0-8-market, on its own instance: price-dependent calls takeprice-feeds, a list that must hold exactly one update despite its ABI maximum of three. That update must carry every Lazer feed the account holds, because the market values the whole position. It accepts feeds 1, 7 and 45 from channels 3 or 4 (200 ms or 1 s fixed rate) and requires confidence within a governed ratio, at least three publishers, and a per-feed timestamp no later than the envelope, within the asset's governed maximum age, at most 60 s ahead and no older than the last one used for that feed. USDh comes from DIA. Collateral adds, repays and liquidations requirecontract-callerto equaltx-sender(u400025), so a contract calls them fromas-contract?and owns the position itself. Its local-testing helpers differ (Zest test fixtures). - Granite
borrower-v1/liquidator-v1, on its own instance: borrowing, removing collateral and liquidating take a required update. Itspyth-adapter-v1verifies it withmax-age none(the instance's governed threshold), then requires a positive price, an exponent within [-18, 11], an envelope time within its governedtime-deltaand, when the update carries one, confidence within the token's governed ratio. The live logic does not call its legacy Core adapters (Granite). - Hermetica has no Lazer path in production: USDh minting decodes Pyth Core payloads per call, which are no longer published on Stacks, and prices USDCx at a fixed $1; hBTC's share price uses no oracle (Hermetica prices).
Deploy a Lazer instance for your protocol
Pyth runs no Stacks contract, so each protocol deploys and governs its own verifier; the upstream README says Pyth's terms forbid sharing one deployment. The subscription terms do not mention instances but forbid sharing API keys, so each protocol needs its own Pyth Pro key regardless. Steps:
- Access: Get a Pyth Pro API key (a Pyth Terminal account at app.pyth.com; production plans through Pyth). Keys are limited to asset types and a minimum channel, so first look up each feed's id, exponent and
min_channelathttps://pyth.dourolabs.app/v1/symbols(no key needed). Keep the key server-side; browsers use short-lived tokens fromhttps://pyth.dourolabs.app/auth/token. - Revision: Deploy stx-labs/stacks-pyth-lazer at
a6034d2e, the audited contracts. - Deploy: Publish
pyth-lazer-traits,pyth-lazer-oracle, thenpyth-lazer-decoder-v1, unmodified, from one account (they call each other by relative name), withclarity_version = 5set explicitly: Clarity 3 fails onstacks-block-time, and epoch 4.0 otherwise defaults new contracts to the untested Clarity 6. Upstream ships only testnet plans: write your own mainnet plan and rehearse on testnet, where the default staleness is about five years until you callset-stale-price-threshold. The deployer starts with both roles and as fee recipient; deploying from your governance multisig removes that single-key window. - Check: Compare the published source with the pinned files, and read the initial state with read-only calls: no trusted signers, fee
u0, thresholdu7200, not paused, decoder.pyth-lazer-decoder-v1. - Trust Pyth's signer: Every verification fails with
u2105until you call(set-trusted-signers (list {pubkey: 0x03a4380f01136eb2640f90c17e1e319e02bbafbeef2e6e67dc48af53f9827e155b, expires-at: u<unix seconds>})). The oracle stores this 33-byte compressed key (EVM address0x26FB61A864c758AE9fBA027a96010480658385B9); confirm it by recovering the signer of a fresh update with the decoder's read-onlyrecover-signer. Each call replaces the whole list. - Hand off governance: The roles are
0x00(governance: signers, decoder, threshold, fee, roles) and0x01(pause), checked againstcontract-caller. A DAO that executes through proposals needs an adapter that holds the roles and forwards each admin call whentx-senderis the DAO, as Zest v1'sSP2VCQJGH7PHP2DJK7Z0V48AGBHQAW3R3ZW1QF4N.pyth-lazer-governance-v1does. From the deployer, grant both roles to governance and0x01to any emergency pauser. Governance then revokes the deployer (no one can remove their own governance role,u4005) and pointsset-fee-recipientat the treasury. - Timelock in your governance: The oracle applies every change at once, and
set-trusted-signersworks even while paused, so whoever can call it can sign any price. Put signer, decoder, threshold and fee changes behind your governance timelock (immediate actions) and keep pausing instant. Give governance the pause role too: while paused every other setter fails (u4004), so recovering from a rogue pauser takes one contract call that unpauses and revokes it, which a native multisig cannot do. - Verify the end state:
has-rolefalse for the deployer on both roles and true for governance, a future signer expiry, feeu0, not paused. Every setter prints an event; the contract's event log is the audit trail.
Operate it:
- Renew or rotate the signer well before
expires-at, allowing for your governance delay. Pyth can rotate its key through its DAO, so follow its announcements. Rotate without downtime by listing the old and new keys together, then dropping the old one. - Fetch updates server-side: REST
POST https://pyth-lazer.dourolabs.app/v1/latest_pricewithAuthorization: Bearer <key>, or the stream atwss://pyth-lazer-{0,1,2}.dourolabs.app/v1/stream(@pythnetwork/pyth-lazer-sdk7.0.1 or later). Requestformats: ["evm"](the only envelope the decoder verifies),jsonBinaryEncoding: "hex", a fixed-rate channel (fixed_rate@200msorfixed_rate@1000ms) andprice,exponent,confidence,publisherCountandfeedUpdateTimestamp. Pass theevm.databytes as the(buff 8192)update. The v1 decoder rejects property types above 12 (u2205). - The verifier stores nothing, so a relayer that only pushes updates helps no one: put a fresh update into each consumer transaction.
- Before sending a transaction, check a fetched update with the decoder's read-only
recover-signeranddecode-lazer-payload.
Integrate and test
- Record consumer, oracle, decoder, signer expiry, roles, fees, feed IDs, channels and source revisions.
- Verify payload bounds, signature recovery, field counts, endianness and trailing bytes. Test missing/duplicate feeds and properties explicitly.
- Check envelope and feed age, future timestamps and exact threshold boundaries: stale feeds in fresh envelopes, milliseconds mistaken for microseconds, double conversion.
- Convert prices into the accounting asset's units. USD feeds do not establish a bridged or wrapped token's parity with its reference asset.
- Test the actual verifier/parser (Clarigen by default, or the project's harness), plus authentic signed vectors and isolated test signers (signed fixtures). Mutate signature, payload, signer, feed, time, price, exponent and confidence independently. Assert the consumer's behavior, not merely
(ok decoded)from the verifier. - Refresh deployed governance/configuration even when repository HEAD is unchanged.
- Migrating from Core changes payload acquisition, feed IDs, decoder, caller checks and storage assumptions. Replace cached-price reads with verified transaction inputs where required, and test missing updates, halted delivery, expired signers, pause/recovery and stale feeds before enabling new collateral or liquidation paths.
Signed fixtures
- Pin the reviewed upstream revision (a Git submodule, a vendored copy at that commit, or
[[project.requirements]]for a deployed instance) and reference its contracts from the test manifest. Keep local signer/bootstrap helpers separate; deployment bindings must identify the intended on-chain instance and governance. - Start with golden fixture tests, synthetic encoders, decoder and governance tests.
- Golden vectors establish external encoding compatibility; self-generated vectors alone can reproduce the encoder's own mistake.
- Historic vectors need controlled fixture time or a test-only freshness override. Never copy those permissive thresholds into production.
- Register deterministic signers only in isolated fixtures. Test untrusted/expired keys, high-S signatures, malformed lengths, trailing data, sparse fields, duplicates and boundary timestamps.
- Refresh signed quotes after time-advancing setup or block skips. Simnet public setup calls advance the clock too; test the timestamp read by the consumer.
- Use
clarity-testingfor Clarigen setup. - Authentic updates for a mainnet fork: a successful mainnet transaction that verified one carries it in its arguments (for example the
updateargument of a Graniteborrower-v1call or theprice-feedsargument of a Zestv0-8-marketcall; list calls with/extended/v3/principals/<contract>/transactions, then read arguments with/extended/v3/transactions/<txid>?include=function_args). Fork one block before that transaction so the signer registered then is trusted, and send freshness-sensitive calls in onesimnet.mineBlockbefore any empty block (fork block time).
Related skills
clarity-testing: Clarigen, dependency fixtures and public-call assertions.clarity-epochs: clocks, signatures and buffer semantics.clarity-deployment: ordered contract publication and governance bootstrap.clarity-protocols: protocol-specific oracle consumers and deployment records, including DIA (USDh/USD) and Granite's Lazer adapter.clarity-ecosystem: vault valuation with these prices.
Reference baseline
Examples use Clarity 4+ syntax unless marked, checked on epoch 4.0. Source links in this skill use these pins.
stacks-core: ccf31dc69c.pyth-lazer: a6034d2e39.- Audit: Clarity Alliance, 2026-07-27, final revision
bf945c12c698b008d97624b4c424d720454acb10; excludes off-chain infrastructure.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

