Skip to main content

chia_sdk_driver/offers/
asset_info.rs

1use std::collections::HashMap;
2
3use chia_protocol::Bytes32;
4
5use crate::{DriverError, HashedPtr};
6
7#[derive(Debug, Default, Clone)]
8pub struct AssetInfo {
9    cats: HashMap<Bytes32, CatAssetInfo>,
10    nfts: HashMap<Bytes32, NftAssetInfo>,
11    options: HashMap<Bytes32, OptionAssetInfo>,
12}
13
14impl AssetInfo {
15    pub fn new() -> Self {
16        Self::default()
17    }
18
19    pub fn cats(&self) -> impl Iterator<Item = &Bytes32> {
20        self.cats.keys()
21    }
22
23    pub fn nfts(&self) -> impl Iterator<Item = &Bytes32> {
24        self.nfts.keys()
25    }
26
27    pub fn options(&self) -> impl Iterator<Item = &Bytes32> {
28        self.options.keys()
29    }
30
31    pub fn cat(&self, asset_id: Bytes32) -> Option<&CatAssetInfo> {
32        self.cats.get(&asset_id)
33    }
34
35    pub fn nft(&self, launcher_id: Bytes32) -> Option<&NftAssetInfo> {
36        self.nfts.get(&launcher_id)
37    }
38
39    pub fn option(&self, launcher_id: Bytes32) -> Option<&OptionAssetInfo> {
40        self.options.get(&launcher_id)
41    }
42
43    pub fn insert_cat(&mut self, asset_id: Bytes32, info: CatAssetInfo) -> Result<(), DriverError> {
44        if self
45            .cats
46            .insert(asset_id, info)
47            .is_some_and(|existing| existing != info)
48        {
49            return Err(DriverError::IncompatibleAssetInfo);
50        }
51
52        Ok(())
53    }
54
55    pub fn insert_nft(
56        &mut self,
57        launcher_id: Bytes32,
58        info: NftAssetInfo,
59    ) -> Result<(), DriverError> {
60        if self
61            .nfts
62            .insert(launcher_id, info)
63            .is_some_and(|existing| existing != info)
64        {
65            return Err(DriverError::IncompatibleAssetInfo);
66        }
67
68        Ok(())
69    }
70
71    pub fn insert_option(
72        &mut self,
73        launcher_id: Bytes32,
74        info: OptionAssetInfo,
75    ) -> Result<(), DriverError> {
76        if self
77            .options
78            .insert(launcher_id, info)
79            .is_some_and(|existing| existing != info)
80        {
81            return Err(DriverError::IncompatibleAssetInfo);
82        }
83
84        Ok(())
85    }
86
87    pub fn extend(&mut self, other: Self) -> Result<(), DriverError> {
88        for (asset_id, asset_info) in other.cats {
89            self.insert_cat(asset_id, asset_info)?;
90        }
91
92        for (launcher_id, asset_info) in other.nfts {
93            self.insert_nft(launcher_id, asset_info)?;
94        }
95
96        for (launcher_id, asset_info) in other.options {
97            self.insert_option(launcher_id, asset_info)?;
98        }
99
100        Ok(())
101    }
102}
103
104#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
105pub struct CatAssetInfo {
106    pub hidden_puzzle_hash: Option<Bytes32>,
107}
108
109impl CatAssetInfo {
110    pub fn new(hidden_puzzle_hash: Option<Bytes32>) -> Self {
111        Self { hidden_puzzle_hash }
112    }
113}
114
115#[derive(Debug, Clone, Copy, PartialEq, Eq)]
116pub struct NftAssetInfo {
117    pub metadata: HashedPtr,
118    pub metadata_updater_puzzle_hash: Bytes32,
119    pub royalty_puzzle_hash: Bytes32,
120    pub royalty_basis_points: u16,
121}
122
123impl NftAssetInfo {
124    pub fn new(
125        metadata: HashedPtr,
126        metadata_updater_puzzle_hash: Bytes32,
127        royalty_puzzle_hash: Bytes32,
128        royalty_basis_points: u16,
129    ) -> Self {
130        Self {
131            metadata,
132            metadata_updater_puzzle_hash,
133            royalty_puzzle_hash,
134            royalty_basis_points,
135        }
136    }
137}
138
139#[derive(Debug, Clone, Copy, PartialEq, Eq)]
140pub struct OptionAssetInfo {
141    pub underlying_coin_id: Bytes32,
142    pub underlying_delegated_puzzle_hash: Bytes32,
143}
144
145impl OptionAssetInfo {
146    pub fn new(underlying_coin_id: Bytes32, underlying_delegated_puzzle_hash: Bytes32) -> Self {
147        Self {
148            underlying_coin_id,
149            underlying_delegated_puzzle_hash,
150        }
151    }
152}