inkpad_browser/
runtime.rs1use crate::{result::err_check, ri::Interface, ti::Transaction, Tree};
3use inkpad_runtime::Runtime as RuntimeInner;
4use wasm_bindgen::prelude::wasm_bindgen;
5
6const CERES_BROWSER_CACHE: &str = "CERES_BROWSER_CACHE";
7
8#[wasm_bindgen]
10pub struct Runtime(inkpad_runtime::Runtime);
11
12#[wasm_bindgen]
13impl Runtime {
14 #[wasm_bindgen(constructor)]
16 pub fn new(contract: &str) -> Runtime {
17 Runtime(err_check(RuntimeInner::from_metadata(
18 err_check(serde_json::from_str(contract)),
19 Tree::new(CERES_BROWSER_CACHE),
20 Some(Interface),
21 )))
22 }
23
24 pub fn deploy(&mut self, method: &str, args_json: &str, tx_json: Option<String>) {
26 Self::parse_args_and_then(args_json, tx_json, move |args, tx| {
27 err_check(self.0.deploy(method, args, tx.map(|v| v.into())));
28 })
29 }
30
31 pub fn call(&mut self, method: &str, args_json: &str, tx_json: Option<String>) -> String {
33 hex::encode(
34 &Self::parse_args_and_then(args_json, tx_json, move |args, tx| {
35 err_check(self.0.call(method, args, tx.map(|v| v.into())))
36 })
37 .unwrap_or_default(),
38 )
39 }
40
41 fn parse_args_and_then<F, T>(args_json: &str, tx_json: Option<String>, mut f: F) -> T
43 where
44 F: FnMut(Vec<Vec<u8>>, Option<Transaction>) -> T,
45 {
46 let args: Vec<String> = err_check(serde_json::from_str(args_json));
47 let tx = tx_json.map(|v| err_check(serde_json::from_str(&v)));
48 let mut args_bytes: Vec<Vec<u8>> = Default::default();
49
50 for arg in args {
51 args_bytes.push(err_check(hex::decode(arg)));
52 }
53 f(args_bytes, tx)
54 }
55}