iota_graphql_rpc/types/
object_read.rs

1// Copyright (c) Mysten Labs, Inc.
2// Modifications Copyright (c) 2024 IOTA Stiftung
3// SPDX-License-Identifier: Apache-2.0
4
5use async_graphql::*;
6use iota_types::base_types::ObjectRef as NativeObjectRef;
7
8use crate::types::{iota_address::IotaAddress, object::Object, uint53::UInt53};
9
10// A helper type representing the read of a specific version of an object.
11// Intended to be "flattened" into other GraphQL types.
12#[derive(Clone, Eq, PartialEq)]
13pub(crate) struct ObjectRead {
14    pub native: NativeObjectRef,
15    /// The checkpoint sequence number this was viewed at.
16    pub checkpoint_viewed_at: u64,
17}
18
19#[Object]
20impl ObjectRead {
21    /// ID of the object being read.
22    async fn address(&self) -> IotaAddress {
23        self.address_impl()
24    }
25
26    /// Version of the object being read.
27    async fn version(&self) -> UInt53 {
28        self.version_impl().into()
29    }
30
31    /// 32-byte hash that identifies the object's contents at this version,
32    /// encoded as a Base58 string.
33    async fn digest(&self) -> String {
34        self.native.2.base58_encode()
35    }
36
37    /// The object at this version.  May not be available due to pruning.
38    async fn object(&self, ctx: &Context<'_>) -> Result<Option<Object>> {
39        Object::query(
40            ctx,
41            self.address_impl(),
42            Object::at_version(self.version_impl(), self.checkpoint_viewed_at),
43        )
44        .await
45        .extend()
46    }
47}
48
49impl ObjectRead {
50    fn address_impl(&self) -> IotaAddress {
51        IotaAddress::from(self.native.0)
52    }
53
54    fn version_impl(&self) -> u64 {
55        self.native.1.value()
56    }
57}