-
Notifications
You must be signed in to change notification settings - Fork 1
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
Conversation
@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. |
For output format, check out https://github.com/Lay3rLabs/lay3r-contracts/pull/41 The result is parsed as |
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.
Looks good.
Please update the result type as described, then feel free to merge.
|
||
## Deploy | ||
|
||
Upload the compiled Wasm component to the Wasmatic node. |
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.
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/src/lib.rs
Outdated
|
||
#[derive(Serialize, Debug)] | ||
#[serde(rename_all = "camelCase")] | ||
struct Price { |
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.
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
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.
@ethanfrey Just to confirm, price
would be the latest BTCUSD or one of the trailing averages?
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.
Just pushed, as average price over the past minute.
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.
Actually, probably better over the past hour average price. Due to the frequency of polling.
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.
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
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.
Sorry missed that in your example.
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.
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); |
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.
Uh, I agree with the change, but shouldn't you change the name of the variable as well?
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.
Oops, yeah. I'll fix.
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.
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 ofwstd
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 chainthe 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.