whisky_js/wasm/
tx_builder.rs

1use crate::*;
2use wasm::WasmResult;
3use whisky_common::*;
4use whisky_csl::WhiskyCSL;
5
6/// ## WASM Transaction building method
7///
8/// Serialize the transaction body
9///
10/// ### Arguments
11///
12/// * `tx_builder_body_json` - The transaction builder body information, serialized as JSON string
13/// * `params_json` - Optional protocol parameters, default as Cardano mainnet configuration, serialized as JSON string
14///
15/// ### Returns
16///
17/// * `String` - the built transaction hex
18#[wasm_bindgen]
19pub fn js_serialize_tx_body(tx_builder_body_json: &str, params_json: &str) -> WasmResult {
20    let tx_builder_body: TxBuilderBody = match serde_json::from_str(tx_builder_body_json) {
21        Ok(tx_builder_body) => tx_builder_body,
22        Err(e) => {
23            return WasmResult::new_error("failure".to_string(), format!("Invalid JSON: {:?}", e))
24        }
25    };
26
27    let params: Option<Protocol> = match serde_json::from_str(params_json) {
28        Ok(params) => Some(params),
29        Err(e) => {
30            return WasmResult::new_error(
31                "failure".to_string(),
32                format!("Invalid Protocol Param JSON: {:?} \n {:?}", params_json, e),
33            )
34        }
35    };
36
37    let mut tx_builder = WhiskyCSL::new(params).unwrap();
38    tx_builder.tx_builder_body = tx_builder_body;
39
40    match tx_builder.unbalanced_serialize_tx_body() {
41        Ok(tx_hex) => WasmResult::new("success".to_string(), tx_hex.to_string()),
42        Err(e) => WasmResult::new_error("failure".to_string(), format!("{:?}", e.to_string())),
43    }
44}