Skip to content
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

added oracle example and ergonomic WASI guest HTTP client crate #4

Merged
merged 7 commits into from
Oct 2, 2024

Conversation

calvinrp
Copy link
Collaborator

@calvinrp calvinrp commented Oct 1, 2024

Incorporating feedback from Lay3rLabs/avs-toolkit#22

Simplified the ergonomic HTTP guest crate down to the bare minimum for the hackathon. Imports the latest version of wstd crate but only uses its public functions / types that work for the async runtime. Unfortunately, the rest of wstd is not usable yet. So I wrote a simplified HTTP client from scratch here that should be good enough for now.

The example Oracle operates as a task queue trigger application. When triggered,
makes an outbound HTTP request to CoinGecko API with the a provided API_KEY env var.
The app stores the BTCUSD price history in the file system. Completes with writing to the chain
the resulting current price and averages over the past minute and hour. But still requires testing additional testing.

Closes https://github.com/Lay3rLabs/wasmatic/issues/8
Closes #1
Closes Lay3rLabs/avs-toolkit#22

More docs and guiding through the Wasmatic deployment process can be added to follow up PRs or this one. Just wanted to get the code up for review ASAP.

@calvinrp
Copy link
Collaborator Author

calvinrp commented Oct 1, 2024

@macovedj is investigating what we need to change for the data structure JSON that is written to the chain as the result.

We need to change the output of the component to match the schema expected by the contract. Or create a new contract.

@ethanfrey
Copy link
Contributor

For output format, check out https://github.com/Lay3rLabs/lay3r-contracts/pull/41

The result is parsed as struct PriceResult { price: cosmwasm_std::Decimal }.
Decimal is a string-encoded fixed-point decimal, you could stringify f64 also.
Like {"price": "123.456"}

Copy link
Contributor

@ethanfrey ethanfrey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.
Please update the result type as described, then feel free to merge.

crates/layer-wasi/src/lib.rs Outdated Show resolved Hide resolved

## Deploy

Upload the compiled Wasm component to the Wasmatic node.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: we will want some cli tool to make this easier. This bash, json, curl stuff is a pain to work with.

This should be completed as part of Lay3rLabs/avs-toolkit#17 and then added here

cc @ueco-jb

oracle-example/README.md Show resolved Hide resolved
oracle-example/src/price_history.rs Show resolved Hide resolved

#[derive(Serialize, Debug)]
#[serde(rename_all = "camelCase")]
struct Price {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The result expected by the oracle contract is just the price field as a string. (you can use f32::to_string())

You can make this field and print it for nice debug logs. (I think it is cool to show println!() works, but I guess devs won't have access to logs)

But for the Result, use:

struct Result {
  price: String,
}

let result = Result { price: calc_prices.btcusd.avg_last_hour.price.to_string() };

And of course json-encode as now

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ethanfrey Just to confirm, price would be the latest BTCUSD or one of the trailing averages?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just pushed, as average price over the past minute.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, probably better over the past hour average price. Due to the frequency of polling.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought my code sample clarified. calc_prices.btcusd.avg_last_hour.price.to_string()

yes, the rolling average over the last hour.
this actually needs some overhaul in average calculation to make it "blockchain safe", but we can start with this for now and get things running

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry missed that in your example.

oracle-example/src/coin_gecko.rs Show resolved Hide resolved
@calvinrp calvinrp merged commit 98e0ea2 into main Oct 2, 2024
@calvinrp calvinrp deleted the example-and-updated-guest-ergonomic-lib branch October 2, 2024 15:17
Copy link
Contributor

@ethanfrey ethanfrey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to merge, what's left is just a naming issue, good to follow up on to not confuse anyone reading later

@@ -39,7 +39,7 @@ async fn get_avg_btc(reactor: Reactor) -> Result<Vec<u8>, String> {
history.record_latest_price(now, price)?;

// calculate average prices
let avg_last_minute = history.average(now - 60);
let avg_last_minute = history.average(now - 3600);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uh, I agree with the change, but shouldn't you change the name of the variable as well?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, yeah. I'll fix.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add oracle WASI components
2 participants