Skip to main content

chia_sdk_driver/layers/action_layer/actions/xchandles/
action_log.rs

1use chia_protocol::{Bytes32, Coin};
2use chia_puzzle_types::singleton::SingletonArgs;
3use chia_sdk_types::puzzles::{
4    CompactCoinProof, XchandlesHandleSlotValue, XchandlesPricingSolution, XchandlesUpdateSlotValue,
5};
6use clvm_traits::FromClvm;
7use clvmr::NodePtr;
8
9use crate::{DriverError, SpendContext, XchandlesRegistryState};
10
11#[derive(Debug, Clone, PartialEq, Eq)]
12pub struct XchandlesPrecommitValueLog {
13    pub cat_maker_hash: Bytes32,
14    pub pricing_puzzle_hash: Bytes32,
15    pub pricing_solution: XchandlesPricingSolution,
16    pub handle: String,
17    pub secret: Bytes32,
18    pub owner_launcher_id: Bytes32,
19    pub resolved_launcher_id: Bytes32,
20}
21
22impl XchandlesPrecommitValueLog {
23    #[allow(clippy::too_many_arguments)]
24    pub fn new(
25        cat_maker_hash: Bytes32,
26        pricing_puzzle_hash: Bytes32,
27        pricing_solution: XchandlesPricingSolution,
28        handle: String,
29        secret: Bytes32,
30        owner_launcher_id: Bytes32,
31        resolved_launcher_id: Bytes32,
32    ) -> Self {
33        Self {
34            cat_maker_hash,
35            pricing_puzzle_hash,
36            pricing_solution,
37            handle,
38            secret,
39            owner_launcher_id,
40            resolved_launcher_id,
41        }
42    }
43}
44
45#[derive(FromClvm, Debug, Clone, Copy, PartialEq, Eq)]
46#[clvm(list)]
47struct XchandlesPricingOutput {
48    total_price: u64,
49    #[clvm(rest)]
50    registered_time: u64,
51}
52
53pub fn run_pricing_output(
54    ctx: &mut SpendContext,
55    puzzle: NodePtr,
56    solution: NodePtr,
57) -> Result<(u64, u64), DriverError> {
58    let output = ctx.run(puzzle, solution)?;
59    let output = ctx.extract::<XchandlesPricingOutput>(output)?;
60    Ok((output.total_price, output.registered_time))
61}
62
63pub fn coin_id_from_owner_proof(proof: CompactCoinProof, owner_launcher_id: Bytes32) -> Bytes32 {
64    Coin::new(
65        proof.parent_coin_info,
66        SingletonArgs::curry_tree_hash(owner_launcher_id, proof.inner_puzzle_hash.into()).into(),
67        proof.amount,
68    )
69    .coin_id()
70}
71
72#[derive(Debug, Clone, PartialEq, Eq)]
73pub struct XchandlesOracleActionLog {
74    pub spent_slot: XchandlesHandleSlotValue,
75    pub created_slot: XchandlesHandleSlotValue,
76}
77
78impl XchandlesOracleActionLog {
79    pub fn extend_spent_handle_slots(&self, out: &mut Vec<XchandlesHandleSlotValue>) {
80        out.push(self.spent_slot);
81    }
82
83    pub fn extend_created_handle_slots(&self, out: &mut Vec<XchandlesHandleSlotValue>) {
84        out.push(self.created_slot);
85    }
86}
87
88#[derive(Debug, Clone, PartialEq, Eq)]
89pub struct XchandlesExtendActionLog {
90    pub spent_slot: XchandlesHandleSlotValue,
91    pub created_slot: XchandlesHandleSlotValue,
92    pub total_price: u64,
93    pub registered_time: u64,
94}
95
96impl XchandlesExtendActionLog {
97    pub fn extend_spent_handle_slots(&self, out: &mut Vec<XchandlesHandleSlotValue>) {
98        out.push(self.spent_slot);
99    }
100
101    pub fn extend_created_handle_slots(&self, out: &mut Vec<XchandlesHandleSlotValue>) {
102        out.push(self.created_slot);
103    }
104}
105
106#[derive(Debug, Clone, PartialEq, Eq)]
107pub struct XchandlesExpireActionLog {
108    pub spent_slot: XchandlesHandleSlotValue,
109    pub created_slot: XchandlesHandleSlotValue,
110    pub precommit_value: XchandlesPrecommitValueLog,
111    pub total_price: u64,
112    pub registered_time: u64,
113    pub owner_full_puzzle_hash: Bytes32,
114    pub resolved_full_puzzle_hash: Option<Bytes32>,
115    pub owner_inner_puzzle_hash: Bytes32,
116    pub resolved_inner_puzzle_hash: Bytes32,
117}
118
119impl XchandlesExpireActionLog {
120    pub fn extend_spent_handle_slots(&self, out: &mut Vec<XchandlesHandleSlotValue>) {
121        out.push(self.spent_slot);
122    }
123
124    pub fn extend_created_handle_slots(&self, out: &mut Vec<XchandlesHandleSlotValue>) {
125        out.push(self.created_slot);
126    }
127}
128
129#[derive(Debug, Clone, PartialEq, Eq)]
130pub struct XchandlesInitiateUpdateActionLog {
131    pub spent_slot: XchandlesHandleSlotValue,
132    pub created_handle_slot: XchandlesHandleSlotValue,
133    pub created_update_slot: XchandlesUpdateSlotValue,
134    pub initiator_coin_id: Bytes32,
135}
136
137impl XchandlesInitiateUpdateActionLog {
138    pub fn extend_spent_handle_slots(&self, out: &mut Vec<XchandlesHandleSlotValue>) {
139        out.push(self.spent_slot);
140    }
141
142    pub fn extend_created_handle_slots(&self, out: &mut Vec<XchandlesHandleSlotValue>) {
143        out.push(self.created_handle_slot);
144    }
145
146    pub fn extend_created_update_slots(&self, out: &mut Vec<XchandlesUpdateSlotValue>) {
147        out.push(self.created_update_slot);
148    }
149}
150
151#[derive(Debug, Clone, PartialEq, Eq)]
152pub struct XchandlesExecuteUpdateActionLog {
153    pub spent_handle_slot: XchandlesHandleSlotValue,
154    pub spent_update_slot: XchandlesUpdateSlotValue,
155    pub created_slot: XchandlesHandleSlotValue,
156    pub owner_coin_id: Bytes32,
157    pub owner_full_puzzle_hash: Bytes32,
158    pub resolved_full_puzzle_hash: Option<Bytes32>,
159    pub owner_inner_puzzle_hash: Bytes32,
160    pub resolved_inner_puzzle_hash: Bytes32,
161}
162
163impl XchandlesExecuteUpdateActionLog {
164    pub fn extend_spent_handle_slots(&self, out: &mut Vec<XchandlesHandleSlotValue>) {
165        out.push(self.spent_handle_slot);
166    }
167
168    pub fn extend_spent_update_slots(&self, out: &mut Vec<XchandlesUpdateSlotValue>) {
169        out.push(self.spent_update_slot);
170    }
171
172    pub fn extend_created_handle_slots(&self, out: &mut Vec<XchandlesHandleSlotValue>) {
173        out.push(self.created_slot);
174    }
175}
176
177#[derive(Debug, Clone, PartialEq, Eq)]
178pub struct XchandlesRefundActionLog {
179    pub spent_slot: Option<XchandlesHandleSlotValue>,
180    pub created_slot: Option<XchandlesHandleSlotValue>,
181    pub precommit_value: XchandlesPrecommitValueLog,
182    pub precommitted_total_price: u64,
183    pub precommitted_registered_time: u64,
184}
185
186impl XchandlesRefundActionLog {
187    pub fn extend_spent_handle_slots(&self, out: &mut Vec<XchandlesHandleSlotValue>) {
188        if let Some(slot) = self.spent_slot {
189            out.push(slot);
190        }
191    }
192
193    pub fn extend_created_handle_slots(&self, out: &mut Vec<XchandlesHandleSlotValue>) {
194        if let Some(slot) = self.created_slot {
195            out.push(slot);
196        }
197    }
198}
199
200#[derive(Debug, Clone, PartialEq, Eq)]
201pub struct XchandlesRegisterActionLog {
202    pub spent_left_slot: XchandlesHandleSlotValue,
203    pub spent_right_slot: XchandlesHandleSlotValue,
204    pub created_left_slot: XchandlesHandleSlotValue,
205    pub created_handle_slot: XchandlesHandleSlotValue,
206    pub created_right_slot: XchandlesHandleSlotValue,
207    /// Complete revealed precommit (Handle string + registration secret + pricing).
208    pub precommit_value: XchandlesPrecommitValueLog,
209    pub total_price: u64,
210    pub registered_time: u64,
211    pub owner_full_puzzle_hash: Bytes32,
212    pub resolved_full_puzzle_hash: Option<Bytes32>,
213    pub owner_inner_puzzle_hash: Bytes32,
214    pub resolved_inner_puzzle_hash: Bytes32,
215}
216
217impl XchandlesRegisterActionLog {
218    pub fn extend_spent_handle_slots(&self, out: &mut Vec<XchandlesHandleSlotValue>) {
219        out.push(self.spent_left_slot);
220        out.push(self.spent_right_slot);
221    }
222
223    pub fn extend_created_handle_slots(&self, out: &mut Vec<XchandlesHandleSlotValue>) {
224        out.push(self.created_left_slot);
225        out.push(self.created_handle_slot);
226        out.push(self.created_right_slot);
227    }
228}
229
230#[derive(Debug, Clone, Copy, PartialEq, Eq)]
231pub struct XchandlesDelegatedStateActionLog {
232    pub old_state: XchandlesRegistryState,
233    pub new_state: XchandlesRegistryState,
234}
235
236impl XchandlesDelegatedStateActionLog {}
237
238#[derive(Debug, Clone, PartialEq, Eq)]
239#[allow(clippy::large_enum_variant)]
240pub enum XchandlesActionLog {
241    Oracle(XchandlesOracleActionLog),
242    Extend(XchandlesExtendActionLog),
243    Expire(XchandlesExpireActionLog),
244    InitiateUpdate(XchandlesInitiateUpdateActionLog),
245    ExecuteUpdate(XchandlesExecuteUpdateActionLog),
246    Refund(XchandlesRefundActionLog),
247    Register(XchandlesRegisterActionLog),
248    DelegatedState(XchandlesDelegatedStateActionLog),
249}
250
251impl XchandlesActionLog {
252    pub fn extend_spent_handle_slots(&self, out: &mut Vec<XchandlesHandleSlotValue>) {
253        match self {
254            Self::Oracle(log) => log.extend_spent_handle_slots(out),
255            Self::Extend(log) => log.extend_spent_handle_slots(out),
256            Self::Expire(log) => log.extend_spent_handle_slots(out),
257            Self::InitiateUpdate(log) => log.extend_spent_handle_slots(out),
258            Self::ExecuteUpdate(log) => log.extend_spent_handle_slots(out),
259            Self::Refund(log) => log.extend_spent_handle_slots(out),
260            Self::Register(log) => log.extend_spent_handle_slots(out),
261            Self::DelegatedState(_log) => {} // no slots spent
262        }
263    }
264
265    pub fn extend_created_handle_slots(&self, out: &mut Vec<XchandlesHandleSlotValue>) {
266        match self {
267            Self::Oracle(log) => log.extend_created_handle_slots(out),
268            Self::Extend(log) => log.extend_created_handle_slots(out),
269            Self::Expire(log) => log.extend_created_handle_slots(out),
270            Self::InitiateUpdate(log) => log.extend_created_handle_slots(out),
271            Self::ExecuteUpdate(log) => log.extend_created_handle_slots(out),
272            Self::Refund(log) => log.extend_created_handle_slots(out),
273            Self::Register(log) => log.extend_created_handle_slots(out),
274            Self::DelegatedState(_log) => {} // no slots created
275        }
276    }
277
278    pub fn extend_spent_update_slots(&self, out: &mut Vec<XchandlesUpdateSlotValue>) {
279        if let Self::ExecuteUpdate(log) = self {
280            log.extend_spent_update_slots(out);
281        }
282    }
283
284    pub fn extend_created_update_slots(&self, out: &mut Vec<XchandlesUpdateSlotValue>) {
285        if let Self::InitiateUpdate(log) = self {
286            log.extend_created_update_slots(out);
287        }
288    }
289}