Skip to content

Commit

Permalink
Create wasm.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Aug 6, 2024
1 parent ec09e40 commit 7b3e862
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions smart-contract/wasm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// wasm.rs
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub struct SmartContract {
pub storage: Vec<u8>,
}

#[wasm_bindgen]
impl SmartContract {
#[wasm_bindgen(constructor)]
pub fn new() -> Self {
Self { storage: Vec::new() }
}

#[wasm_bindgen(js_name = "store")]
pub fn store(&mut self, key: &str, value: &str) {
self.storage.push(format!("{}={}", key, value).as_bytes().to_vec());
}

#[wasm_bindgen(js_name = "retrieve")]
pub fn retrieve(&self, key: &str) -> Option<String> {
for entry in &self.storage {
let entry_str = std::str::from_utf8(entry).unwrap();
let parts: Vec<&str> = entry_str.split("=").collect();
if parts[0] == key {
return Some(parts[1].to_string());
}
}
None
}
}

0 comments on commit 7b3e862

Please sign in to comment.