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

feat(hermes): create latest TWAP endpoint #2131

Merged
merged 10 commits into from
Nov 26, 2024
Merged

feat(hermes): create latest TWAP endpoint #2131

merged 10 commits into from
Nov 26, 2024

Conversation

tejasbadadare
Copy link
Contributor

@tejasbadadare tejasbadadare commented Nov 19, 2024

Purpose

Create Hermes endpoint at /v2/updates/twap/:window_seconds/latest/ to get the latest TWAP prices for a list of price IDs with a dynamic time window (default 300s/5m).

  • Hermes will calculate the requested TWAP, but on-chain consumers won’t have a way to verify this calculated price. Thus, the “real” calculation needs to happen on-chain, using the verified prices from t0 and t1, in the target chain contract.
    • The calculation returned by Hermes (in parsed) will match what our SDK will return from on-chain.
  • Thus, the endpoint returns the following structure (TwapsResponse):
    • binary (list of BinaryUpdate, where each update contains the start & end TwapMessages for a requested feed)
      • The TwapMessages contain everything needed to compute the TWAP on-chain (cumul price, expo, slot, num down slots, etc).
      • parsed (list of ParsedPriceFeedTwap, which contains the calculated TWAP for each requested feed)

See a sample response here.

Testing

  • Added unit tests

Next up

  • Next PR will include an endpoint for fetching historical TWAP given a publish_time.
  • Future PR will include functionality to fallback to Benchmarks if the required price updates aren't present in the cache (for TWAPs longer than ~15m.)
  • Future PR will add these features to the Solana receiver contract.

Copy link

vercel bot commented Nov 19, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
api-reference ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 25, 2024 8:23pm
proposals ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 25, 2024 8:23pm
staking ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 25, 2024 8:23pm
2 Skipped Deployments
Name Status Preview Comments Updated (UTC)
component-library ⬜️ Ignored (Inspect) Visit Preview Nov 25, 2024 8:23pm
insights ⬜️ Ignored (Inspect) Visit Preview Nov 25, 2024 8:23pm

Copy link
Collaborator

@ali-bahjati ali-bahjati left a comment

Choose a reason for hiding this comment

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

Amazing!

apps/hermes/server/src/state/aggregate.rs Outdated Show resolved Hide resolved
apps/hermes/server/src/state/aggregate.rs Show resolved Hide resolved
Copy link
Collaborator

@ali-bahjati ali-bahjati left a comment

Choose a reason for hiding this comment

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

Nice! Please bump the version as well.

validate_price_ids(&state, &price_id_inputs, params.ignore_invalid_price_ids).await?;

// Collect start and end bounds for the TWAP window
let window_seconds = path_params.window_seconds as i64;
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe you want this to be an i64 in the params to avoid this conversion here?

Copy link
Contributor Author

@tejasbadadare tejasbadadare Nov 25, 2024

Choose a reason for hiding this comment

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

i made window_seconds of type DurationInSeconds from pyth_sdk, which is a u64 (probably to enforce a nonnegative duration.) it seemed like a good fit but i see your point as well -- any preference on i64 vs u64 here?

price: pft.twap.price,
conf: pft.twap.conf,
expo: pft.twap.expo,
publish_time: pft.twap.publish_time,
Copy link
Contributor

@guibescos guibescos Nov 22, 2024

Choose a reason for hiding this comment

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

publish_time seems redundant

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah fair. i reused RpcPrice here to avoid creating a slightly different struct just for twap prices, since the other fields fit well (price, conf, expo). thoughts?

// Perform division before casting to maintain precision
// Cast slot_diff to the same type as price / conf diff before division
let price = (price_diff / slot_diff as i128) as i64;
let conf = (conf_diff / slot_diff as u128) as u64;
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's better to use the checked casts u128::from and u64::try_from generally

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oo great tip ty

Copy link
Collaborator

Choose a reason for hiding this comment

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

The as keyword for integer conversion is one the biggest mistakes in rust :D very un-rusty

Copy link
Contributor Author

Choose a reason for hiding this comment

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

is there a rust equivalent to the zen of python out there? :)

@tejasbadadare
Copy link
Contributor Author

Nice! Please bump the version as well.

it is getting bumped to new minor version 0.8.0, want me to bump major version instead?

@keyvankhademi
Copy link
Contributor

Nice 👍

Copy link
Collaborator

@ali-bahjati ali-bahjati left a comment

Choose a reason for hiding this comment

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

Very nice!

/// The % of slots where the network was down over the TWAP window.
/// A value of zero indicates no slots were missed over the window, and
/// a value of one indicates that every slot was missed over the window.
/// This is a float value stored as a string to avoid precision loss.
Copy link
Collaborator

Choose a reason for hiding this comment

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

update this comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it is actually being returned as a string (utoipa's default behavior for Decimal)

@tejasbadadare tejasbadadare merged commit d6ba2b6 into main Nov 26, 2024
8 checks passed
@tejasbadadare tejasbadadare deleted the tb/hermes/twap branch November 26, 2024 17:28
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.

4 participants