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