Skip to main content

iota_types/stardust/output/
basic.rs

1// Copyright (c) 2024 IOTA Stiftung
2// SPDX-License-Identifier: Apache-2.0
3
4//! Rust types and logic for the Move counterparts in the `stardust` system
5//! package.
6
7use anyhow::Result;
8use iota_sdk_move_types::stardust::{
9    expiration_unlock_condition::ExpirationUnlockCondition,
10    storage_deposit_return_unlock_condition::StorageDepositReturnUnlockCondition,
11    timelock_unlock_condition::TimelockUnlockCondition,
12};
13use iota_sdk_types::{Address, Identifier, StructTag};
14use serde::{Deserialize, Serialize};
15use serde_with::serde_as;
16
17use crate::{balance::Balance, collection_types::Bag, error::IotaError, id::UID, object::Object};
18
19pub const BASIC_OUTPUT_MODULE_NAME: Identifier = Identifier::from_static("basic_output");
20pub const BASIC_OUTPUT_STRUCT_NAME: Identifier = Identifier::from_static("BasicOutput");
21
22/// Rust version of the stardust basic output.
23#[serde_as]
24#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq)]
25pub struct BasicOutput {
26    /// Hash of the `OutputId` that was migrated.
27    pub id: UID,
28
29    /// The amount of coins held by the output.
30    pub balance: Balance,
31
32    /// The `Bag` holds native tokens, key-ed by the stringified type of the
33    /// asset. Example: key: "0xabcded::soon::SOON", value:
34    /// Balance<0xabcded::soon::SOON>.
35    pub native_tokens: Bag,
36
37    /// The storage deposit return unlock condition.
38    pub storage_deposit_return: Option<StorageDepositReturnUnlockCondition>,
39    /// The timelock unlock condition.
40    pub timelock: Option<TimelockUnlockCondition>,
41    /// The expiration unlock condition.
42    pub expiration: Option<ExpirationUnlockCondition>,
43
44    // Possible features, they have no effect and only here to hold data until the object is
45    // deleted.
46    /// The metadata feature.
47    pub metadata: Option<Vec<u8>>,
48    /// The tag feature.
49    pub tag: Option<Vec<u8>>,
50    /// The sender feature.
51    pub sender: Option<Address>,
52}
53
54impl BasicOutput {
55    /// Create a `BasicOutput` from BCS bytes.
56    pub fn from_bcs_bytes(content: &[u8]) -> Result<Self, IotaError> {
57        bcs::from_bytes(content).map_err(|err| IotaError::ObjectDeserialization {
58            error: format!("Unable to deserialize BasicOutput object: {err:?}"),
59        })
60    }
61
62    /// Whether the given `StructTag` represents a `BasicOutput`.
63    pub fn is_basic_output(s: &StructTag) -> bool {
64        s.address() == Address::STARDUST
65            && s.module() == &BASIC_OUTPUT_MODULE_NAME
66            && s.name() == &BASIC_OUTPUT_STRUCT_NAME
67    }
68}
69
70impl TryFrom<&Object> for BasicOutput {
71    type Error = IotaError;
72    fn try_from(object: &Object) -> Result<Self, Self::Error> {
73        if let Some(o) = object.data.as_opt_struct() {
74            if BasicOutput::is_basic_output(o.struct_tag()) {
75                return BasicOutput::from_bcs_bytes(o.contents());
76            }
77        }
78
79        Err(IotaError::Type {
80            error: format!("Object type is not a BasicOutput: {object:?}"),
81        })
82    }
83}