sui_graphql_rpc/types/
move_type.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use async_graphql::*;
use move_binary_format::file_format::AbilitySet;
use move_core_types::{annotated_value as A, language_storage::TypeTag};
use serde::{Deserialize, Serialize};
use sui_types::base_types::MoveObjectType;
use sui_types::type_input::TypeInput;

use crate::data::package_resolver::PackageResolver;
use crate::error::Error;

use super::open_move_type::MoveAbility;

#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct MoveType {
    pub native: TypeInput,
}

scalar!(
    MoveTypeSignature,
    "MoveTypeSignature",
    "The signature of a concrete Move Type (a type with all its type parameters instantiated with \
     concrete types, that contains no references), corresponding to the following recursive type:

type MoveTypeSignature =
    \"address\"
  | \"bool\"
  | \"u8\" | \"u16\" | ... | \"u256\"
  | { vector: MoveTypeSignature }
  | {
      datatype: {
        package: string,
        module: string,
        type: string,
        typeParameters: [MoveTypeSignature],
      }
    }"
);

scalar!(
    MoveTypeLayout,
    "MoveTypeLayout",
    "The shape of a concrete Move Type (a type with all its type parameters instantiated with \
     concrete types), corresponding to the following recursive type:

type MoveTypeLayout =
    \"address\"
  | \"bool\"
  | \"u8\" | \"u16\" | ... | \"u256\"
  | { vector: MoveTypeLayout }
  | {
      struct: {
        type: string,
        fields: [{ name: string, layout: MoveTypeLayout }],
      }
    }
  | { enum: [{
          type: string,
          variants: [{
              name: string,
              fields: [{ name: string, layout: MoveTypeLayout }],
          }]
      }]
  }"
);

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub(crate) enum MoveTypeSignature {
    Address,
    Bool,
    U8,
    U16,
    U32,
    U64,
    U128,
    U256,
    Vector(Box<MoveTypeSignature>),
    Datatype {
        package: String,
        module: String,
        #[serde(rename = "type")]
        type_: String,
        #[serde(rename = "typeParameters")]
        type_parameters: Vec<MoveTypeSignature>,
    },
}

#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) enum MoveTypeLayout {
    Address,
    Bool,
    U8,
    U16,
    U32,
    U64,
    U128,
    U256,
    Vector(Box<MoveTypeLayout>),
    Struct(MoveStructLayout),
    Enum(MoveEnumLayout),
}

#[derive(Serialize, Deserialize)]
pub(crate) struct MoveStructLayout {
    #[serde(rename = "type")]
    type_: String,
    fields: Vec<MoveFieldLayout>,
}

#[derive(Serialize, Deserialize)]
pub(crate) struct MoveEnumLayout {
    variants: Vec<MoveVariantLayout>,
}

#[derive(Serialize, Deserialize)]
pub(crate) struct MoveVariantLayout {
    name: String,
    layout: Vec<MoveFieldLayout>,
}

#[derive(Serialize, Deserialize)]
pub(crate) struct MoveFieldLayout {
    name: String,
    layout: MoveTypeLayout,
}

/// Represents concrete types (no type parameters, no references).
#[Object]
impl MoveType {
    /// Flat representation of the type signature, as a displayable string.
    async fn repr(&self) -> String {
        self.native.to_canonical_string(/* with_prefix */ true)
    }

    /// Structured representation of the type signature.
    async fn signature(&self) -> Result<MoveTypeSignature> {
        // Factor out into its own non-GraphQL, non-async function for better testability
        self.signature_impl().extend()
    }

    /// Structured representation of the "shape" of values that match this type. May return no
    /// layout if the type is invalid.
    async fn layout(&self, ctx: &Context<'_>) -> Result<Option<MoveTypeLayout>> {
        let resolver: &PackageResolver = ctx
            .data()
            .map_err(|_| Error::Internal("Unable to fetch Package Cache.".to_string()))
            .extend()?;

        let Some(layout) = self.layout_impl(resolver).await.extend()? else {
            return Ok(None);
        };

        Ok(Some(MoveTypeLayout::try_from(layout).extend()?))
    }

    /// The abilities this concrete type has. Returns no abilities if the type is invalid.
    async fn abilities(&self, ctx: &Context<'_>) -> Result<Option<Vec<MoveAbility>>> {
        let resolver: &PackageResolver = ctx
            .data()
            .map_err(|_| Error::Internal("Unable to fetch Package Cache.".to_string()))
            .extend()?;

        let Some(abilities) = self.abilities_impl(resolver).await.extend()? else {
            return Ok(None);
        };

        Ok(Some(abilities.into_iter().map(MoveAbility::from).collect()))
    }
}

impl MoveType {
    fn signature_impl(&self) -> Result<MoveTypeSignature, Error> {
        MoveTypeSignature::try_from(self.native.clone())
    }

    pub(crate) async fn layout_impl(
        &self,
        resolver: &PackageResolver,
    ) -> Result<Option<A::MoveTypeLayout>, Error> {
        let Ok(tag) = self.native.as_type_tag() else {
            return Ok(None);
        };

        Ok(Some(resolver.type_layout(tag).await.map_err(|e| {
            Error::Internal(format!(
                "Error calculating layout for {}: {e}",
                self.native.to_canonical_display(/* with_prefix */ true),
            ))
        })?))
    }

    pub(crate) async fn abilities_impl(
        &self,
        resolver: &PackageResolver,
    ) -> Result<Option<AbilitySet>, Error> {
        let Ok(tag) = self.native.as_type_tag() else {
            return Ok(None);
        };

        Ok(Some(resolver.abilities(tag).await.map_err(|e| {
            Error::Internal(format!(
                "Error calculating abilities for {}: {e}",
                self.native.to_canonical_string(/* with_prefix */ true),
            ))
        })?))
    }
}

impl From<MoveObjectType> for MoveType {
    fn from(obj: MoveObjectType) -> Self {
        let tag: TypeTag = obj.into();
        Self { native: tag.into() }
    }
}

impl From<TypeTag> for MoveType {
    fn from(tag: TypeTag) -> Self {
        Self { native: tag.into() }
    }
}

impl From<TypeInput> for MoveType {
    fn from(native: TypeInput) -> Self {
        Self { native }
    }
}

impl TryFrom<TypeInput> for MoveTypeSignature {
    type Error = Error;

    fn try_from(tag: TypeInput) -> Result<Self, Error> {
        use TypeInput as T;

        Ok(match tag {
            T::Signer => return Err(unexpected_signer_error()),

            T::U8 => Self::U8,
            T::U16 => Self::U16,
            T::U32 => Self::U32,
            T::U64 => Self::U64,
            T::U128 => Self::U128,
            T::U256 => Self::U256,

            T::Bool => Self::Bool,
            T::Address => Self::Address,

            T::Vector(v) => Self::Vector(Box::new(Self::try_from(*v)?)),

            T::Struct(s) => Self::Datatype {
                package: s.address.to_canonical_string(/* with_prefix */ true),
                module: s.module,
                type_: s.name,
                type_parameters: s
                    .type_params
                    .into_iter()
                    .map(Self::try_from)
                    .collect::<Result<Vec<_>, _>>()?,
            },
        })
    }
}

impl TryFrom<A::MoveTypeLayout> for MoveTypeLayout {
    type Error = Error;

    fn try_from(layout: A::MoveTypeLayout) -> Result<Self, Error> {
        use A::MoveTypeLayout as TL;

        Ok(match layout {
            TL::Signer => return Err(unexpected_signer_error()),

            TL::U8 => Self::U8,
            TL::U16 => Self::U16,
            TL::U32 => Self::U32,
            TL::U64 => Self::U64,
            TL::U128 => Self::U128,
            TL::U256 => Self::U256,

            TL::Bool => Self::Bool,
            TL::Address => Self::Address,

            TL::Vector(v) => Self::Vector(Box::new(Self::try_from(*v)?)),
            TL::Struct(s) => Self::Struct((*s).try_into()?),
            TL::Enum(e) => Self::Enum((*e).try_into()?),
        })
    }
}

impl TryFrom<A::MoveEnumLayout> for MoveEnumLayout {
    type Error = Error;

    fn try_from(layout: A::MoveEnumLayout) -> Result<Self, Error> {
        let A::MoveEnumLayout { variants, .. } = layout;
        let mut variant_layouts = Vec::new();
        for ((name, _), variant_fields) in variants {
            let mut field_layouts = Vec::new();
            for field in variant_fields {
                field_layouts.push(MoveFieldLayout::try_from(field)?);
            }
            variant_layouts.push(MoveVariantLayout {
                name: name.to_string(),
                layout: field_layouts,
            });
        }

        Ok(MoveEnumLayout {
            variants: variant_layouts,
        })
    }
}

impl TryFrom<A::MoveStructLayout> for MoveStructLayout {
    type Error = Error;

    fn try_from(layout: A::MoveStructLayout) -> Result<Self, Error> {
        Ok(Self {
            type_: layout.type_.to_canonical_string(/* with_prefix */ true),
            fields: layout
                .fields
                .into_iter()
                .map(MoveFieldLayout::try_from)
                .collect::<Result<_, _>>()?,
        })
    }
}

impl TryFrom<A::MoveFieldLayout> for MoveFieldLayout {
    type Error = Error;

    fn try_from(layout: A::MoveFieldLayout) -> Result<Self, Error> {
        Ok(Self {
            name: layout.name.to_string(),
            layout: layout.layout.try_into()?,
        })
    }
}

/// Error from seeing a `signer` value or type, which shouldn't be possible in Sui Move.
pub(crate) fn unexpected_signer_error() -> Error {
    Error::Internal("Unexpected value of type: signer.".to_string())
}

#[cfg(test)]
mod tests {
    use std::str::FromStr;

    use super::*;

    use expect_test::expect;

    fn signature(repr: impl Into<String>) -> Result<MoveTypeSignature, Error> {
        let tag = TypeTag::from_str(repr.into().as_str()).unwrap();
        MoveType::from(tag).signature_impl()
    }

    #[test]
    fn complex_type() {
        let sig = signature("vector<0x42::foo::Bar<address, u32, bool, u256>>").unwrap();
        let expect = expect![[r#"
            Vector(
                Datatype {
                    package: "0x0000000000000000000000000000000000000000000000000000000000000042",
                    module: "foo",
                    type_: "Bar",
                    type_parameters: [
                        Address,
                        U32,
                        Bool,
                        U256,
                    ],
                },
            )"#]];
        expect.assert_eq(&format!("{sig:#?}"));
    }

    #[test]
    fn signer_type() {
        let err = signature("signer").unwrap_err();
        let expect = expect![[r#"Internal("Unexpected value of type: signer.")"#]];
        expect.assert_eq(&format!("{err:?}"));
    }

    #[test]
    fn nested_signer_type() {
        let err = signature("0x42::baz::Qux<u32, vector<signer>>").unwrap_err();
        let expect = expect![[r#"Internal("Unexpected value of type: signer.")"#]];
        expect.assert_eq(&format!("{err:?}"));
    }
}