1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use chia_puzzles::offer::{SettlementPaymentsSolution, SETTLEMENT_PAYMENTS_PUZZLE_HASH};
use clvm_traits::FromClvm;
use clvmr::{Allocator, NodePtr};

use crate::{DriverError, Layer, Puzzle, SpendContext};

/// The settlement [`Layer`] is used to spend coins that are part of an offer.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SettlementLayer;

impl Layer for SettlementLayer {
    type Solution = SettlementPaymentsSolution;

    fn construct_puzzle(&self, ctx: &mut SpendContext) -> Result<NodePtr, DriverError> {
        ctx.settlement_payments_puzzle()
    }

    fn construct_solution(
        &self,
        ctx: &mut SpendContext,
        solution: Self::Solution,
    ) -> Result<NodePtr, DriverError> {
        ctx.alloc(&solution)
    }

    fn parse_puzzle(_allocator: &Allocator, puzzle: Puzzle) -> Result<Option<Self>, DriverError> {
        if puzzle.curried_puzzle_hash() != SETTLEMENT_PAYMENTS_PUZZLE_HASH {
            return Ok(None);
        }
        Ok(Some(Self))
    }

    fn parse_solution(
        allocator: &Allocator,
        solution: NodePtr,
    ) -> Result<Self::Solution, DriverError> {
        Ok(FromClvm::from_clvm(allocator, solution)?)
    }
}