whisky_csl/tx_parser/
mod.rs1mod certificates;
2mod change_address;
3mod change_datum;
4mod collaterals;
5mod inputs;
6mod metadata;
7mod mints;
8mod outputs;
9mod reference_inputs;
10mod required_signatures;
11mod validity_range;
12mod votes;
13mod withdrawals;
14
15use cardano_serialization_lib::{self as csl};
16use serde::{Deserialize, Serialize};
17use whisky_common::*;
18
19use crate::utils::calculate_tx_hash;
20
21#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
22#[serde(rename_all = "camelCase")]
23pub struct TxParser {
24 pub tx_hash: String,
25 pub tx_hex: String,
26 pub tx_fee_lovelace: u64,
27 pub tx_body: TxBuilderBody,
28 pub csl_tx_body: csl::TransactionBody,
29 pub csl_witness_set: csl::TransactionWitnessSet,
30}
31
32impl TxParser {
33 pub fn new(tx_hex: &str) -> Result<TxParser, WError> {
34 let csl_tx = csl::Transaction::from_hex(tx_hex).expect("Invalid transaction");
35 let csl_tx_body = csl_tx.body();
36 let csl_witness_set = csl_tx.witness_set();
37
38 let mut tx_parser = TxParser {
41 tx_hash: calculate_tx_hash(tx_hex)?,
42 tx_hex: tx_hex.to_string(),
43 tx_fee_lovelace: csl_tx.body().fee().to_str().parse::<u64>().unwrap(),
44 tx_body: TxBuilderBody::new(),
45 csl_tx_body,
46 csl_witness_set,
47 };
48
49 tx_parser
50 .inputs()
51 .map_err(WError::from_err("TxParser - new"))?;
52 tx_parser
53 .outputs()
54 .map_err(WError::from_err("TxParser - new"))?;
55 tx_parser
56 .collaterals()
57 .map_err(WError::from_err("TxParser - new"))?;
58 tx_parser
59 .required_signatures()
60 .map_err(WError::from_err("TxParser - new"))?;
61 tx_parser
62 .reference_inputs()
63 .map_err(WError::from_err("TxParser - new"))?;
64 tx_parser
65 .withdrawals()
66 .map_err(WError::from_err("TxParser - new"))?;
67 tx_parser
68 .mints()
69 .map_err(WError::from_err("TxParser - new"))?;
70 tx_parser
71 .change_address()
72 .map_err(WError::from_err("TxParser - new"))?;
73 tx_parser
74 .change_datum()
75 .map_err(WError::from_err("TxParser - new"))?;
76 tx_parser
77 .metadata()
78 .map_err(WError::from_err("TxParser - new"))?;
79 tx_parser
80 .validity_range()
81 .map_err(WError::from_err("TxParser - new"))?;
82 tx_parser
83 .certificates()
84 .map_err(WError::from_err("TxParser - new"))?;
85 tx_parser
86 .votes()
87 .map_err(WError::from_err("TxParser - new"))?;
88
89 Ok(tx_parser)
90 }
91}