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