iota_types/
coin_manager.rs1use iota_sdk_types::Identifier;
5use serde::{Deserialize, Serialize};
6
7use crate::{
8 IotaAddress, StructTag,
9 coin::{CoinMetadata, TreasuryCap},
10 error::IotaError,
11 id::UID,
12 object::{Data, Object},
13};
14
15pub const COIN_MANAGER_TREASURY_CAP_STRUCT_NAME: Identifier =
16 Identifier::from_static("CoinManagerTreasuryCap");
17
18#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq)]
25pub struct CoinManager {
26 pub id: UID,
28 pub treasury_cap: TreasuryCap,
30 pub metadata: Option<CoinMetadata>,
32 pub immutable_metadata: Option<ImmutableCoinMetadata>,
35 pub maximum_supply: Option<u64>,
38 pub supply_immutable: bool,
41 pub metadata_immutable: bool,
44}
45
46impl CoinManager {
47 pub fn from_bcs_bytes(content: &[u8]) -> Result<Self, IotaError> {
48 bcs::from_bytes(content).map_err(|err| IotaError::ObjectDeserialization {
49 error: format!("Unable to deserialize CoinManager object: {err}"),
50 })
51 }
52}
53
54#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq)]
57pub struct ImmutableCoinMetadata {
58 pub decimals: u8,
60 pub name: String,
62 pub symbol: String,
64 pub description: String,
66 pub icon_url: Option<String>,
68}
69
70#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq)]
73pub struct CoinManagerTreasuryCap {
74 pub id: UID,
76}
77
78impl CoinManagerTreasuryCap {
79 pub fn is_coin_manager_treasury_cap(object_type: &StructTag) -> bool {
80 object_type.address() == IotaAddress::FRAMEWORK
81 && object_type.module() == &Identifier::COIN_MANAGER_MODULE
82 && object_type.name() == &COIN_MANAGER_TREASURY_CAP_STRUCT_NAME
83 }
84}
85
86impl TryFrom<Object> for CoinManager {
87 type Error = IotaError;
88 fn try_from(object: Object) -> Result<Self, Self::Error> {
89 TryFrom::try_from(&object)
90 }
91}
92
93impl TryFrom<&Object> for CoinManager {
94 type Error = IotaError;
95 fn try_from(object: &Object) -> Result<Self, Self::Error> {
96 if let Data::Struct(o) = &object.data {
97 if o.struct_tag().is_coin_manager() {
98 return CoinManager::from_bcs_bytes(o.contents());
99 }
100 }
101
102 Err(IotaError::Type {
103 error: format!("Object type is not a CoinManager: {object:?}"),
104 })
105 }
106}