Skip to content

Commit

Permalink
feat(wasm-dpp): use u64 (bigint) when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
pshenmic committed Feb 4, 2025
1 parent f0984e9 commit f924e6f
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 48 deletions.
4 changes: 2 additions & 2 deletions packages/wasm-dpp/src/data_contract/data_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,8 @@ impl DataContractWasm {
}

#[wasm_bindgen(js_name=setIdentityNonce)]
pub fn set_identity_nonce(&mut self, e: u64) -> Result<(), JsValue> {
self.identity_nonce = Some(e);
pub fn set_identity_nonce(&mut self, nonce: u64) -> Result<(), JsValue> {
self.identity_nonce = Some(nonce);
Ok(())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use dpp::{
use serde::Serialize;
use serde_json::Value as JsonValue;
use wasm_bindgen::prelude::*;
use dpp::prelude::Revision;
use dpp::fee::Credits;
use dpp::platform_value::converter::serde_json::BTreeValueJsonConverter;
use dpp::state_transition::documents_batch_transition::document_base_transition::DocumentBaseTransition;
Expand Down Expand Up @@ -95,12 +96,8 @@ impl DocumentTransitionWasm {
}

#[wasm_bindgen(js_name=getRevision)]
pub fn get_revision(&self) -> JsValue {
if let Some(revision) = self.0.revision() {
(revision as f64).into()
} else {
JsValue::NULL
}
pub fn get_revision(&self) -> Option<Revision> {
self.0.revision()
}
#[wasm_bindgen(js_name=getEntropy)]
pub fn get_entropy(&self) -> Option<Vec<u8>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ impl DocumentsBatchTransitionWasm {
}

#[wasm_bindgen(js_name=setIdentityContractNonce)]
pub fn set_identity_contract_nonce(&mut self, nonce: u32) {
self.0.set_identity_contract_nonce(nonce as u64);
pub fn set_identity_contract_nonce(&mut self, nonce: u64) {
self.0.set_identity_contract_nonce(nonce);
}

// #[wasm_bindgen(js_name=toJSON)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ impl BalanceIsNotEnoughErrorWasm {
}

#[wasm_bindgen(js_name=getBalance)]
pub fn get_balance(&self) -> f64 {
self.inner.balance() as f64
pub fn get_balance(&self) -> u64 {
self.inner.balance()
}

#[wasm_bindgen(js_name=getFee)]
pub fn get_fee(&self) -> f64 {
self.inner.fee() as f64
pub fn get_fee(&self) -> u64 {
self.inner.fee() as u64
}

#[wasm_bindgen(js_name=getCode)]
Expand Down
28 changes: 14 additions & 14 deletions packages/wasm-dpp/src/identity/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,38 +105,38 @@ impl IdentityWasm {
}

#[wasm_bindgen(getter)]
pub fn balance(&self) -> f64 {
self.inner.balance() as f64
pub fn balance(&self) -> u64 {
self.inner.balance()
}

#[wasm_bindgen(js_name=getBalance)]
pub fn get_balance(&self) -> f64 {
self.inner.balance() as f64
pub fn get_balance(&self) -> u64 {
self.inner.balance()
}

#[wasm_bindgen(js_name=setBalance)]
pub fn set_balance(&mut self, balance: f64) {
self.inner.set_balance(balance as u64);
pub fn set_balance(&mut self, balance: u64) {
self.inner.set_balance(balance);
}

#[wasm_bindgen(js_name=increaseBalance)]
pub fn increase_balance(&mut self, amount: f64) -> f64 {
self.inner.increase_balance(amount as u64) as f64
pub fn increase_balance(&mut self, amount: u64) -> u64 {
self.inner.increase_balance(amount)
}

#[wasm_bindgen(js_name=reduceBalance)]
pub fn reduce_balance(&mut self, amount: f64) -> f64 {
self.inner.reduce_balance(amount as u64) as f64
pub fn reduce_balance(&mut self, amount: u64) -> u64 {
self.inner.reduce_balance(amount)
}

#[wasm_bindgen(js_name=setRevision)]
pub fn set_revision(&mut self, revision: f64) {
self.inner.set_revision(revision as u64);
pub fn set_revision(&mut self, revision: u64) {
self.inner.set_revision(revision);
}

#[wasm_bindgen(js_name=getRevision)]
pub fn get_revision(&self) -> f64 {
self.inner.revision() as f64
pub fn get_revision(&self) -> u64 {
self.inner.revision() as u64
}

#[wasm_bindgen(js_name=setMetadata)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ impl IdentityCreditTransferTransitionWasm {
}

#[wasm_bindgen(js_name=getAmount)]
pub fn get_amount(&self) -> f64 {
self.0.amount() as f64
pub fn get_amount(&self) -> u64 {
self.0.amount()
}

#[wasm_bindgen(js_name=setAmount)]
pub fn set_amount(&mut self, amount: f64) {
self.0.set_amount(amount as u64);
pub fn set_amount(&mut self, amount: u64) {
self.0.set_amount(amount);
}

#[wasm_bindgen(js_name=getUserFeeIncrease)]
Expand Down Expand Up @@ -181,7 +181,7 @@ impl IdentityCreditTransferTransitionWasm {
js_sys::Reflect::set(
&js_object,
&"amount".to_owned().into(),
&JsValue::from_f64(object.amount as f64),
&JsValue::bigint_from_str(&object.amount.to_string()),
)?;

Ok(js_object.into())
Expand Down Expand Up @@ -256,7 +256,7 @@ impl IdentityCreditTransferTransitionWasm {
js_sys::Reflect::set(
&js_object,
&"amount".to_owned().into(),
&JsValue::from_f64(object.amount as f64),
&JsValue::bigint_from_str(&object.amount.to_string()),
)?;

Ok(js_object.into())
Expand Down
28 changes: 14 additions & 14 deletions packages/wasm-dpp/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,22 @@ impl MetadataWasm {
pub fn new(options: JsValue) -> Result<MetadataWasm, JsValue> {
let metadata_options = options.with_serde_to_json_value()?;
let block_height = metadata_options
.get_f64("blockHeight")
.get_u64("blockHeight")
.map_err(|e| JsError::new(&e.to_string()))?;
let core_chain_locked_height = metadata_options
.get_f64("coreChainLockedHeight")
.get_u32("coreChainLockedHeight")
.map_err(|e| JsError::new(&e.to_string()))?;
let time_ms = metadata_options
.get_f64("timeMs")
.get_u64("timeMs")
.map_err(|e| JsError::new(&e.to_string()))?;
let protocol_version = metadata_options
.get_f64("protocolVersion")
.get_u32("protocolVersion")
.map_err(|e| JsError::new(&e.to_string()))?;

let inner = Metadata {
block_height: block_height as u64,
block_height: block_height,
core_chain_locked_height: core_chain_locked_height as u64,
time_ms: time_ms as u64,
time_ms: time_ms,
protocol_version: protocol_version as u64 as ProtocolVersion,
};
Ok(inner.into())
Expand All @@ -77,22 +77,22 @@ impl MetadataWasm {
}

#[wasm_bindgen(js_name=getBlockHeight)]
pub fn block_height(&self) -> f64 {
self.0.block_height as f64
pub fn block_height(&self) -> u64 {
self.0.block_height
}

#[wasm_bindgen(js_name=getCoreChainLockedHeight)]
pub fn core_chain_locked_height(&self) -> f64 {
self.0.core_chain_locked_height as f64
pub fn core_chain_locked_height(&self) -> u32 {
self.0.core_chain_locked_height as u32
}

#[wasm_bindgen(js_name=getTimeMs)]
pub fn time_ms(&self) -> f64 {
self.0.time_ms as f64
pub fn time_ms(&self) -> u64 {
self.0.time_ms as u64
}

#[wasm_bindgen(js_name=getProtocolVersion)]
pub fn protocol_version(&self) -> f64 {
self.0.protocol_version as f64
pub fn protocol_version(&self) -> u32 {
self.0.protocol_version as u32
}
}

0 comments on commit f924e6f

Please sign in to comment.