chia_sdk_driver/action_system/
asset.rs1use chia_protocol::{Bytes32, Coin};
2use chia_puzzles::SETTLEMENT_PAYMENT_HASH;
3
4use crate::{Cat, Did, Nft, OptionContract, OutputConstraints};
5
6pub trait Asset {
7 fn coin_id(&self) -> Bytes32;
8 fn full_puzzle_hash(&self) -> Bytes32;
9 fn p2_puzzle_hash(&self) -> Bytes32;
10 fn amount(&self) -> u64;
11 fn constraints(&self) -> OutputConstraints;
12}
13
14impl Asset for Coin {
15 fn coin_id(&self) -> Bytes32 {
16 self.coin_id()
17 }
18
19 fn full_puzzle_hash(&self) -> Bytes32 {
20 self.puzzle_hash
21 }
22
23 fn p2_puzzle_hash(&self) -> Bytes32 {
24 self.puzzle_hash
25 }
26
27 fn amount(&self) -> u64 {
28 self.amount
29 }
30
31 fn constraints(&self) -> OutputConstraints {
32 OutputConstraints {
33 singleton: false,
34 settlement: self.puzzle_hash == SETTLEMENT_PAYMENT_HASH.into(),
35 }
36 }
37}
38
39impl Asset for Cat {
40 fn coin_id(&self) -> Bytes32 {
41 self.coin.coin_id()
42 }
43
44 fn full_puzzle_hash(&self) -> Bytes32 {
45 self.coin.puzzle_hash
46 }
47
48 fn p2_puzzle_hash(&self) -> Bytes32 {
49 self.info.p2_puzzle_hash
50 }
51
52 fn amount(&self) -> u64 {
53 self.coin.amount
54 }
55
56 fn constraints(&self) -> OutputConstraints {
57 OutputConstraints {
58 singleton: false,
59 settlement: self.info.p2_puzzle_hash == SETTLEMENT_PAYMENT_HASH.into(),
60 }
61 }
62}
63
64impl Asset for Did {
65 fn coin_id(&self) -> Bytes32 {
66 self.coin.coin_id()
67 }
68
69 fn full_puzzle_hash(&self) -> Bytes32 {
70 self.coin.puzzle_hash
71 }
72
73 fn p2_puzzle_hash(&self) -> Bytes32 {
74 self.info.p2_puzzle_hash
75 }
76
77 fn amount(&self) -> u64 {
78 self.coin.amount
79 }
80
81 fn constraints(&self) -> OutputConstraints {
82 OutputConstraints {
83 singleton: true,
84 settlement: self.info.p2_puzzle_hash == SETTLEMENT_PAYMENT_HASH.into(),
85 }
86 }
87}
88
89impl Asset for Nft {
90 fn coin_id(&self) -> Bytes32 {
91 self.coin.coin_id()
92 }
93
94 fn full_puzzle_hash(&self) -> Bytes32 {
95 self.coin.puzzle_hash
96 }
97
98 fn p2_puzzle_hash(&self) -> Bytes32 {
99 self.info.p2_puzzle_hash
100 }
101
102 fn amount(&self) -> u64 {
103 self.coin.amount
104 }
105
106 fn constraints(&self) -> OutputConstraints {
107 OutputConstraints {
108 singleton: true,
109 settlement: self.info.p2_puzzle_hash == SETTLEMENT_PAYMENT_HASH.into(),
110 }
111 }
112}
113
114impl Asset for OptionContract {
115 fn coin_id(&self) -> Bytes32 {
116 self.coin.coin_id()
117 }
118
119 fn full_puzzle_hash(&self) -> Bytes32 {
120 self.coin.puzzle_hash
121 }
122
123 fn p2_puzzle_hash(&self) -> Bytes32 {
124 self.info.p2_puzzle_hash
125 }
126
127 fn amount(&self) -> u64 {
128 self.coin.amount
129 }
130
131 fn constraints(&self) -> OutputConstraints {
132 OutputConstraints {
133 singleton: true,
134 settlement: self.info.p2_puzzle_hash == SETTLEMENT_PAYMENT_HASH.into(),
135 }
136 }
137}