use std::collections::HashMap;
use af_sui_types::{
Address as SuiAddress,
Object,
ObjectArg,
ObjectId,
ObjectRef,
StructTag,
TransactionData,
};
#[doc(hidden)]
pub use bimap::BiMap;
use futures::Stream;
use outputs::{DynamicField, ObjectKey, RawMoveStruct, RawMoveValue};
use crate::{extract, GraphQlClient, GraphQlErrors};
mod current_epoch_id;
mod epoch_final_checkpoint_num;
mod events_backward;
mod filtered_full_objects;
pub mod fragments;
mod full_object;
mod full_objects;
mod gas_payment;
mod genesis_tx;
mod latest_checkpoint;
mod latest_object_version;
mod latest_objects_version;
mod latest_version_at_checkpoint_v2;
mod max_page_size;
mod object_args;
mod object_args_and_content;
mod object_content;
mod object_type;
mod objects_content;
mod objects_flat;
pub mod outputs;
mod owner_df_content;
mod owner_df_contents;
mod owner_dof_content;
mod packages_from_original;
mod packages_published_epoch;
mod reference_gas_price;
mod transaction_blocks_status;
pub use self::events_backward::{EventEdge, EventFilter};
pub use self::gas_payment::Error as GasPaymentError;
pub use self::latest_version_at_checkpoint_v2::Error as LatestVersionAtCheckpointError;
pub use self::object_args::Error as ObjectArgsError;
pub use self::object_args_and_content::Error as ObjectArgsAndContentError;
type QueryResult<T, C> = Result<T, Error<<C as GraphQlClient>::Error>>;
#[trait_variant::make(Send)]
pub trait GraphQlClientExt: GraphQlClient + Sized {
async fn current_epoch_id(&self) -> QueryResult<u64, Self> {
current_epoch_id::query(self)
}
async fn epoch_final_checkpoint_num(&self, epoch_id: u64) -> QueryResult<u64, Self> {
epoch_final_checkpoint_num::query(self, epoch_id)
}
async fn events_backward(
&self,
filter: Option<EventFilter>,
cursor: Option<String>,
page_size: Option<u32>,
) -> QueryResult<(Vec<EventEdge>, bool), Self> {
events_backward::query(self, filter, cursor, page_size)
}
fn filtered_full_objects(
&self,
owner: Option<SuiAddress>,
type_: Option<String>,
page_size: Option<u32>,
) -> impl Stream<Item = Result<Object, Error<Self::Error>>> + '_ {
filtered_full_objects::query(self, owner, type_, page_size)
}
async fn full_object(
&self,
object_id: ObjectId,
version: Option<u64>,
) -> QueryResult<Object, Self> {
full_object::query(self, object_id, version)
}
async fn full_objects(
&self,
objects: impl IntoIterator<Item = (ObjectId, Option<u64>)> + Send,
page_size: Option<u32>,
) -> QueryResult<HashMap<ObjectId, Object>, Self> {
full_objects::query(self, objects, page_size)
}
async fn genesis_tx(&self) -> QueryResult<TransactionData, Self> {
genesis_tx::query(self)
}
async fn latest_checkpoint(&self) -> QueryResult<u64, Self> {
latest_checkpoint::query(self)
}
async fn latest_object_version(&self, object_id: ObjectId) -> QueryResult<(u64, u64), Self> {
latest_object_version::query(self, object_id)
}
async fn latest_objects_version(
&self,
object_ids: &[ObjectId],
) -> QueryResult<(u64, HashMap<ObjectId, u64>), Self> {
latest_objects_version::query(self, object_ids)
}
async fn latest_version_at_checkpoint(
&self,
id: ObjectId,
ckpt_num: u64,
) -> Result<u64, LatestVersionAtCheckpointError<Self::Error>> {
latest_version_at_checkpoint_v2::query(self, id, ckpt_num)
}
async fn object_args(
&self,
names: BiMap<String, ObjectId>,
page_size: Option<u32>,
) -> Result<BiMap<String, ObjectArg>, ObjectArgsError<Self::Error>> {
object_args::query(self, names, page_size)
}
async fn object_args_and_content(
&self,
object_ids: impl IntoIterator<Item = ObjectId> + Send,
mutable: bool,
page_size: Option<u32>,
) -> Result<Vec<(ObjectArg, RawMoveStruct)>, ObjectArgsAndContentError<Self::Error>> {
object_args_and_content::query(self, object_ids, mutable, page_size)
}
async fn object_content(
&self,
object_id: ObjectId,
version: Option<u64>,
) -> QueryResult<RawMoveStruct, Self> {
object_content::query(self, object_id, version)
}
async fn objects_content(
&self,
object_ids: Vec<ObjectId>,
) -> QueryResult<HashMap<ObjectId, RawMoveStruct>, Self> {
objects_content::query(self, object_ids)
}
async fn owner_df_content(
&self,
address: SuiAddress,
raw_move_value: RawMoveValue,
root_version: Option<u64>,
) -> QueryResult<RawMoveValue, Self> {
owner_df_content::query(self, address, raw_move_value, root_version)
}
async fn owner_df_contents(
&self,
address: SuiAddress,
root_version: Option<u64>,
first: Option<i32>,
after: Option<String>,
) -> QueryResult<(HashMap<RawMoveValue, DynamicField>, Option<String>), Self> {
owner_df_contents::query(self, address, root_version, first, after)
}
async fn owner_dof_content(
&self,
address: SuiAddress,
raw_move_value: RawMoveValue,
root_version: Option<u64>,
) -> QueryResult<(ObjectKey, RawMoveStruct), Self> {
owner_dof_content::query(self, address, raw_move_value, root_version)
}
async fn packages_from_original(
&self,
package_id: ObjectId,
) -> QueryResult<impl Iterator<Item = (ObjectId, u64)>, Self> {
packages_from_original::query(self, package_id)
}
async fn packages_published_epoch(
&self,
package_ids: Vec<ObjectId>,
) -> QueryResult<impl Iterator<Item = (ObjectId, u64, u64)>, Self> {
packages_published_epoch::query(self, package_ids)
}
async fn reference_gas_price(&self) -> QueryResult<u64, Self> {
reference_gas_price::query(self)
}
async fn transaction_blocks_status(
&self,
transaction_digests: Vec<String>,
) -> QueryResult<impl Iterator<Item = Result<(String, bool), extract::Error>>, Self> {
transaction_blocks_status::query(self, transaction_digests)
}
async fn gas_payment(
&self,
sponsor: SuiAddress,
budget: u64,
exclude: Vec<ObjectId>,
) -> Result<Vec<ObjectRef>, GasPaymentError<Self::Error>> {
gas_payment::query(self, sponsor, budget, exclude)
}
async fn max_page_size(&self) -> QueryResult<i32, Self> {
max_page_size::query(self)
}
async fn object_type(&self, id: ObjectId) -> QueryResult<StructTag, Self> {
object_type::query(self, id)
}
}
impl<T: GraphQlClient> GraphQlClientExt for T {}
#[derive(thiserror::Error, Clone, Debug)]
pub enum Error<C: std::error::Error> {
#[error("Client error: {0}")]
Client(C),
#[error("In server response: {0}")]
Server(#[from] GraphQlErrors),
#[error("Missing data in response: {0}")]
MissingData(String),
}
impl<C: std::error::Error> From<crate::extract::Error> for Error<C> {
fn from(value: crate::extract::Error) -> Self {
Self::MissingData(value.0)
}
}
#[macro_export]
macro_rules! missing_data {
($($msg:tt)*) => {
$crate::queries::Error::MissingData(format!($($msg)*))
};
}