chia_sdk_driver/primitives/option/
option_info.rs1use chia_protocol::Bytes32;
2use chia_sdk_types::{Mod, puzzles::OptionContractArgs};
3use clvm_utils::{ToTreeHash, TreeHash};
4use clvmr::Allocator;
5
6use crate::{DriverError, Layer, OptionContractLayer, Puzzle, SingletonInfo, SingletonLayer};
7
8pub type OptionContractLayers<I> = SingletonLayer<OptionContractLayer<I>>;
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
11pub struct OptionInfo {
12 pub launcher_id: Bytes32,
13 pub underlying_coin_id: Bytes32,
14 pub underlying_delegated_puzzle_hash: Bytes32,
15 pub p2_puzzle_hash: Bytes32,
16}
17
18impl OptionInfo {
19 pub fn new(
20 launcher_id: Bytes32,
21 underlying_coin_id: Bytes32,
22 underlying_delegated_puzzle_hash: Bytes32,
23 p2_puzzle_hash: Bytes32,
24 ) -> Self {
25 Self {
26 launcher_id,
27 underlying_coin_id,
28 underlying_delegated_puzzle_hash,
29 p2_puzzle_hash,
30 }
31 }
32
33 pub fn parse(
34 allocator: &Allocator,
35 puzzle: Puzzle,
36 ) -> Result<Option<(Self, Puzzle)>, DriverError> {
37 let Some(layers) = OptionContractLayers::parse_puzzle(allocator, puzzle)? else {
38 return Ok(None);
39 };
40
41 let p2_puzzle = layers.inner_puzzle.inner_puzzle;
42
43 Ok(Some((Self::from_layers(&layers), p2_puzzle)))
44 }
45
46 pub fn from_layers<I>(layers: &OptionContractLayers<I>) -> Self
47 where
48 I: ToTreeHash,
49 {
50 Self {
51 launcher_id: layers.launcher_id,
52 underlying_coin_id: layers.inner_puzzle.underlying_coin_id,
53 underlying_delegated_puzzle_hash: layers.inner_puzzle.underlying_delegated_puzzle_hash,
54 p2_puzzle_hash: layers.inner_puzzle.inner_puzzle.tree_hash().into(),
55 }
56 }
57
58 #[must_use]
59 pub fn into_layers<I>(self, p2_puzzle: I) -> OptionContractLayers<I> {
60 SingletonLayer::new(
61 self.launcher_id,
62 OptionContractLayer::new(
63 self.underlying_coin_id,
64 self.underlying_delegated_puzzle_hash,
65 p2_puzzle,
66 ),
67 )
68 }
69}
70
71impl SingletonInfo for OptionInfo {
72 fn launcher_id(&self) -> Bytes32 {
73 self.launcher_id
74 }
75
76 fn inner_puzzle_hash(&self) -> TreeHash {
77 OptionContractArgs::new(
78 self.underlying_coin_id,
79 self.underlying_delegated_puzzle_hash,
80 TreeHash::from(self.p2_puzzle_hash),
81 )
82 .curry_tree_hash()
83 }
84}