chia_sdk_driver/layers/action_layer/
conditions_layer.rs1use chia_sdk_types::{Condition, Conditions};
2use clvm_traits::{clvm_quote, match_quote, FromClvm, ToClvm};
3use clvmr::{Allocator, NodePtr};
4
5use crate::{DriverError, Layer, Puzzle, Spend, SpendContext};
6
7#[derive(Debug, Clone, PartialEq, Eq)]
9pub struct ConditionsLayer<T = NodePtr> {
10 pub conditions: Conditions<T>,
11}
12
13impl<T> ConditionsLayer<T> {
14 pub fn new(conditions: Conditions<T>) -> Self {
15 Self { conditions }
16 }
17}
18
19impl<T> Layer for ConditionsLayer<T>
20where
21 T: FromClvm<Allocator> + ToClvm<Allocator> + Clone,
22{
23 type Solution = ();
24
25 fn parse_puzzle(allocator: &Allocator, puzzle: Puzzle) -> Result<Option<Self>, DriverError> {
26 let Some(puzzle) = puzzle.as_raw() else {
27 return Ok(None);
28 };
29
30 let (_q, conditions) = <match_quote!(Vec<Condition<T>>)>::from_clvm(allocator, puzzle.ptr)?;
31
32 Ok(Some(Self::new(
33 Conditions::<T>::default().extend(conditions),
34 )))
35 }
36
37 fn parse_solution(_: &Allocator, _: NodePtr) -> Result<Self::Solution, DriverError> {
38 Ok(())
39 }
40
41 fn construct_puzzle(&self, ctx: &mut SpendContext) -> Result<NodePtr, DriverError> {
42 Ok(clvm_quote!(self.conditions.clone()).to_clvm(ctx)?)
43 }
44
45 fn construct_solution(
46 &self,
47 _: &mut SpendContext,
48 (): Self::Solution,
49 ) -> Result<NodePtr, DriverError> {
50 Ok(NodePtr::NIL)
51 }
52}
53
54impl<T> ConditionsLayer<T>
55where
56 T: FromClvm<Allocator> + ToClvm<Allocator> + Clone,
57{
58 pub fn spend(self, ctx: &mut SpendContext) -> Result<Spend, DriverError> {
59 let puzzle = self.construct_puzzle(ctx)?;
60 let solution = self.construct_solution(ctx, ())?;
61
62 Ok(Spend { puzzle, solution })
63 }
64}