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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use sidan_csl_rs::csl::JsError;

use crate::builder::TxBuilder;

pub mod inputs;
pub mod mints;
pub mod withdrawal;

#[derive(Debug, Clone)]
pub enum WhiskyScriptType {
    Spending,
    Minting,
    Withdrawal,
}

pub struct WhiskyTx {
    pub tx_builder: TxBuilder,
    pub current_script_type: Option<WhiskyScriptType>,
}

#[derive(Debug, Clone)]
pub struct RefScriptInput {
    pub tx_hash: String,
    pub tx_index: u32,
    pub script_hash: String,
    pub script_size: usize,
}

impl WhiskyTx {
    pub fn new() -> Self {
        Self {
            tx_builder: TxBuilder::default(),
            current_script_type: None,
        }
    }

    pub fn provide_script(&mut self, script_cbor: &str) -> Result<&mut Self, JsError> {
        match self.current_script_type {
            Some(WhiskyScriptType::Spending) => self.tx_builder.tx_in_script(script_cbor),
            Some(WhiskyScriptType::Minting) => self.tx_builder.minting_script(script_cbor),
            Some(WhiskyScriptType::Withdrawal) => self.tx_builder.withdrawal_script(script_cbor),
            None => return Err(JsError::from_str("No script type can be inferred, script must be provided after an indicating apis: unlock_from_script, mint_assets, withdraw_from_script")),
        };
        Ok(self)
    }

    pub fn inline_script(
        &mut self,
        tx_hash: &str,
        tx_index: u32,
        script_hash: &str,
        script_size: usize,
    ) -> Result<&mut Self, JsError> {
        match self.current_script_type {
            Some(WhiskyScriptType::Spending) => self.tx_builder.spending_tx_in_reference(
                tx_hash,
                tx_index,
                script_hash,
                script_size,
            ),
            Some(WhiskyScriptType::Minting) => {
                self.tx_builder
                    .mint_tx_in_reference(tx_hash, tx_index, script_hash, script_size)
            }
            Some(WhiskyScriptType::Withdrawal) => self.tx_builder.withdrawal_tx_in_reference(
                tx_hash,
                tx_index,
                script_hash,
                script_size,
            ),
            None => return Err(JsError::from_str("No script type can be inferred, script must be provided after an indicating apis: unlock_from_script, mint_assets, withdraw_from_script")),
        };
        Ok(self)
    }

    pub async fn build(&mut self) -> Result<String, JsError> {
        self.tx_builder.complete(None).await?;
        Ok(self.tx_builder.tx_hex())
    }
}

impl Default for WhiskyTx {
    fn default() -> Self {
        Self::new()
    }
}