Repository navigation
feat(examples): read the SEP-40 feed from a consumer #440
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 16 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
72f7593
feat(examples): add a SEP-40 consumer contract
hpmaxi c8afda9
test(harness): read the feed from a consumer
hpmaxi ab178a4
fix(harness): scope force and check event payload
hpmaxi e24abeb
style(harness): shorten the send comment
hpmaxi e7d2c22
docs: record the SEP-40 consumer on testnet
hpmaxi 707f2c6
docs: say the consumer run can attest a NAV
hpmaxi 2416c10
test(examples): check temporary storage is empty
hpmaxi 21aa722
fix(examples): refuse a non-contract feed
hpmaxi 976c9dd
docs: correct the recorded SEP-40 no-price run
hpmaxi 2850566
refactor(harness): drop unused recordedEvent
hpmaxi 350d1b0
refactor(harness): merge wasmOf and exampleWasmOf
hpmaxi cc698e3
test(harness): verify the sent consumer event too
hpmaxi b482ccb
docs: note the example in the doc-size test
hpmaxi d63f656
docs(harness): say test:sep40 attests a stale NAV
hpmaxi 0a3cd79
fix(harness): attest on a fresh network
hpmaxi fc675a9
docs: record a SEP-40 price read on testnet
hpmaxi ee96ae3
docs: link the feed design, not a decision record
hpmaxi 0c8e49f
test(harness): read contract events of a sent tx
hpmaxi 6079c4b
docs(examples): leave the price age to callers
hpmaxi 39be5b2
Merge commit 'e05e4efaad7c04af293d7cf1cba4c74ddefc99b0' into feat/237…
hpmaxi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| [package] | ||
| name = "sep40-consumer" | ||
| version = "0.1.0" | ||
| edition.workspace = true | ||
| license.workspace = true | ||
| publish = false | ||
|
|
||
| [lib] | ||
| crate-type = ["lib", "cdylib"] | ||
| doctest = false | ||
|
|
||
| [dependencies] | ||
| soroban-sdk = { workspace = true } | ||
| sep40 = { path = "../../crates/sep40" } | ||
|
|
||
| [dev-dependencies] | ||
| soroban-sdk = { workspace = true, features = ["testutils"] } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| //! # Design | ||
| //! | ||
| //! A third-party contract that reads a SEP-40 feed and records what it finds. | ||
| //! It proves the standard alone is enough to consume a feed; it is not part | ||
| //! of the kit. | ||
| //! | ||
| //! - **Knows only SEP-40.** It depends on the `sep40` crate alone, never on a | ||
| //! kit crate. The cost: it cannot tell a kit feed from any other SEP-40 | ||
| //! feed, nor use anything the kit adds beyond the standard. | ||
| //! - **Never panics on a missing price or a failing feed.** A feed that | ||
| //! answers no price, whose call fails outright, or whose address is not a | ||
| //! contract, all end in `NoPrice`. The cost: a malformed feed address and | ||
| //! an honest "no price yet" look the same from here. A feed that uses up | ||
| //! the whole transaction budget still fails the call; this contract cannot | ||
| //! protect against that. | ||
| //! - **One event per call.** `record_price` always emits exactly one of | ||
| //! `PriceRecorded` or `NoPrice`. The cost: a caller watching only one event | ||
| //! kind must still handle the other. | ||
| //! - **No state.** Nothing is written to instance or persistent storage; the | ||
| //! price lives only in the event. The cost: no history, no last-known | ||
| //! 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. | ||
|
|
||
| #![no_std] | ||
|
|
||
| #[cfg(test)] | ||
| mod test; | ||
|
|
||
| use sep40::{Asset, PriceFeedClient}; | ||
| use soroban_sdk::{contract, contractevent, contractimpl, Address, Env, Executable}; | ||
|
|
||
| #[contractevent] | ||
| pub struct PriceRecorded { | ||
| #[topic] | ||
| pub feed: Address, | ||
| #[topic] | ||
| pub asset: Asset, | ||
| pub price: i128, | ||
| pub timestamp: u64, | ||
| pub decimals: u32, | ||
| pub resolution: u32, | ||
| } | ||
|
|
||
| #[contractevent] | ||
| pub struct NoPrice { | ||
| #[topic] | ||
| pub feed: Address, | ||
| #[topic] | ||
| pub asset: Asset, | ||
| } | ||
|
|
||
| #[contract] | ||
| pub struct Sep40Consumer; | ||
|
|
||
| #[contractimpl] | ||
| impl Sep40Consumer { | ||
| /// Reads `feed`'s price for `asset` and records what it finds. | ||
| /// | ||
| /// # Arguments | ||
| /// | ||
| /// * `feed` - The SEP-40 feed to read. | ||
| /// * `asset` - The asset to price, in the feed's own terms. | ||
| /// | ||
| /// # Events | ||
| /// | ||
| /// * `PriceRecorded` - `feed` answered `decimals`, `resolution` and a | ||
| /// price for `asset`. | ||
| /// * `NoPrice` - Any of those reads failed, the feed named no price, or | ||
| /// `feed` is not a contract address. | ||
| /// | ||
| /// # Authorization | ||
| /// | ||
| /// None. | ||
| pub fn record_price(e: &Env, feed: Address, asset: Asset) { | ||
| if !matches!(feed.executable(), Some(Executable::Wasm(_))) { | ||
| NoPrice { feed, asset }.publish(e); | ||
| return; | ||
| } | ||
|
|
||
| let client = PriceFeedClient::new(e, &feed); | ||
| let reading = ( | ||
| client.try_decimals(), | ||
| client.try_resolution(), | ||
| client.try_lastprice(&asset), | ||
| ); | ||
|
|
||
| if let (Ok(Ok(decimals)), Ok(Ok(resolution)), Ok(Ok(Some(data)))) = reading { | ||
| PriceRecorded { | ||
| feed, | ||
| asset, | ||
| price: data.price, | ||
| timestamp: data.timestamp, | ||
| decimals, | ||
| resolution, | ||
| } | ||
| .publish(e); | ||
| } else { | ||
| NoPrice { feed, asset }.publish(e); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.