whisky_pallas/tx_parser/
outputs.rs

1use pallas::{
2    codec::utils::PositiveCoin,
3    ledger::primitives::{
4        conway::{Tx, Value},
5        Fragment,
6    },
7};
8use whisky_common::{Output, ProvidedScriptSource, ProvidedSimpleScriptSource, WError};
9
10use crate::converter::{bech32_from_bytes, value_to_asset_vec};
11
12pub fn extract_outputs(pallas_tx: &Tx) -> Result<Vec<Output>, WError> {
13    let mut outputs_vec: Vec<Output> = Vec::new();
14    let outputs = &pallas_tx.transaction_body.outputs;
15    for output in outputs.iter() {
16        match output {
17            pallas::ledger::primitives::babbage::GenTransactionOutput::Legacy(legacy_output) => {
18                let coerced_output = match &legacy_output.amount {
19                    pallas::ledger::primitives::alonzo::Value::Coin(coin) => {
20                        Value::Coin(coin.clone())
21                    }
22                    pallas::ledger::primitives::alonzo::Value::Multiasset(coin, asset_map) => {
23                        let converted_asset_map = asset_map
24                            .into_iter()
25                            .map(|(policy_id, assets)| {
26                                let converted_assets = assets
27                                    .into_iter()
28                                    .map(|(asset_name, amount)| {
29                                        (
30                                            asset_name.clone(),
31                                            PositiveCoin::try_from(amount.clone()).unwrap(),
32                                        )
33                                    })
34                                    .collect();
35                                (policy_id.clone(), converted_assets)
36                            })
37                            .collect();
38                        Value::Multiasset(coin.clone(), converted_asset_map)
39                    }
40                };
41                let tx_out = Output {
42                    address: bech32_from_bytes(&legacy_output.address.to_string())?,
43                    amount: value_to_asset_vec(&&crate::wrapper::transaction_body::Value {
44                        inner: coerced_output,
45                    })?,
46                    datum: match &legacy_output.datum_hash {
47                        Some(datum) => Some(whisky_common::Datum::Hash(datum.to_string())),
48                        None => None,
49                    },
50                    reference_script: None,
51                };
52                outputs_vec.push(tx_out);
53            }
54            pallas::ledger::primitives::babbage::GenTransactionOutput::PostAlonzo(
55                post_alonzo_output,
56            ) => {
57                let tx_out: Output = Output {
58                    address: bech32_from_bytes(&post_alonzo_output.address.to_string())?,
59                    amount: value_to_asset_vec(&&crate::wrapper::transaction_body::Value {
60                        inner: post_alonzo_output.value.clone(),
61                    })?,
62                    datum: match &post_alonzo_output.datum_option {
63                        Some(datum) => match datum.clone().unwrap() {
64                            pallas::ledger::primitives::conway::DatumOption::Hash(hash) => {
65                                Some(whisky_common::Datum::Hash(hash.to_string()))
66                            }
67                            pallas::ledger::primitives::conway::DatumOption::Data(datum) => {
68                                Some(whisky_common::Datum::Inline(hex::encode(datum.raw_cbor())))
69                            }
70                        },
71                        None => None,
72                    },
73                    reference_script: match &post_alonzo_output.script_ref {
74                        Some(script) => Some(match script.clone().unwrap() {
75                            pallas::ledger::primitives::conway::ScriptRef::NativeScript(
76                                native_script,
77                            ) => whisky_common::OutputScriptSource::ProvidedSimpleScriptSource(
78                                ProvidedSimpleScriptSource {
79                                    script_cbor: hex::encode(
80                                        native_script.encode_fragment().unwrap(),
81                                    ),
82                                },
83                            ),
84                            pallas::ledger::primitives::conway::ScriptRef::PlutusV1Script(
85                                plutus_script,
86                            ) => whisky_common::OutputScriptSource::ProvidedScriptSource(
87                                ProvidedScriptSource {
88                                    script_cbor: hex::encode(
89                                        plutus_script.encode_fragment().unwrap(),
90                                    ),
91                                    language_version: whisky_common::LanguageVersion::V1,
92                                },
93                            ),
94                            pallas::ledger::primitives::conway::ScriptRef::PlutusV2Script(
95                                plutus_script,
96                            ) => whisky_common::OutputScriptSource::ProvidedScriptSource(
97                                ProvidedScriptSource {
98                                    script_cbor: hex::encode(
99                                        plutus_script.encode_fragment().unwrap(),
100                                    ),
101                                    language_version: whisky_common::LanguageVersion::V2,
102                                },
103                            ),
104                            pallas::ledger::primitives::conway::ScriptRef::PlutusV3Script(
105                                plutus_script,
106                            ) => whisky_common::OutputScriptSource::ProvidedScriptSource(
107                                ProvidedScriptSource {
108                                    script_cbor: hex::encode(
109                                        plutus_script.encode_fragment().unwrap(),
110                                    ),
111                                    language_version: whisky_common::LanguageVersion::V3,
112                                },
113                            ),
114                        }),
115                        None => None,
116                    },
117                };
118                outputs_vec.push(tx_out);
119            }
120        }
121    }
122    Ok(outputs_vec)
123}