iota_graphql_rpc/types/
object_change.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::effects::{IDOperation, ObjectChange as NativeObjectChange};
7
8use crate::types::{iota_address::IotaAddress, object::Object};
9
10pub(crate) struct ObjectChange {
11    pub native: NativeObjectChange,
12    /// The checkpoint sequence number this was viewed at.
13    pub checkpoint_viewed_at: u64,
14}
15
16/// Effect on an individual Object (keyed by its ID).
17#[Object]
18impl ObjectChange {
19    /// The address of the object that has changed.
20    async fn address(&self) -> IotaAddress {
21        self.native.id.into()
22    }
23
24    /// The contents of the object immediately before the transaction.
25    async fn input_state(&self, ctx: &Context<'_>) -> Result<Option<Object>> {
26        let Some(version) = self.native.input_version else {
27            return Ok(None);
28        };
29
30        Object::query(
31            ctx,
32            self.native.id.into(),
33            Object::at_version(version.value(), self.checkpoint_viewed_at),
34        )
35        .await
36        .extend()
37    }
38
39    /// The contents of the object immediately after the transaction.
40    async fn output_state(&self, ctx: &Context<'_>) -> Result<Option<Object>> {
41        let Some(version) = self.native.output_version else {
42            return Ok(None);
43        };
44
45        Object::query(
46            ctx,
47            self.native.id.into(),
48            Object::at_version(version.value(), self.checkpoint_viewed_at),
49        )
50        .await
51        .extend()
52    }
53
54    /// Whether the ID was created in this transaction.
55    async fn id_created(&self) -> Option<bool> {
56        Some(self.native.id_operation == IDOperation::Created)
57    }
58
59    /// Whether the ID was deleted in this transaction.
60    async fn id_deleted(&self) -> Option<bool> {
61        Some(self.native.id_operation == IDOperation::Deleted)
62    }
63}