chia_sdk_driver/layers/
p2_curried_layer.rs

1use chia_protocol::Bytes32;
2use chia_sdk_types::puzzles::{P2CurriedArgs, P2CurriedSolution, P2_CURRIED_PUZZLE_HASH};
3use clvm_traits::FromClvm;
4use clvmr::{Allocator, NodePtr};
5
6use crate::{DriverError, Layer, Puzzle, SpendContext};
7
8/// The p2 curried [`Layer`] allows for .
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub struct P2CurriedLayer {
11    pub puzzle_hash: Bytes32,
12}
13
14impl Layer for P2CurriedLayer {
15    type Solution = P2CurriedSolution<NodePtr, NodePtr>;
16
17    fn parse_puzzle(allocator: &Allocator, puzzle: Puzzle) -> Result<Option<Self>, DriverError> {
18        let Some(puzzle) = puzzle.as_curried() else {
19            return Ok(None);
20        };
21
22        if puzzle.mod_hash != P2_CURRIED_PUZZLE_HASH {
23            return Ok(None);
24        }
25
26        let args = P2CurriedArgs::from_clvm(allocator, puzzle.args)?;
27
28        Ok(Some(Self {
29            puzzle_hash: args.puzzle_hash,
30        }))
31    }
32
33    fn parse_solution(
34        allocator: &Allocator,
35        solution: NodePtr,
36    ) -> Result<Self::Solution, DriverError> {
37        Ok(P2CurriedSolution::<NodePtr, NodePtr>::from_clvm(
38            allocator, solution,
39        )?)
40    }
41
42    fn construct_puzzle(&self, ctx: &mut SpendContext) -> Result<NodePtr, DriverError> {
43        let curried = ctx.curry(P2CurriedArgs {
44            puzzle_hash: self.puzzle_hash,
45        })?;
46        ctx.alloc(&curried)
47    }
48
49    fn construct_solution(
50        &self,
51        ctx: &mut SpendContext,
52        solution: Self::Solution,
53    ) -> Result<NodePtr, DriverError> {
54        ctx.alloc(&solution)
55    }
56}