Skip to content

Commit

Permalink
use string for wasm slot config input
Browse files Browse the repository at this point in the history
  • Loading branch information
twwu123 committed Dec 22, 2024
1 parent 7833ab3 commit dc0412e
Showing 1 changed file with 44 additions and 4 deletions.
48 changes: 44 additions & 4 deletions packages/sidan-csl-rs/src/core/utils/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<UTxO> = Vec::new();
for utxo_json in resolved_utxos {
Expand All @@ -55,12 +63,27 @@ pub fn evaluate_tx_scripts_js(
}
};

let deserialized_slot_config: SlotConfig =
match serde_json::from_str::<JsonSlotConfig>(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 {
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand All @@ -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());
}
}

0 comments on commit dc0412e

Please sign in to comment.