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(pyth-solana-receiver): add post_twap_update instruction & types #2172

Merged
merged 3 commits into from
Dec 7, 2024

Conversation

tejasbadadare
Copy link
Contributor

@tejasbadadare tejasbadadare commented Dec 6, 2024

Purpose

Allows users to post verified TWAP prices on chain using the Pyth Solana Receiver Contract.

Implementation details

  • The post_twap_update instruction takes in start & end VAA accounts and start & end MerklePriceUpdate calldata.

    • The calldata contains cumulative prices, allowing us to calculate twap = (cumul_price_t1 - cumul_price_t0)/(pub_slot_t1 - pub_slot_t0).
    • The start & end VAAs are needed to verify that the MerklePriceUpdates have valid proofs.
  • The calculated TWAP is stored in a TwapUpdate account as follows:

      pub struct TwapUpdate {
        pub write_authority: Pubkey,
        pub verification_level: VerificationLevel,
        pub twap: TwapPrice,
        pub posted_slot: u64,
      }
      pub struct TwapPrice {
          pub feed_id: FeedId,
          pub start_time: i64,
          pub end_time: i64,
          pub price: i64,
          pub conf: u64,
          pub exponent: i32,
          /// Ratio out of 1_000_000, where a value of 1_000_000 represents
          /// all slots were missed and 0 represents no slots were missed.
          pub down_slots_ratio: u32,
      }
    • The TwapUpdate and TwapPrice types live in the solana_receiver_sdk Rust SDK to facilitate easy deserialization for users consuming the update account, similar to the PriceUpdateV2 type.
  • Misc

    • Refactored lib.rs to extract out common functionality across post_update and post_twap_update, such as collecting the update fee and verifying VAAs.

Testing

  • Added a happy path test for post_twap_update. Verification/malformed inputs are already being tested in the other tests.
  • Added unit tests for the TWAP calculation in lib.rs since calculate_twap is private and can't be accessed by the tests crates. This also tests for invalid inputs, e.g. start slot > end slot.

Up next

  • [Solana Receiver JS SDK] Add functions to verify and post cumul. prices (TwapMessages) to receiver
  • [Solana Receiver Rust SDK] Add functions to consume prices from the output TwapUpdate account
  • [HermesClient] Add TWAP endpoints to the JS client
  • Support timestamped TWAP in SDKs

Copy link

vercel bot commented Dec 6, 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 Dec 7, 2024 0:17am
proposals ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 7, 2024 0:17am
staking ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 7, 2024 0:17am
2 Skipped Deployments
Name Status Preview Comments Updated (UTC)
component-library ⬜️ Ignored (Inspect) Visit Preview Dec 7, 2024 0:17am
insights ⬜️ Ignored (Inspect) Visit Preview Dec 7, 2024 0:17am

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!

pythnet/pythnet_sdk/src/messages.rs Outdated Show resolved Hide resolved
let (price, conf, down_slots_ratio) = calculate_twap(&start_msg, &end_msg)?;

twap_update_account.write_authority = write_authority.key();
twap_update_account.verification_level = start_vaa_components.verification_level;
Copy link
Contributor

Choose a reason for hiding this comment

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

here maybe you would want to make it the minimum of start_vaa_components.verification_level and end_vaa_components.verification_level

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah yep good point, will update

twap_update_account.write_authority = write_authority.key();
twap_update_account.verification_level = start_vaa_components.verification_level;

twap_update_account.twap.feed_id = start_msg.feed_id;
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 you forgot to check that start and end are for the same id?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yea true, will update

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