whisky_js/wasm/
tx_evaluator.rs

1use crate::*;
2use uplc::tx::SlotConfig;
3use whisky_common::*;
4use whisky_csl::*;
5
6#[wasm_bindgen]
7pub fn js_evaluate_tx_scripts(
8    tx_hex: String,
9    resolved_utxos: &JsVecString,
10    additional_txs: &JsVecString,
11    network: String,
12    slot_config: String,
13) -> WasmResult {
14    let mut deserialized_utxos: Vec<UTxO> = Vec::new();
15    for utxo_json in resolved_utxos {
16        match serde_json::from_str(utxo_json.as_str()) {
17            Ok(utxo) => deserialized_utxos.push(utxo),
18            Err(e) => {
19                return WasmResult::new_error(
20                    "failure".to_string(),
21                    format!("Error in decoding UTXO: {:?}", e),
22                );
23            }
24        }
25    }
26
27    let deserialize_network = match network.try_into() {
28        Ok(network) => network,
29        Err(e) => {
30            return WasmResult::new_error(
31                "failure".to_string(),
32                format!("Error in decoding network: {:?}", e),
33            );
34        }
35    };
36
37    let deserialized_slot_config: SlotConfig =
38        match serde_json::from_str::<JsonSlotConfig>(slot_config.as_str()) {
39            Ok(slot_config) => SlotConfig {
40                slot_length: slot_config.slot_length,
41                zero_slot: slot_config.zero_slot,
42                zero_time: slot_config.zero_time,
43            },
44            Err(e) => {
45                return WasmResult::new_error(
46                    "failure".to_string(),
47                    format!("Error in decoding slot config: {:?}", e),
48                );
49            }
50        };
51
52    let eval_result = evaluate_tx_scripts(
53        &tx_hex,
54        &deserialized_utxos,
55        additional_txs.as_ref_vec(),
56        &deserialize_network,
57        &deserialized_slot_config,
58    );
59
60    match eval_result {
61        Ok(actions) => {
62            let actions_json = serde_json::to_string(&actions).unwrap();
63            WasmResult::new("success".to_string(), actions_json)
64        }
65        Err(e) => WasmResult::new_error("failure".to_string(), format!("{:?}", e)),
66    }
67}