sui_gql_client/queries/
fragments.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
use af_sui_types::{Address as SuiAddress, ObjectId, TypeTag, Version};
use cynic::QueryFragment;
use sui_gql_schema::{scalars, schema};

// ====================================================================================================
//  Input objects
// ====================================================================================================
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 {
        /// Filter objects by their type's `package`, `package::module`, or their fully qualified type
        /// name.
        ///
        /// Generic types can be queried by either the generic type name, e.g. `0x2::coin::Coin`, or by
        /// the full type name, such as `0x2::coin::Coin<0x2::sui::SUI>`.
        #[cynic(rename = "type")]
        pub type_: Option<scalars::TypeTag>,
        pub owner: Option<Address>,
        pub object_ids: Option<Vec<ObjectId>>,
        /// Filter for live objects by their ID and version.
        #[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 {
    /// Filter objects by their type's `package`, `package::module`, or their fully qualified type
    /// name.
    ///
    /// Generic types can be queried by either the generic type name, e.g. `0x2::coin::Coin`, or by
    /// the full type name, such as `0x2::coin::Coin<0x2::sui::SUI>`.
    #[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 {
    /// The string type of the DynamicField's 'name' field.
    /// A string representation of a Move primitive like 'u64', or a struct type like '0x2::kiosk::Listing'
    #[cynic(rename = "type")]
    pub type_: scalars::TypeTag,
    /// The Base64 encoded bcs serialization of the DynamicField's 'name' field.
    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,
}

// ====================================================================================================
//  Simple fragments
// ====================================================================================================

#[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> {
        // Fail early if type tag is not expected
        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),
}

/// Helper to extract a strongly typed [`TypeTag`] from the `MoveType` GQL type.
#[derive(cynic::QueryFragment, Clone)]
#[cynic(graphql_type = "MoveType")]
pub struct MoveTypeTag {
    /// Keep this private so that we can change where we get the [TypeTag] from.
    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
    }
}

// ====================================================================================================
//  Internal
// ====================================================================================================

#[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
    }
}