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