whisky/transaction/
mod.rs

1use crate::builder::TxBuilder;
2use crate::*;
3
4pub mod inputs;
5pub mod mints;
6pub mod withdrawal;
7
8#[derive(Debug, Clone)]
9pub enum WhiskyScriptType {
10    Spending,
11    Minting,
12    Withdrawal,
13}
14
15pub struct WhiskyTx {
16    pub tx_builder: TxBuilder,
17    pub current_script_type: Option<WhiskyScriptType>,
18}
19
20#[derive(Debug, Clone)]
21pub struct RefScriptInput {
22    pub tx_hash: String,
23    pub tx_index: u32,
24    pub script_hash: String,
25    pub script_size: usize,
26}
27
28impl WhiskyTx {
29    pub fn new() -> Self {
30        Self {
31            tx_builder: TxBuilder::default(),
32            current_script_type: None,
33        }
34    }
35
36    pub fn provide_script(&mut self, script_cbor: &str) -> Result<&mut Self, WError> {
37        match self.current_script_type {
38            Some(WhiskyScriptType::Spending) => self.tx_builder.tx_in_script(script_cbor),
39            Some(WhiskyScriptType::Minting) => self.tx_builder.minting_script(script_cbor),
40            Some(WhiskyScriptType::Withdrawal) => self.tx_builder.withdrawal_script(script_cbor),
41            None => return Err(WError::new(
42                "WhiskyTx - provide_script",
43                "No script type can be inferred, script must be provided after an indicating apis: unlock_from_script, mint_assets, withdraw_from_script"
44            )),
45        };
46        Ok(self)
47    }
48
49    pub fn inline_script(
50        &mut self,
51        tx_hash: &str,
52        tx_index: u32,
53        script_hash: &str,
54        script_size: usize,
55    ) -> Result<&mut Self, WError> {
56        match self.current_script_type {
57            Some(WhiskyScriptType::Spending) => self.tx_builder.spending_tx_in_reference(
58                tx_hash,
59                tx_index,
60                script_hash,
61                script_size,
62            ),
63            Some(WhiskyScriptType::Minting) => {
64                self.tx_builder
65                    .mint_tx_in_reference(tx_hash, tx_index, script_hash, script_size)
66            }
67            Some(WhiskyScriptType::Withdrawal) => self.tx_builder.withdrawal_tx_in_reference(
68                tx_hash,
69                tx_index,
70                script_hash,
71                script_size,
72            ),
73            None => return Err(WError::new(
74                "WhiskyTx - inline_script",
75                "No script type can be inferred, script must be provided after an indicating apis: unlock_from_script, mint_assets, withdraw_from_script")),
76        };
77        Ok(self)
78    }
79
80    pub async fn build(&mut self) -> Result<String, WError> {
81        self.tx_builder.complete(None).await?;
82        Ok(self.tx_builder.tx_hex())
83    }
84}
85
86impl Default for WhiskyTx {
87    fn default() -> Self {
88        Self::new()
89    }
90}