From dc0412ebeff2b0d8f34d0a1e3a2110162263c71d Mon Sep 17 00:00:00 2001 From: twwu123 Date: Sun, 22 Dec 2024 22:41:59 +0800 Subject: [PATCH] use string for wasm slot config input --- .../sidan-csl-rs/src/core/utils/evaluator.rs | 48 +++++++++++++++++-- 1 file changed, 44 insertions(+), 4 deletions(-) diff --git a/packages/sidan-csl-rs/src/core/utils/evaluator.rs b/packages/sidan-csl-rs/src/core/utils/evaluator.rs index 56c3ac5..8665076 100644 --- a/packages/sidan-csl-rs/src/core/utils/evaluator.rs +++ b/packages/sidan-csl-rs/src/core/utils/evaluator.rs @@ -24,13 +24,21 @@ use pallas_primitives::{ use pallas_traverse::{Era, MultiEraTx}; use uplc::{tx::error::Error as UplcError, tx::ResolvedInput, Hash, TransactionInput}; +#[derive(serde::Deserialize, serde::Serialize)] +#[serde(rename_all = "camelCase")] +pub struct JsonSlotConfig { + slot_length: u32, + zero_slot: u64, + zero_time: u64, +} + #[wasm_bindgen] pub fn evaluate_tx_scripts_js( tx_hex: String, resolved_utxos: &JsVecString, additional_txs: &JsVecString, network: String, - slot_config: SlotConfig, + slot_config: String, ) -> WasmResult { let mut deserialized_utxos: Vec = Vec::new(); for utxo_json in resolved_utxos { @@ -55,12 +63,27 @@ pub fn evaluate_tx_scripts_js( } }; + let deserialized_slot_config: SlotConfig = + match serde_json::from_str::(slot_config.as_str()) { + Ok(slot_config) => SlotConfig { + slot_length: slot_config.slot_length, + zero_slot: slot_config.zero_slot, + zero_time: slot_config.zero_time, + }, + Err(e) => { + return WasmResult::new_error( + "failure".to_string(), + format!("Error in decoding slot config: {:?}", e), + ); + } + }; + let eval_result = evaluate_tx_scripts( &tx_hex, &deserialized_utxos, additional_txs.as_ref_vec(), &deserialize_network, - &slot_config, + &deserialized_slot_config, ); match eval_result { @@ -533,7 +556,11 @@ mod test { &resolved_utxos, &additional_txs, "preprod".to_string(), - SlotConfig::default(), + serde_json::to_string(&JsonSlotConfig { + slot_length: 1000, + zero_slot: 4492800, + zero_time: 1596059091000, + }).unwrap(), ); assert_eq!(result.get_status(), "success"); @@ -600,7 +627,11 @@ mod test { &resolved_utxos, &additional_txs, "preprod".to_string(), - SlotConfig::default(), + serde_json::to_string(&JsonSlotConfig { + slot_length: 1000, + zero_slot: 0, + zero_time: 1666656000000, + }).unwrap(), ); assert_eq!(result.get_status(), "success"); @@ -625,4 +656,13 @@ mod test { ); assert_eq!(error_result.logs, ["This is a trace"]); } + + #[test] + fn config_test() { + println!("{:?}", serde_json::to_string(&JsonSlotConfig { + slot_length: 1000, + zero_slot: 0, + zero_time: 1666656000000, + }).unwrap()); + } }