af_iperps/graphql/
mod.rs

1use std::future::Future;
2
3use af_move_type::MoveInstance;
4use af_sui_types::{Address, ObjectId, Version};
5use futures::Stream;
6use sui_gql_client::GraphQlClient;
7
8use crate::orderbook::Order;
9use crate::position::Position;
10use crate::Vault;
11
12mod ch_orders;
13mod ch_positions;
14mod ch_vault;
15mod map_orders;
16mod order_maps;
17
18pub use sui_gql_client::queries::Error;
19
20pub use self::ch_vault::Error as ChVaultError;
21pub use self::order_maps::OrderMaps;
22
23/// Extension trait to [`GraphQlClient`] collecting all defined queries in one place.
24pub trait GraphQlClientExt: GraphQlClient + Sized {
25    /// Snapshot of the orders on one side of the orderbook, rooted at the [`ClearingHouse`] id
26    /// and version.
27    ///
28    /// If you already know the object ID of the orders [`Map`], then [`map_orders`] is more
29    /// efficient.
30    ///
31    /// [`ClearingHouse`]: crate::ClearingHouse
32    /// [`Map`]: crate::ordered_map::Map
33    /// [`map_orders`]: GraphQlClientExt::map_orders
34    fn clearing_house_orders(
35        &self,
36        package: Address,
37        ch: ObjectId,
38        version: Option<Version>,
39        asks: bool,
40    ) -> impl Stream<Item = Result<(u128, Order), Error<Self::Error>>> + '_ {
41        ch_orders::query(self, package, ch, version, asks)
42    }
43
44    /// Snapshot of the orders on one side of the orderbook, rooted at the [`Map`] id and
45    /// [`ClearingHouse`] version.
46    ///
47    /// To find the [`Map`] id, you can use [`order_maps`].
48    ///
49    /// [`Map`]: crate::ordered_map::Map
50    /// [`ClearingHouse`]: crate::ClearingHouse
51    /// [`order_maps`]: GraphQlClientExt::order_maps
52    fn map_orders(
53        &self,
54        map: ObjectId,
55        ch_version: Option<Version>,
56    ) -> impl Stream<Item = Result<(u128, Order), Error<Self::Error>>> + '_ {
57        map_orders::query(self, map, ch_version)
58    }
59
60    /// Object IDs of the orderbook and asks/bids maps for a market.
61    ///
62    /// These never change, so you can query them once and save them.
63    fn order_maps(
64        &self,
65        package: Address,
66        ch: ObjectId,
67    ) -> impl Future<Output = Result<OrderMaps, Error<Self::Error>>> + Send {
68        order_maps::query(self, package, ch)
69    }
70
71    /// The unparsed clearing house's collateral [`Vault`].
72    ///
73    /// [`Vault`]: crate::Vault
74    fn clearing_house_vault(
75        &self,
76        package: Address,
77        ch: ObjectId,
78    ) -> impl Future<Output = Result<MoveInstance<Vault>, ChVaultError<Self::Error>>> + Send {
79        ch_vault::query(self, package, ch)
80    }
81
82    /// Snapshot of positions under the [`ClearingHouse`].
83    ///
84    /// [`ClearingHouse`]: crate::ClearingHouse
85    fn clearing_house_positions(
86        &self,
87        ch: ObjectId,
88        version: Option<Version>,
89    ) -> impl Stream<Item = Result<(u64, MoveInstance<Position>), Error<Self::Error>>> + '_ {
90        ch_positions::query(self, ch, version)
91    }
92}
93
94impl<T: GraphQlClient> GraphQlClientExt for T {}