use af_sui_types::{Address as SuiAddress, ObjectId, TypeTag, Version};
use cynic::QueryFragment;
use sui_gql_schema::{scalars, schema};
pub use self::object_filter_legacy::ObjectFilter;
#[expect(deprecated, reason = "Derived impls using ObjectFilter::object_keys")]
mod object_filter_legacy {
use af_sui_types::{Address, ObjectId};
use sui_gql_schema::{scalars, schema};
use super::ObjectKey;
#[derive(cynic::InputObject, Clone, Debug, Default)]
pub struct ObjectFilter {
#[cynic(rename = "type")]
pub type_: Option<scalars::TypeTag>,
pub owner: Option<Address>,
pub object_ids: Option<Vec<ObjectId>>,
#[deprecated = "this input filter has been deprecated in favor of `multiGetObjects` query as \
it does not make sense to query for live objects by their versions. This filter will be \
removed with v1.42.0 release."]
pub object_keys: Option<Vec<ObjectKey>>,
}
}
#[derive(cynic::InputObject, Clone, Debug)]
pub struct ObjectKey {
pub object_id: ObjectId,
pub version: Version,
}
#[derive(cynic::InputObject, Clone, Debug, Default)]
#[cynic(graphql_type = "ObjectFilter")]
pub(crate) struct ObjectFilterV2 {
#[cynic(rename = "type")]
pub(crate) type_: Option<String>,
pub(crate) owner: Option<SuiAddress>,
pub(crate) object_ids: Option<Vec<ObjectId>>,
}
#[derive(cynic::InputObject, Clone, Debug)]
pub struct DynamicFieldName {
#[cynic(rename = "type")]
pub type_: scalars::TypeTag,
pub bcs: scalars::Base64<Vec<u8>>,
}
#[cfg(feature = "move-type")]
impl<T: af_move_type::MoveType> TryFrom<af_move_type::MoveInstance<T>> for DynamicFieldName {
type Error = bcs::Error;
fn try_from(value: af_move_type::MoveInstance<T>) -> Result<Self, Self::Error> {
let af_move_type::MoveInstance { type_, value } = value;
Ok(Self {
type_: scalars::TypeTag(type_.into()),
bcs: scalars::Base64::new(bcs::to_bytes(&value)?),
})
}
}
#[derive(cynic::InputObject, Clone, Debug, Default)]
pub(crate) struct TransactionBlockFilter {
pub(crate) function: Option<String>,
pub(crate) kind: Option<TransactionBlockKindInput>,
pub(crate) after_checkpoint: Option<Version>,
pub(crate) at_checkpoint: Option<Version>,
pub(crate) before_checkpoint: Option<Version>,
pub(crate) affected_address: Option<SuiAddress>,
pub(crate) sent_address: Option<SuiAddress>,
pub(crate) input_object: Option<SuiAddress>,
pub(crate) changed_object: Option<SuiAddress>,
pub(crate) transaction_ids: Option<Vec<String>>,
}
#[derive(cynic::Enum, Clone, Debug)]
pub(crate) enum TransactionBlockKindInput {
SystemTx,
ProgrammableTx,
}
#[derive(cynic::QueryFragment, Clone, Debug)]
#[cynic(graphql_type = "PageInfo")]
pub struct PageInfoForward {
pub has_next_page: bool,
pub end_cursor: Option<String>,
}
#[derive(cynic::QueryFragment, Clone, Debug)]
#[cynic(graphql_type = "PageInfo")]
pub struct PageInfoBackward {
pub has_previous_page: bool,
pub start_cursor: Option<String>,
}
#[derive(cynic::QueryFragment, Clone, Debug)]
#[cynic(graphql_type = "MoveValue")]
pub struct MoveValueRaw {
#[cynic(rename = "type")]
pub type_: MoveTypeTag,
pub bcs: scalars::Base64<Vec<u8>>,
}
impl From<MoveValueRaw> for super::outputs::RawMoveValue {
fn from(MoveValueRaw { type_, bcs }: MoveValueRaw) -> Self {
Self {
type_: type_.into(),
bcs: bcs.into_inner(),
}
}
}
#[derive(thiserror::Error, Debug)]
#[error("TypeTag is not a Struct variant")]
pub struct NotMoveStructError;
impl TryFrom<MoveValueRaw> for super::outputs::RawMoveStruct {
type Error = NotMoveStructError;
fn try_from(MoveValueRaw { type_, bcs }: MoveValueRaw) -> Result<Self, Self::Error> {
let tag: TypeTag = type_.into();
let TypeTag::Struct(stag) = tag else {
return Err(NotMoveStructError);
};
Ok(Self {
type_: *stag,
bcs: bcs.into_inner(),
})
}
}
#[cfg(feature = "move-type")]
impl<T> TryFrom<MoveValueRaw> for af_move_type::MoveInstance<T>
where
T: af_move_type::MoveType,
{
type Error = ToMoveInstanceError;
fn try_from(MoveValueRaw { bcs, type_ }: MoveValueRaw) -> Result<Self, Self::Error> {
let type_ = TypeTag::from(type_).try_into()?;
let value = bcs::from_bytes(bcs.as_ref())?;
Ok(Self { type_, value })
}
}
#[cfg(feature = "move-type")]
#[derive(thiserror::Error, Debug)]
pub enum ToMoveInstanceError {
#[error("Mismatched types: {0}")]
TypeTag(#[from] af_move_type::TypeTagError),
#[error("Deserializing value: {0}")]
Bcs(#[from] bcs::Error),
}
#[derive(cynic::QueryFragment, Clone)]
#[cynic(graphql_type = "MoveType")]
pub struct MoveTypeTag {
repr: scalars::TypeTag,
}
impl std::fmt::Debug for MoveTypeTag {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "MoveTypeTag({})", self.repr.0)
}
}
impl From<MoveTypeTag> for TypeTag {
fn from(value: MoveTypeTag) -> Self {
value.repr.0
}
}
#[derive(cynic::QueryFragment, Clone, Debug)]
#[cynic(graphql_type = "MoveObject")]
pub(super) struct MoveObjectContent<MoveValue = MoveValueRaw>
where
MoveValue: QueryFragment<SchemaType = schema::MoveValue, VariablesFields = ()>,
{
pub(super) contents: Option<MoveValue>,
}
impl<MoveValue> MoveObjectContent<MoveValue>
where
MoveValue: QueryFragment<SchemaType = schema::MoveValue, VariablesFields = ()>,
{
pub fn into_content(self) -> Option<MoveValue> {
self.contents
}
}