use chik_protocol::Bytes32;
use chik_sdk_types::puzzles::{
DelegationLayerArgs, DelegationLayerSolution, DELEGATION_LAYER_PUZZLE_HASH,
};
use klvm_traits::FromKlvm;
use klvmr::{Allocator, NodePtr};
use crate::{DriverError, Layer, Puzzle, SpendContext};
#[allow(clippy::doc_markdown)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DelegationLayer {
pub launcher_id: Bytes32,
pub owner_puzzle_hash: Bytes32,
pub merkle_root: Bytes32,
}
impl DelegationLayer {
pub fn new(launcher_id: Bytes32, owner_puzzle_hash: Bytes32, merkle_root: Bytes32) -> Self {
Self {
launcher_id,
owner_puzzle_hash,
merkle_root,
}
}
}
impl Layer for DelegationLayer {
type Solution = DelegationLayerSolution<NodePtr, NodePtr>;
fn parse_puzzle(allocator: &Allocator, puzzle: Puzzle) -> Result<Option<Self>, DriverError> {
let Some(puzzle) = puzzle.as_curried() else {
return Ok(None);
};
if puzzle.mod_hash != DELEGATION_LAYER_PUZZLE_HASH {
return Ok(None);
}
let args = DelegationLayerArgs::from_klvm(allocator, puzzle.args)?;
Ok(Some(Self {
launcher_id: args.launcher_id,
owner_puzzle_hash: args.owner_puzzle_hash,
merkle_root: args.merkle_root,
}))
}
fn parse_solution(
allocator: &Allocator,
solution: NodePtr,
) -> Result<Self::Solution, DriverError> {
Ok(DelegationLayerSolution::<NodePtr, NodePtr>::from_klvm(
allocator, solution,
)?)
}
fn construct_puzzle(&self, ctx: &mut SpendContext) -> Result<NodePtr, DriverError> {
let curried = ctx.curry(DelegationLayerArgs::new(
self.launcher_id,
self.owner_puzzle_hash,
self.merkle_root,
))?;
ctx.alloc(&curried)
}
fn construct_solution(
&self,
ctx: &mut SpendContext,
solution: Self::Solution,
) -> Result<NodePtr, DriverError> {
ctx.alloc(&solution)
}
}