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

introduce price feed server #10

Merged
merged 2 commits into from
Aug 4, 2021
Merged

introduce price feed server #10

merged 2 commits into from
Aug 4, 2021

Conversation

hussein-aitlahcen
Copy link
Contributor

@hussein-aitlahcen hussein-aitlahcen commented Aug 3, 2021

This PR will introduce the first draft of the price server we are going to use for the oracle pallet.
Assuming we have pairs of assets such as BTC/USD, we provide a HTTP endpoint GET /price/BTC/USD for the offchain worker.
The returned object is of type { "timestamp": UNIXEPOCH, "value": price } where timestamp is the time at which the latest price notification has been received.

Why not directly use the pythd websocket or any other source directly? The offchain worker are targeting WASM and pyth-client is written in C++. C++ FFI for Rust is not supported by WASM target. More than that, the offchain worker is only able to fire HTTP calls, not anything more sophisticated such as websocket...

The other side of the server is connected to pyth-client daemon and subscribe to price events for asset pairs. It only store the latest received (timestamped) price.
Currently, the implementation handle only one feed, which is provided by the pyth daemon.
By default, I hardcoded the subscriptions because I don't know whether we want to make this thing more dynamic (perhaps cli option?).
When all the subscriptions are dropped, either because of network failure or because of pyth daemon, the server will gracefully exit; is this a desired behavior or should we make it crash?

If we want to handle multiple feeds, we will have to think about either merging the price directly at the server level (this involve some kind of synchronisation because for X feed, their latest price might be spaced by a bit of time?).

Finally, as you will notice, there is no tests yet... Because I don't really know how to test this kind of "networked" stuff (most of the code is synchronisation and networking stuff). Feel free to give me any advice :)!

NOTE:
If you want to test it out, you will have to use my patch of pyth-client, their JSONRPC implementation of subscription was... not common.
I provided a nix shell script inside the price-feed subdirectory that will make both pythd and pyth_tx running locally.

let asset_pair = (x, y);
match prices.read().unwrap().get(&asset_pair) {
Some(latest_price) => with_status(json(latest_price), StatusCode::OK),
// TODO: how to 404 without content???
Copy link
Contributor

Choose a reason for hiding this comment

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

yes, can respond 404 without content because oracle only has response.code != 200 check (https://github.com/ComposableFi/composable-node-beta/blob/main/pallets/oracle/src/lib.rs#L606)

.await
.expect("connection to pyth-client failed");

// TODO: subscribe to all asset prices? cli configurable?
Copy link
Contributor

Choose a reason for hiding this comment

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

or can add subscription on a new asset by an oracle request

@andor0
Copy link
Contributor

andor0 commented Aug 4, 2021

LGFM.
@hussein-aitlahcen, great work!

@hussein-aitlahcen
Copy link
Contributor Author

Thanks! I merge this now and open a second PR for the integration with the oracle pallet!

@hussein-aitlahcen hussein-aitlahcen merged commit 7924988 into main Aug 4, 2021
@flipchan
Copy link
Contributor

flipchan commented Aug 4, 2021

nice job! @hussein-aitlahcen

@seunlanlege seunlanlege deleted the price-feed-server branch August 4, 2021 15:50
seunlanlege pushed a commit that referenced this pull request Sep 20, 2021
haroldsphinx added a commit that referenced this pull request Jan 27, 2022
# This is the 1st commit message:

fetch depth

# The commit message #2 will be skipped:

# Update checkout action

# The commit message #3 will be skipped:

# Update checkout action

# The commit message #4 will be skipped:

# Update checkout action

# The commit message #5 will be skipped:

# Update checkout action

# The commit message #6 will be skipped:

# Update checkout action

# The commit message #7 will be skipped:

# Update checkout action

# The commit message #8 will be skipped:

# Update checkout action

# The commit message #9 will be skipped:

# Update checkout action

# The commit message #10 will be skipped:

# Update checkout action
haroldsphinx added a commit that referenced this pull request Jan 27, 2022
# This is the 1st commit message:

fetch depth

# The commit message #2 will be skipped:

# Update checkout action

# The commit message #3 will be skipped:

# Update checkout action

# The commit message #4 will be skipped:

# Update checkout action

# The commit message #5 will be skipped:

# Update checkout action

# The commit message #6 will be skipped:

# Update checkout action

# The commit message #7 will be skipped:

# Update checkout action

# The commit message #8 will be skipped:

# Update checkout action

# The commit message #9 will be skipped:

# Update checkout action

# The commit message #10 will be skipped:

# Update checkout action

# The commit message #11 will be skipped:

# Update checkout action

# The commit message #12 will be skipped:

# Update checkout action

# The commit message #13 will be skipped:

# Update checkout action

# The commit message #14 will be skipped:

# Update checkout action

# The commit message #15 will be skipped:

# Update checkout action

# The commit message #16 will be skipped:

# Update checkout action

# The commit message #17 will be skipped:

# Update checkout action
seunlanlege pushed a commit that referenced this pull request Oct 6, 2022
dzmitry-lahoda added a commit that referenced this pull request Dec 14, 2022
# This is the 1st commit message:

lfs

# This is the commit message #2:

staging migration

Signed-off-by: Dzmitry Lahoda <dzmitry@lahoda.pro>

# This is the commit message #3:

picasso

# This is the commit message #4:

yamlgit add .git add .

# This is the commit message #5:

fmt; reuse; clean

Signed-off-by: Dzmitry Lahoda <dzmitry@lahoda.pro>

# This is the commit message #6:

trying replace nixops devnet

# This is the commit message #7:

fixed container

# This is the commit message #8:

zombies in compose

# This is the commit message #9:

getting routes

# This is the commit message #10:

ops mapping

# This is the commit message #11:

returned garbage for happy refactoring

# This is the commit message #12:

more

# This is the commit message #13:

revert
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.

3 participants