action_layer_driver/singleton/
spend_options.rs

1//! Spend bundle options and broadcast configuration
2
3use std::time::Duration;
4
5use chia::protocol::{Bytes32, Coin};
6
7/// Options for spend bundle handling
8///
9/// Allows configuring whether to broadcast immediately, add fees,
10/// and wait for confirmation.
11#[derive(Clone)]
12pub struct SpendOptions {
13    /// Fee amount to include (0 = no fee)
14    pub fee: u64,
15
16    /// Fee coin to spend (required if fee > 0)
17    pub fee_coin: Option<Coin>,
18
19    /// Puzzle hash to send change to (required if fee_coin provided)
20    pub change_puzzle_hash: Option<Bytes32>,
21
22    /// Whether to wait for confirmation after broadcast
23    pub wait_for_confirmation: bool,
24
25    /// Timeout for confirmation (default 5 minutes)
26    pub confirmation_timeout: Duration,
27}
28
29impl Default for SpendOptions {
30    fn default() -> Self {
31        Self {
32            fee: 0,
33            fee_coin: None,
34            change_puzzle_hash: None,
35            wait_for_confirmation: false,
36            confirmation_timeout: Duration::from_secs(300),
37        }
38    }
39}
40
41impl SpendOptions {
42    /// Create options with no fee
43    pub fn no_fee() -> Self {
44        Self::default()
45    }
46
47    /// Create options with a fee
48    pub fn with_fee(fee: u64, fee_coin: Coin, change_puzzle_hash: Bytes32) -> Self {
49        Self {
50            fee,
51            fee_coin: Some(fee_coin),
52            change_puzzle_hash: Some(change_puzzle_hash),
53            ..Default::default()
54        }
55    }
56
57    /// Set whether to wait for confirmation
58    pub fn wait(mut self, wait: bool) -> Self {
59        self.wait_for_confirmation = wait;
60        self
61    }
62
63    /// Set confirmation timeout
64    pub fn timeout(mut self, timeout: Duration) -> Self {
65        self.confirmation_timeout = timeout;
66        self
67    }
68
69    /// Check if fee is configured
70    pub fn has_fee(&self) -> bool {
71        self.fee > 0 && self.fee_coin.is_some()
72    }
73}
74
75/// Fee configuration for spend bundles
76#[derive(Debug, Clone)]
77pub struct FeeOptions {
78    /// The coin to use for fees
79    pub fee_coin: Coin,
80
81    /// Fee amount in mojos
82    pub fee_amount: u64,
83
84    /// Puzzle hash for change output
85    pub change_puzzle_hash: Bytes32,
86}
87
88impl FeeOptions {
89    /// Create new fee options
90    pub fn new(fee_coin: Coin, fee_amount: u64, change_puzzle_hash: Bytes32) -> Self {
91        Self {
92            fee_coin,
93            fee_amount,
94            change_puzzle_hash,
95        }
96    }
97
98    /// Calculate change amount
99    pub fn change_amount(&self) -> u64 {
100        self.fee_coin.amount.saturating_sub(self.fee_amount)
101    }
102}