1use super::*;
2
3#[wasm_bindgen(js_name = CoeusState)]
4pub struct JsCoeusState {
5 inner: CoeusState,
6}
7
8#[wasm_bindgen(js_class = CoeusState)]
9impl JsCoeusState {
10 #[wasm_bindgen(constructor)]
11 pub fn new() -> Result<JsCoeusState, JsValue> {
12 let inner = CoeusState::new();
13 Ok(Self { inner })
14 }
15
16 #[wasm_bindgen(js_name = resolveData)]
17 pub fn resolve_data(&self, name: &JsDomainName) -> Result<JsValue, JsValue> {
18 let data = self.inner.resolve_data(name.inner()).map_err_to_js()?;
19 JsValue::from_serde(data).map_err_to_js()
20 }
21
22 #[wasm_bindgen(js_name = getMetadata)]
23 pub fn get_metadata(&self, name: &JsDomainName) -> Result<JsValue, JsValue> {
24 let domain = self.inner.domain(name.inner()).map_err_to_js()?;
25
26 #[derive(Debug, Serialize)]
27 #[serde(rename_all = "camelCase")]
28 struct Metadata<'a> {
29 owner: &'a Principal,
30 subtree_policies: &'a SubtreePolicies,
31 registration_policy: &'a RegistrationPolicy,
32 expires_at_height: BlockHeight,
33 }
34
35 let metadata = Metadata {
36 owner: domain.owner(),
37 subtree_policies: domain.subtree_policies(),
38 registration_policy: domain.registration_policy(),
39 expires_at_height: domain.expires_at_height(),
40 };
41
42 JsValue::from_serde(&metadata).map_err_to_js()
43 }
44
45 #[wasm_bindgen(js_name = getChildren)]
46 pub fn get_children(&self, name: &JsDomainName) -> Result<JsValue, JsValue> {
47 let domain = self.inner.domain(name.inner()).map_err_to_js()?;
48 JsValue::from_serde(&domain.child_names()).map_err_to_js()
49 }
50
51 #[wasm_bindgen(js_name = lastNonce)]
52 pub fn last_nonce(&self, pk: &JsMPublicKey) -> Nonce {
53 return self.inner.nonce(pk.inner());
54 }
55
56 #[wasm_bindgen(js_name = applyTransaction)]
57 pub fn apply_transaction(&mut self, txid: &str, asset: &JsCoeusAsset) -> Result<(), JsValue> {
58 self.inner.apply_transaction(txid, asset.inner().to_owned()).map_err_to_js()
59 }
60
61 #[wasm_bindgen(js_name = revertTransaction)]
62 pub fn revert_transaction(&mut self, txid: &str, asset: &JsCoeusAsset) -> Result<(), JsValue> {
63 self.inner.revert_transaction(txid, asset.inner().to_owned()).map_err_to_js()
64 }
65
66 #[wasm_bindgen(js_name = blockApplying)]
67 pub fn block_applying(&mut self, height: BlockHeight) -> Result<(), JsValue> {
68 self.inner.block_applying(height).map_err_to_js()
69 }
70
71 #[wasm_bindgen(js_name = blockReverted)]
72 pub fn block_reverted(&mut self, height: BlockHeight) -> Result<(), JsValue> {
73 self.inner.block_reverted(height).map_err_to_js()
74 }
75
76 #[wasm_bindgen(getter = corrupted)]
77 pub fn is_corrupted(&self) -> bool {
78 self.inner.is_corrupted()
79 }
80
81 #[wasm_bindgen(getter)]
82 pub fn version(&self) -> Version {
83 self.inner.version()
84 }
85
86 #[wasm_bindgen(getter = lastSeenHeight)]
87 pub fn last_seen_height(&self) -> BlockHeight {
88 self.inner.last_seen_height()
89 }
90
91 #[wasm_bindgen(js_name = getTxnStatus)]
92 pub fn get_txn_status(&self, txid: &str) -> Result<bool, JsValue> {
93 let status = self.inner.get_txn_status(txid).map_err_to_js()?;
94 Ok(status.success)
95 }
96
97 }
102
103impl From<CoeusState> for JsCoeusState {
104 fn from(inner: CoeusState) -> Self {
105 Self { inner }
106 }
107}
108
109impl Wraps<CoeusState> for JsCoeusState {
110 fn inner(&self) -> &CoeusState {
111 &self.inner
112 }
113}