af_pyth_wrapper/
graphql.rs

1use af_move_type::MoveInstance;
2use af_sui_types::{Address, ObjectId};
3use sui_framework_sdk::object::ID;
4use sui_gql_client::queries::{Error as QueryError, GraphQlClientExt as _};
5use sui_gql_client::GraphQlClient;
6
7type Key = crate::wrapper::PythPriceInfoId;
8
9/// Error for [`GraphQlClientExt`].
10#[derive(thiserror::Error, Debug)]
11pub enum Error<C: std::error::Error> {
12    #[error("Querying Owner DF content: {0}")]
13    OwnerDfContent(QueryError<C>),
14
15    #[error("BCS De/Ser: {0}")]
16    Bcs(#[from] bcs::Error),
17
18    #[error(transparent)]
19    FromRawType(#[from] af_move_type::FromRawTypeError),
20}
21
22#[trait_variant::make(Send)]
23pub trait GraphQlClientExt: GraphQlClient + Sized {
24    /// Get the ID of the Pyth `PriceInfoObject` for a `PriceFeed`.
25    async fn price_info_object_id_for_feed(
26        &self,
27        pyth_wrapper_pkg: Address,
28        price_feed: ObjectId,
29    ) -> Result<ObjectId, Error<Self::Error>> {
30        async move {
31            let key = Key::new().move_instance(pyth_wrapper_pkg);
32            let raw = self
33                .owner_df_content(price_feed.into(), key.try_into()?, None)
34                .await
35                .map_err(Error::OwnerDfContent)?;
36            let pf: MoveInstance<ID> = raw.try_into()?;
37
38            Ok(pf.value.bytes)
39        }
40    }
41}
42
43impl<T: GraphQlClient> GraphQlClientExt for T {}