iota_types/stardust/output/
basic.rs1use move_core_types::{ident_str, identifier::IdentStr, language_storage::StructTag};
8use serde::{Deserialize, Serialize};
9use serde_with::serde_as;
10
11use super::unlock_conditions::{
12 ExpirationUnlockCondition, StorageDepositReturnUnlockCondition, TimelockUnlockCondition,
13};
14use crate::{
15 STARDUST_ADDRESS, TypeTag,
16 balance::Balance,
17 base_types::IotaAddress,
18 collection_types::Bag,
19 error::IotaError,
20 id::UID,
21 object::{Data, Object},
22};
23
24pub const BASIC_OUTPUT_MODULE_NAME: &IdentStr = ident_str!("basic_output");
25pub const BASIC_OUTPUT_STRUCT_NAME: &IdentStr = ident_str!("BasicOutput");
26
27#[serde_as]
29#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq)]
30pub struct BasicOutput {
31 pub id: UID,
33
34 pub balance: Balance,
36
37 pub native_tokens: Bag,
41
42 pub storage_deposit_return: Option<StorageDepositReturnUnlockCondition>,
44 pub timelock: Option<TimelockUnlockCondition>,
46 pub expiration: Option<ExpirationUnlockCondition>,
48
49 pub metadata: Option<Vec<u8>>,
53 pub tag: Option<Vec<u8>>,
55 pub sender: Option<IotaAddress>,
57}
58
59impl BasicOutput {
60 pub fn tag(type_param: TypeTag) -> StructTag {
62 StructTag {
63 address: STARDUST_ADDRESS,
64 module: BASIC_OUTPUT_MODULE_NAME.to_owned(),
65 name: BASIC_OUTPUT_STRUCT_NAME.to_owned(),
66 type_params: vec![type_param],
67 }
68 }
69
70 pub fn from_bcs_bytes(content: &[u8]) -> Result<Self, IotaError> {
72 bcs::from_bytes(content).map_err(|err| IotaError::ObjectDeserialization {
73 error: format!("Unable to deserialize BasicOutput object: {err:?}"),
74 })
75 }
76
77 pub fn is_basic_output(s: &StructTag) -> bool {
79 s.address == STARDUST_ADDRESS
80 && s.module.as_ident_str() == BASIC_OUTPUT_MODULE_NAME
81 && s.name.as_ident_str() == BASIC_OUTPUT_STRUCT_NAME
82 }
83}
84
85impl TryFrom<&Object> for BasicOutput {
86 type Error = IotaError;
87 fn try_from(object: &Object) -> Result<Self, Self::Error> {
88 match &object.data {
89 Data::Move(o) => {
90 if o.type_().is_basic_output() {
91 return BasicOutput::from_bcs_bytes(o.contents());
92 }
93 }
94 Data::Package(_) => {}
95 }
96
97 Err(IotaError::Type {
98 error: format!("Object type is not a BasicOutput: {object:?}"),
99 })
100 }
101}