Repository navigation
feat(examples): read the SEP-40 feed from a consumer - #440
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
luchobonatti
left a comment
There was a problem hiding this comment.
Looks good. A few optional suggestions inline.
| // The NAV can lapse between simulation and inclusion; re-check the | ||
| // event the network actually recorded, not just the simulated one. | ||
| const included = decodedEvent(sentEvents(sent ?? {})) | ||
| expect(included?.name).toBe("price_recorded") |
There was a problem hiding this comment.
Maybe also assert decimals and resolution on included.data, so the recorded payload is covered too?
There was a problem hiding this comment.
Done in 0c8e49f: the included event's data is checked for decimals and resolution too.
| getTransactionResponse?: rpc.Api.GetTransactionResponse | ||
| }): readonly xdr.DiagnosticEvent[] { | ||
| const response = sent.getTransactionResponse | ||
| return response && "diagnosticEventsXdr" in response |
There was a problem hiding this comment.
diagnosticEventsXdr is optional in the SDK types. Would events.contractEventsXdr be a safer source?
There was a problem hiding this comment.
Agreed, done in 0c8e49f. sentEvents reads events.contractEventsXdr now, and decodedEvent works on contract events, so the simulated and the included transaction go through the same finder.
| //! price to read back from the contract itself. | ||
| //! - **Anyone may call it.** `record_price` takes no authorization. The cost: | ||
| //! anyone can spend the call's fee to record a price, including a stale or | ||
| //! absent one. |
There was a problem hiding this comment.
Maybe a bullet saying staleness checks are left to callers, so integrators don't copy it as-is?
There was a problem hiding this comment.
Done in 6079c4b: a design bullet says the consumer leaves the price's age to the caller, and what that costs if copied as is.
Summary
Closes #237
Refs #433
The SEP-40 feed alone does not show that someone else can read it. This adds a small consumer contract that knows only the SEP-40 interface, reads the feed in one transaction, and emits what it read. It is deployed on testnet, and the transaction that recorded a price is in the README.
Changes
examples/sep40-consumer: one entry point,record_price(feed, asset). It readsdecimals,resolutionandlastprice, and emitsPriceRecordedwith the price, its timestamp, the decimals and the resolution, orNoPrice. It keeps no state.NoPrice, never a panic.sep40crate; it lives underexamples/, notcontracts/.examples/*; the build, the client generation and the doc-size test cover the example.test:sep40deploys a new consumer, attests a fresh NAV when the current one is stale, and checks the event in both the simulation and the sent transaction.send()takesforce(off by default, used only forrecord_price, which simulates as a read) andonSent.Deviations
None.
Acceptance criteria
sep40crate, not on kit typesdecimals,resolutionandlastprice, and handles a missing price without panickingTest plan
Automated tests
cargo test --workspace --locked, clippy with-D warnings,cargo fmt --check: pass. The consumer's tests cover a price, no price, an unknown asset, a failing feed, a feed that is an account address, and no stored state.npm run lint,format:check,typecheck,test, and the harness unit tests: pass.deploy,test:happy-path,test:sep40: pass.test:sep40also passes alone on a freshly deployed network with no NAV yet.Manual verification
CDKCYZ6IHE2I6KAGATCQPTOYX7GUTTCYRLK26HOQ2JVHEHGXNVA27DOJ, built from this branch, deployed in5142b57e…4505.3478b5b3…2bf2.record_priceagainst the feed ineb0fd738…fba7: one transaction, aprice_recordedevent with price100100000000000(1.001 at 14 decimals) and the attestation's timestamp.Breaking changes
None.
Checklist
Screenshots
None.