chia_sdk_driver/action_system/
output.rs1use chia_protocol::Bytes32;
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
4pub struct Output {
5 pub puzzle_hash: Bytes32,
6 pub amount: u64,
7}
8
9impl Output {
10 pub fn new(puzzle_hash: Bytes32, amount: u64) -> Self {
11 Self {
12 puzzle_hash,
13 amount,
14 }
15 }
16}
17
18#[derive(Debug, Clone, Copy, PartialEq, Eq)]
19pub struct OutputConstraints {
20 pub singleton: bool,
21 pub settlement: bool,
22}
23
24pub trait OutputSet {
25 fn has_output(&self, output: &Output) -> bool;
26 fn can_run_cat_tail(&self) -> bool;
27 fn missing_singleton_output(&self) -> bool;
28
29 fn find_amount(
30 &self,
31 puzzle_hash: Bytes32,
32 output_constraints: &OutputConstraints,
33 ) -> Option<u64> {
34 (0..u64::MAX)
35 .find(|amount| self.is_allowed(&Output::new(puzzle_hash, *amount), output_constraints))
36 }
37
38 fn is_allowed(&self, output: &Output, output_constraints: &OutputConstraints) -> bool {
39 if output_constraints.singleton && output.amount % 2 == 1 {
40 return false;
41 }
42
43 if output_constraints.settlement && output.amount == 0 {
44 return false;
45 }
46
47 !self.has_output(output)
48 }
49}