Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(wasm-dpp): add extra methods for state transitions #2401

Open
wants to merge 20 commits into
base: v1.8-dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
6196555
feat(wasm-dpp): add resource vote choice
pshenmic Nov 21, 2024
b69bcc6
feat(wasm-dpp): implement userFeeIncrease
pshenmic Nov 25, 2024
6744f4e
feat(wasm-dpp): add identity and identity contract nonces
pshenmic Nov 25, 2024
699c633
feat(wasm-dpp): ensure signature and signature public key id in each …
pshenmic Nov 25, 2024
dc5aedb
fix(wasm-dpp): remove duplicated
pshenmic Nov 25, 2024
797e75b
fix(wasm-dpp): remove duplicated
pshenmic Nov 25, 2024
cacc5e7
fix(wasm-dpp): add prefundedVotingBalance in create document transition
pshenmic Nov 25, 2024
23c0672
feat(wasm-dpp): add prefundedVotingBalance in document transition
pshenmic Nov 25, 2024
7a04862
feat(rs-dpp): add more action types in try from for DocumentTransitio…
pshenmic Nov 25, 2024
e3c3942
fix(wasm-dpp): fix js binding method name
pshenmic Nov 25, 2024
d7d184f
feat(wasm-dpp): add getData for DocumentTransition
pshenmic Nov 25, 2024
5901158
Merge branch 'refs/heads/temp' into feat/wasm-extra-fields
pshenmic Dec 17, 2024
ec0c4d5
chore(wasm-dpp): cleanup masternode vote
pshenmic Dec 18, 2024
0aaf02a
chore(wasm-dpp): cleanup masternode vote
pshenmic Dec 18, 2024
dbbac77
chore(wasm-dpp): remove duplicated nonce method
pshenmic Dec 18, 2024
711463a
chore(wasm-dpp): fix nonce method naming in credit withdrawals
pshenmic Dec 18, 2024
bab0359
feat(wasm-dpp): add entropy getter for document transitions
pshenmic Dec 26, 2024
701d1b5
feat(wasm-dpp): add entropy getter for documenttransition
pshenmic Dec 27, 2024
d77e1aa
feat(rs-dpp): add entropy getter in the rs dpp
pshenmic Dec 27, 2024
e21ae53
feat(dpp): move to optional
pshenmic Dec 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ impl TryFrom<&str> for DocumentTransitionActionType {
"replace" => Ok(DocumentTransitionActionType::Replace),
"delete" => Ok(DocumentTransitionActionType::Delete),
"transfer" => Ok(DocumentTransitionActionType::Transfer),
"purchase" => Ok(DocumentTransitionActionType::Purchase),
"updatePrice" => Ok(DocumentTransitionActionType::UpdatePrice),
action_type => Err(ProtocolError::Generic(format!(
"unknown action type {action_type}"
))),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,27 @@ impl DataContractCreateTransitionWasm {
pub fn get_type(&self) -> u32 {
self.0.state_transition_type() as u32
}

#[wasm_bindgen(js_name=setUserFeeIncrease)]
pub fn set_user_fee_increase(&mut self, user_fee_increase: u16) {
self.0.set_user_fee_increase(user_fee_increase);
}

#[wasm_bindgen(js_name=getUserFeeIncrease)]
pub fn get_user_fee_increase(&self) -> u16 {
self.0.user_fee_increase() as u16
}

#[wasm_bindgen(js_name=getSignature)]
pub fn get_signature(&self) -> Buffer {
Buffer::from_bytes(self.0.signature().as_slice())
}

#[wasm_bindgen(js_name=getSignaturePublicKeyId)]
pub fn get_signature_public_key_id(&self) -> u32 {
self.0.signature_public_key_id()
}

//
// #[wasm_bindgen(js_name=toJSON)]
// pub fn to_json(&self, skip_signature: Option<bool>) -> Result<JsValue, JsValue> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,36 @@ impl DataContractUpdateTransitionWasm {
self.0.owner_id().into()
}

#[wasm_bindgen(js_name=getIdentityContractNonce)]
pub fn get_identity_contract_nonce(&self) -> u64 {
self.0.identity_contract_nonce() as u64
}

#[wasm_bindgen(js_name=getType)]
pub fn get_type(&self) -> u32 {
self.0.state_transition_type() as u32
}

#[wasm_bindgen(js_name=getUserFeeIncrease)]
pub fn get_user_fee_increase(&self) -> u16 {
self.0.user_fee_increase() as u16
}

#[wasm_bindgen(js_name=setUserFeeIncrease)]
pub fn set_user_fee_increase(&mut self, user_fee_increase: u16) {
self.0.set_user_fee_increase(user_fee_increase);
}

#[wasm_bindgen(js_name=getSignature)]
pub fn get_signature(&self) -> Buffer {
Buffer::from_bytes(self.0.signature().as_slice())
}

#[wasm_bindgen(js_name=getSignaturePublicKeyId)]
pub fn get_signature_public_key_id(&self) -> u32 {
self.0.signature_public_key_id()
}

// #[wasm_bindgen(js_name=toJSON)]
// pub fn to_json(&self, skip_signature: Option<bool>) -> Result<JsValue, JsValue> {
// let serializer = serde_wasm_bindgen::Serializer::json_compatible();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use dpp::document::INITIAL_REVISION;

use wasm_bindgen::prelude::*;
use dpp::state_transition::documents_batch_transition::document_base_transition::v0::v0_methods::DocumentBaseTransitionV0Methods;
use dpp::state_transition::documents_batch_transition::document_create_transition::v0::v0_methods::DocumentCreateTransitionV0Methods;

#[wasm_bindgen(js_name=DocumentCreateTransition)]
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -81,6 +83,47 @@
INITIAL_REVISION as u32
}

#[wasm_bindgen(js_name = getEntropy)]
pub fn get_entropy(&self) -> Vec<u8> {
Vec::from(self.inner.entropy())
}

#[wasm_bindgen(js_name=getIdentityContractNonce)]
pub fn get_identity_contract_nonce(&self) -> u64 {
self.inner.base().identity_contract_nonce() as u64
}

#[wasm_bindgen(js_name=setIdentityContractNonce)]
pub fn set_identity_contract_nonce(&mut self, identity_contract_nonce: u64) -> () {

Check warning on line 97 in packages/wasm-dpp/src/document/state_transition/document_batch_transition/document_transition/document_create_transition.rs

View workflow job for this annotation

GitHub Actions / Rust packages (wasm-dpp) / Linting

unneeded unit return type

warning: unneeded unit return type --> packages/wasm-dpp/src/document/state_transition/document_batch_transition/document_transition/document_create_transition.rs:97:80 | 97 | pub fn set_identity_contract_nonce(&mut self, identity_contract_nonce: u64) -> () { | ^^^^^^ help: remove the `-> ()` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unused_unit = note: `#[warn(clippy::unused_unit)]` on by default
let mut base = self.inner.base().clone();

base.set_identity_contract_nonce(identity_contract_nonce);

self.inner.set_base(base)
}

#[wasm_bindgen(js_name = getPrefundedVotingBalance)]
pub fn get_prefunded_voting_balance(&self) -> Result<JsValue, JsValue> {
let prefunded_voting_balance = self.inner.prefunded_voting_balance().clone();

match prefunded_voting_balance {
None => {
Ok(JsValue::null())
},
Some((index_name, credits)) => {
let js_object = js_sys::Object::new();

js_sys::Reflect::set(
&js_object,
&JsValue::from_str(&index_name),
&JsValue::from(credits),
)?;

Ok(JsValue::from(js_object))
}
}
}

// // AbstractDocumentTransitionMethods
// #[wasm_bindgen(js_name=getId)]
// pub fn id(&self) -> IdentifierWrapper {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,30 @@ impl DocumentDeleteTransitionWasm {
self.inner.base().document_type_name().clone()
}

#[wasm_bindgen(js_name = getEntropy)]
pub fn get_entropy(&self) -> Vec<u8> {
Vec::from(self.inner.entropy())
}

#[wasm_bindgen(js_name=getDataContractId)]
pub fn data_contract_id(&self) -> IdentifierWrapper {
self.inner.base().data_contract_id().into()
}

#[wasm_bindgen(js_name=getIdentityContractNonce)]
pub fn get_identity_contract_nonce(&self) -> u64 {
self.inner.base().identity_contract_nonce() as u64
}

#[wasm_bindgen(js_name=setIdentityContractNonce)]
pub fn set_identity_contract_nonce(&mut self, identity_contract_nonce: u64) -> () {
let mut base = self.inner.base().clone();

base.set_identity_contract_nonce(identity_contract_nonce);

self.inner.set_base(base)
}

#[wasm_bindgen(js_name=get)]
pub fn get(&self, path: String) -> Result<JsValue, JsValue> {
let _ = path;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ impl DocumentReplaceTransitionWasm {
self.inner.revision()
}

#[wasm_bindgen(js_name = getEntropy)]
pub fn get_entropy(&self) -> Vec<u8> {
Vec::from(self.inner.entropy())
}

#[wasm_bindgen(js_name=getUpdatedAt)]
pub fn updated_at(&self) -> Option<js_sys::Date> {
self.inner
Expand Down Expand Up @@ -189,6 +194,20 @@ impl DocumentReplaceTransitionWasm {
self.inner.base().data_contract_id().into()
}

#[wasm_bindgen(js_name=getIdentityContractNonce)]
pub fn get_identity_contract_nonce(&self) -> u64 {
self.inner.base().identity_contract_nonce() as u64
}

#[wasm_bindgen(js_name=setIdentityContractNonce)]
pub fn set_identity_contract_nonce(&mut self, identity_contract_nonce: u64) -> () {
let mut base = self.inner.base().clone();

base.set_identity_contract_nonce(identity_contract_nonce);

self.inner.set_base(base)
}

#[wasm_bindgen(js_name=get)]
pub fn get(&self, path: String) -> Result<JsValue, JsValue> {
let document_data = if let Some(ref data) = self.inner.data() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use serde::Serialize;
use serde_json::Value as JsonValue;
use wasm_bindgen::prelude::*;
use dpp::platform_value::converter::serde_json::BTreeValueJsonConverter;
use dpp::state_transition::documents_batch_transition::document_replace_transition::v0::v0_methods::DocumentReplaceTransitionV0Methods;

use crate::{
buffer::Buffer,
Expand All @@ -30,7 +32,7 @@
#[derive(Debug, Clone)]
pub struct DocumentTransitionWasm(DocumentTransition);

#[wasm_bindgen(js_class=DocumentTransition)]

Check failure on line 35 in packages/wasm-dpp/src/document/state_transition/document_batch_transition/document_transition/mod.rs

View workflow job for this annotation

GitHub Actions / Rust packages (wasm-dpp) / Linting

mismatched types

error[E0308]: mismatched types --> packages/wasm-dpp/src/document/state_transition/document_batch_transition/document_transition/mod.rs:35:1 | 35 | #[wasm_bindgen(js_class=DocumentTransition)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | | | expected `Vec<u8>`, found `JsValue` | arguments to this function are incorrect | = note: expected struct `std::vec::Vec<u8>` found struct `wasm_bindgen::JsValue` help: the return type of this call is `wasm_bindgen::JsValue` due to the type of the argument passed --> packages/wasm-dpp/src/document/state_transition/document_batch_transition/document_transition/mod.rs:35:1 | 35 | #[wasm_bindgen(js_class=DocumentTransition)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this argument influences the return type of `wasm_bindgen` note: method defined here --> /home/runner/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wasm-bindgen-0.2.99/src/convert/traits.rs:243:8 | 243 | fn return_abi(self) -> Self::Abi; | ^^^^^^^^^^ = note: this error originates in the attribute macro `wasm_bindgen::prelude::__wasm_bindgen_class_marker` (in Nightly builds, run with -Z macro-backtrace for more info)
impl DocumentTransitionWasm {
#[wasm_bindgen(js_name=getId)]
pub fn get_id(&self) -> IdentifierWrapper {
Expand All @@ -42,6 +44,24 @@
self.0.document_type_name().to_owned()
}

#[wasm_bindgen(js_name=getData)]
pub fn get_data(&self) -> JsValue {
match &self.0 {
DocumentTransition::Create(document_create_transition) => {
let json_value = document_create_transition.data().to_json_value().unwrap();
json_value.serialize(&serde_wasm_bindgen::Serializer::json_compatible()).unwrap()
}
DocumentTransition::Replace(document_replace_transition) => {
let json_value = document_replace_transition.data().to_json_value().unwrap();
json_value.serialize(&serde_wasm_bindgen::Serializer::json_compatible()).unwrap()
}
DocumentTransition::Delete(document_delete_transition) => JsValue::null(),

Check warning on line 58 in packages/wasm-dpp/src/document/state_transition/document_batch_transition/document_transition/mod.rs

View workflow job for this annotation

GitHub Actions / Rust packages (wasm-dpp) / Linting

unused variable: `document_delete_transition`

warning: unused variable: `document_delete_transition` --> packages/wasm-dpp/src/document/state_transition/document_batch_transition/document_transition/mod.rs:58:40 | 58 | DocumentTransition::Delete(document_delete_transition) => JsValue::null(), | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_document_delete_transition` | = note: `#[warn(unused_variables)]` on by default
DocumentTransition::Transfer(document_transfer_transition) => JsValue::null(),

Check warning on line 59 in packages/wasm-dpp/src/document/state_transition/document_batch_transition/document_transition/mod.rs

View workflow job for this annotation

GitHub Actions / Rust packages (wasm-dpp) / Linting

unused variable: `document_transfer_transition`

warning: unused variable: `document_transfer_transition` --> packages/wasm-dpp/src/document/state_transition/document_batch_transition/document_transition/mod.rs:59:42 | 59 | DocumentTransition::Transfer(document_transfer_transition) => JsValue::null(), | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_document_transfer_transition`
DocumentTransition::UpdatePrice(document_update_price_transition) => JsValue::null(),

Check warning on line 60 in packages/wasm-dpp/src/document/state_transition/document_batch_transition/document_transition/mod.rs

View workflow job for this annotation

GitHub Actions / Rust packages (wasm-dpp) / Linting

unused variable: `document_update_price_transition`

warning: unused variable: `document_update_price_transition` --> packages/wasm-dpp/src/document/state_transition/document_batch_transition/document_transition/mod.rs:60:45 | 60 | DocumentTransition::UpdatePrice(document_update_price_transition) => JsValue::null(), | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_document_update_price_transition`
DocumentTransition::Purchase(document_purchase_transition) => JsValue::null()

Check warning on line 61 in packages/wasm-dpp/src/document/state_transition/document_batch_transition/document_transition/mod.rs

View workflow job for this annotation

GitHub Actions / Rust packages (wasm-dpp) / Linting

unused variable: `document_purchase_transition`

warning: unused variable: `document_purchase_transition` --> packages/wasm-dpp/src/document/state_transition/document_batch_transition/document_transition/mod.rs:61:42 | 61 | DocumentTransition::Purchase(document_purchase_transition) => JsValue::null() | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_document_purchase_transition`
}
}
Comment on lines +47 to +63
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add error handling for JSON conversion.

While the implementation is generally good, the unwrap calls on JSON conversion could lead to runtime panics.

Consider handling potential conversion errors:

     pub fn get_data(&self) -> JsValue {
         match &self.0 {
             DocumentTransition::Create(document_create_transition) => {
-                let json_value = document_create_transition.data().to_json_value().unwrap();
-                json_value.serialize(&serde_wasm_bindgen::Serializer::json_compatible()).unwrap()
+                document_create_transition.data()
+                    .to_json_value()
+                    .and_then(|json_value| 
+                        json_value.serialize(&serde_wasm_bindgen::Serializer::json_compatible())
+                    )
+                    .unwrap_or(JsValue::null())
             }
             DocumentTransition::Replace(document_replace_transition) => {
-                let json_value = document_replace_transition.data().to_json_value().unwrap();
-                json_value.serialize(&serde_wasm_bindgen::Serializer::json_compatible()).unwrap()
+                document_replace_transition.data()
+                    .to_json_value()
+                    .and_then(|json_value| 
+                        json_value.serialize(&serde_wasm_bindgen::Serializer::json_compatible())
+                    )
+                    .unwrap_or(JsValue::null())
             }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
#[wasm_bindgen(js_name=getData)]
pub fn get_data(&self) -> JsValue {
match &self.0 {
DocumentTransition::Create(document_create_transition) => {
let json_value = document_create_transition.data().to_json_value().unwrap();
json_value.serialize(&serde_wasm_bindgen::Serializer::json_compatible()).unwrap()
}
DocumentTransition::Replace(document_replace_transition) => {
let json_value = document_replace_transition.data().to_json_value().unwrap();
json_value.serialize(&serde_wasm_bindgen::Serializer::json_compatible()).unwrap()
}
DocumentTransition::Delete(document_delete_transition) => JsValue::null(),
DocumentTransition::Transfer(document_transfer_transition) => JsValue::null(),
DocumentTransition::UpdatePrice(document_update_price_transition) => JsValue::null(),
DocumentTransition::Purchase(document_purchase_transition) => JsValue::null()
}
}
#[wasm_bindgen(js_name=getData)]
pub fn get_data(&self) -> JsValue {
match &self.0 {
DocumentTransition::Create(document_create_transition) => {
document_create_transition.data()
.to_json_value()
.and_then(|json_value|
json_value.serialize(&serde_wasm_bindgen::Serializer::json_compatible())
)
.unwrap_or(JsValue::null())
}
DocumentTransition::Replace(document_replace_transition) => {
document_replace_transition.data()
.to_json_value()
.and_then(|json_value|
json_value.serialize(&serde_wasm_bindgen::Serializer::json_compatible())
)
.unwrap_or(JsValue::null())
}
DocumentTransition::Delete(document_delete_transition) => JsValue::null(),
DocumentTransition::Transfer(document_transfer_transition) => JsValue::null(),
DocumentTransition::UpdatePrice(document_update_price_transition) => JsValue::null(),
DocumentTransition::Purchase(document_purchase_transition) => JsValue::null()
}
}


#[wasm_bindgen(js_name=getAction)]
pub fn get_action(&self) -> u8 {
self.0.action_type() as u8
Expand All @@ -67,6 +87,17 @@
JsValue::NULL
}
}
#[wasm_bindgen(js_name=getEntropy)]
pub fn get_revision(&self) -> Vec<u8> {

Check failure on line 91 in packages/wasm-dpp/src/document/state_transition/document_batch_transition/document_transition/mod.rs

View workflow job for this annotation

GitHub Actions / Rust packages (wasm-dpp) / Linting

duplicate definitions with name `get_revision`

error[E0592]: duplicate definitions with name `get_revision` --> packages/wasm-dpp/src/document/state_transition/document_batch_transition/document_transition/mod.rs:91:5 | 83 | pub fn get_revision(&self) -> JsValue { | ------------------------------------- other definition for `get_revision` ... 91 | pub fn get_revision(&self) -> Vec<u8> { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ duplicate definitions for `get_revision`
match self.0.clone() {
DocumentTransition::Create(document_transition) => Vec::from(document_transition.entropy()),
DocumentTransition::Replace(document_transition) => Vec::from(document_transition.entropy()),

Check failure on line 94 in packages/wasm-dpp/src/document/state_transition/document_batch_transition/document_transition/mod.rs

View workflow job for this annotation

GitHub Actions / Rust packages (wasm-dpp) / Linting

no method named `entropy` found for enum `dpp::state_transition::documents_batch_transition::DocumentReplaceTransition` in the current scope

error[E0599]: no method named `entropy` found for enum `dpp::state_transition::documents_batch_transition::DocumentReplaceTransition` in the current scope --> packages/wasm-dpp/src/document/state_transition/document_batch_transition/document_transition/mod.rs:94:95 | 94 | DocumentTransition::Replace(document_transition) => Vec::from(document_transition.entropy()), | ^^^^^^^ method not found in `DocumentReplaceTransition`
DocumentTransition::Delete(document_transition) => Vec::from(document_transition.entropy()),

Check failure on line 95 in packages/wasm-dpp/src/document/state_transition/document_batch_transition/document_transition/mod.rs

View workflow job for this annotation

GitHub Actions / Rust packages (wasm-dpp) / Linting

no method named `entropy` found for enum `dpp::state_transition::documents_batch_transition::DocumentDeleteTransition` in the current scope

error[E0599]: no method named `entropy` found for enum `dpp::state_transition::documents_batch_transition::DocumentDeleteTransition` in the current scope --> packages/wasm-dpp/src/document/state_transition/document_batch_transition/document_transition/mod.rs:95:94 | 95 | DocumentTransition::Delete(document_transition) => Vec::from(document_transition.entropy()), | ^^^^^^^ method not found in `DocumentDeleteTransition`
DocumentTransition::Transfer(document_transition) => Vec::from(document_transition.entropy()),

Check failure on line 96 in packages/wasm-dpp/src/document/state_transition/document_batch_transition/document_transition/mod.rs

View workflow job for this annotation

GitHub Actions / Rust packages (wasm-dpp) / Linting

no method named `entropy` found for enum `dpp::state_transition::documents_batch_transition::document_transition::DocumentTransferTransition` in the current scope

error[E0599]: no method named `entropy` found for enum `dpp::state_transition::documents_batch_transition::document_transition::DocumentTransferTransition` in the current scope --> packages/wasm-dpp/src/document/state_transition/document_batch_transition/document_transition/mod.rs:96:96 | 96 | DocumentTransition::Transfer(document_transition) => Vec::from(document_transition.entropy()), | ^^^^^^^ method not found in `DocumentTransferTransition`
DocumentTransition::UpdatePrice(document_transition) => Vec::from(document_transition.entropy()),

Check failure on line 97 in packages/wasm-dpp/src/document/state_transition/document_batch_transition/document_transition/mod.rs

View workflow job for this annotation

GitHub Actions / Rust packages (wasm-dpp) / Linting

no method named `entropy` found for enum `dpp::state_transition::documents_batch_transition::document_transition::DocumentUpdatePriceTransition` in the current scope

error[E0599]: no method named `entropy` found for enum `dpp::state_transition::documents_batch_transition::document_transition::DocumentUpdatePriceTransition` in the current scope --> packages/wasm-dpp/src/document/state_transition/document_batch_transition/document_transition/mod.rs:97:99 | 97 | DocumentTransition::UpdatePrice(document_transition) => Vec::from(document_transition.entropy()), | ^^^^^^^ method not found in `DocumentUpdatePriceTransition`
DocumentTransition::Purchase(document_transition) => Vec::from(document_transition.entropy()),

Check failure on line 98 in packages/wasm-dpp/src/document/state_transition/document_batch_transition/document_transition/mod.rs

View workflow job for this annotation

GitHub Actions / Rust packages (wasm-dpp) / Linting

no method named `entropy` found for enum `dpp::state_transition::documents_batch_transition::document_transition::DocumentPurchaseTransition` in the current scope

error[E0599]: no method named `entropy` found for enum `dpp::state_transition::documents_batch_transition::document_transition::DocumentPurchaseTransition` in the current scope --> packages/wasm-dpp/src/document/state_transition/document_batch_transition/document_transition/mod.rs:98:96 | 98 | DocumentTransition::Purchase(document_transition) => Vec::from(document_transition.entropy()), | ^^^^^^^ method not found in `DocumentPurchaseTransition`
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix method name and optimize performance

There are two issues in this implementation:

  1. The method is declared as get_revision but implements entropy functionality, which will cause a compilation error due to the duplicate method name.
  2. The implementation unnecessarily clones the entire transition.

Apply this fix:

-    pub fn get_revision(&self) -> Vec<u8> {
-        match self.0.clone() {
+    pub fn get_entropy(&self) -> Vec<u8> {
+        match &self.0 {
             DocumentTransition::Create(document_transition) => Vec::from(document_transition.entropy()),
             DocumentTransition::Replace(document_transition) => Vec::from(document_transition.entropy()),
             DocumentTransition::Delete(document_transition) => Vec::from(document_transition.entropy()),
             DocumentTransition::Transfer(document_transition) => Vec::from(document_transition.entropy()),
             DocumentTransition::UpdatePrice(document_transition) => Vec::from(document_transition.entropy()),
             DocumentTransition::Purchase(document_transition) => Vec::from(document_transition.entropy()),
         }
     }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
#[wasm_bindgen(js_name=getEntropy)]
pub fn get_revision(&self) -> Vec<u8> {
match self.0.clone() {
DocumentTransition::Create(document_transition) => Vec::from(document_transition.entropy()),
DocumentTransition::Replace(document_transition) => Vec::from(document_transition.entropy()),
DocumentTransition::Delete(document_transition) => Vec::from(document_transition.entropy()),
DocumentTransition::Transfer(document_transition) => Vec::from(document_transition.entropy()),
DocumentTransition::UpdatePrice(document_transition) => Vec::from(document_transition.entropy()),
DocumentTransition::Purchase(document_transition) => Vec::from(document_transition.entropy()),
}
}
#[wasm_bindgen(js_name=getEntropy)]
pub fn get_entropy(&self) -> Vec<u8> {
match &self.0 {
DocumentTransition::Create(document_transition) => Vec::from(document_transition.entropy()),
DocumentTransition::Replace(document_transition) => Vec::from(document_transition.entropy()),
DocumentTransition::Delete(document_transition) => Vec::from(document_transition.entropy()),
DocumentTransition::Transfer(document_transition) => Vec::from(document_transition.entropy()),
DocumentTransition::UpdatePrice(document_transition) => Vec::from(document_transition.entropy()),
DocumentTransition::Purchase(document_transition) => Vec::from(document_transition.entropy()),
}
}


#[wasm_bindgen(js_name=setRevision)]
pub fn set_revision(&mut self, revision: u32) {
Expand All @@ -82,6 +113,32 @@
_ => false,
}
}

#[wasm_bindgen(js_name=getPrefundedVotingBalance)]
pub fn get_prefunded_voting_balance(&self) -> Result<JsValue, JsValue> {
match &self.0 {
DocumentTransition::Create(create_transition) => {
let prefunded_voting_balance = create_transition.prefunded_voting_balance().clone();

if prefunded_voting_balance.is_none() {
return Ok(JsValue::null())
}

let (index_name, credits) = prefunded_voting_balance.unwrap();

let js_object = js_sys::Object::new();

js_sys::Reflect::set(
&js_object,
&JsValue::from_str(&index_name),
&JsValue::from(credits),
)?;

Ok(JsValue::from(js_object))
}
_ => Ok(JsValue::null()),
}
}
}

impl From<DocumentTransition> for DocumentTransitionWasm {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@ impl DocumentsBatchTransitionWasm {
self.0.owner_id().to_owned().into()
}

#[wasm_bindgen(js_name=getUserFeeIncrease)]
pub fn get_user_fee_increase(&self) -> u16 {
self.0.user_fee_increase() as u16
}

#[wasm_bindgen(js_name=setUserFeeIncrease)]
pub fn set_user_fee_increase(&mut self, user_fee_increase: u16) {
self.0.set_user_fee_increase(user_fee_increase);
}

#[wasm_bindgen(js_name=getTransitions)]
pub fn get_transitions(&self) -> js_sys::Array {
let array = js_sys::Array::new();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,16 @@ impl IdentityCreateTransitionWasm {
(IdentityCreateTransitionAccessorsV0::owner_id(&self.0)).into()
}

#[wasm_bindgen(js_name=getUserFeeIncrease)]
pub fn get_user_fee_increase(&self) -> u16 {
self.0.user_fee_increase() as u16
}

#[wasm_bindgen(js_name=setUserFeeIncrease)]
pub fn set_user_fee_increase(&mut self, user_fee_increase: u16) {
self.0.set_user_fee_increase(user_fee_increase);
}

#[wasm_bindgen(js_name=toObject)]
pub fn to_object(&self, options: JsValue) -> Result<JsValue, JsValue> {
let opts: super::to_object::ToObjectOptions = if options.is_object() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,26 @@
self.0.set_amount(amount as u64);
}

#[wasm_bindgen(js_name=getUserFeeIncrease)]
pub fn get_user_fee_increase(&self) -> u16 {
self.0.user_fee_increase() as u16
}

#[wasm_bindgen(js_name=setUserFeeIncrease)]
pub fn set_user_fee_increase(&mut self, user_fee_increase: u16) {
self.0.set_user_fee_increase(user_fee_increase);
}

#[wasm_bindgen(js_name=getNonce)]
pub fn get_nonce(&self) -> u64 {
self.0.nonce()
}

#[wasm_bindgen(js_name=setNonce)]
pub fn set_nonce(&mut self, nonce: u64) -> () {

Check warning on line 118 in packages/wasm-dpp/src/identity/state_transition/identity_credit_transfer_transition/transition.rs

View workflow job for this annotation

GitHub Actions / Rust packages (wasm-dpp) / Linting

unneeded unit return type

warning: unneeded unit return type --> packages/wasm-dpp/src/identity/state_transition/identity_credit_transfer_transition/transition.rs:118:44 | 118 | pub fn set_nonce(&mut self, nonce: u64) -> () { | ^^^^^^ help: remove the `-> ()` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unused_unit
self.0.set_nonce(nonce)
}

#[wasm_bindgen(js_name=toObject)]
pub fn to_object(&self, options: JsValue) -> Result<JsValue, JsValue> {
let opts: super::to_object::ToObjectOptions = if options.is_object() {
Expand Down Expand Up @@ -318,6 +338,11 @@
.set_signature(BinaryData::new(signature.unwrap_or_default()))
}

#[wasm_bindgen(js_name=getSignaturePublicKeyId)]
pub fn get_signature_public_key_id(&self) -> u32 {
self.0.signature_public_key_id()
}

#[wasm_bindgen]
pub fn sign(
&mut self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,16 @@ impl IdentityCreditWithdrawalTransitionWasm {
self.0.set_nonce(revision);
}

#[wasm_bindgen(js_name=getUserFeeIncrease)]
pub fn get_user_fee_increase(&self) -> u16 {
self.0.user_fee_increase() as u16
}

#[wasm_bindgen(js_name=setUserFeeIncrease)]
pub fn set_user_fee_increase(&mut self, user_fee_increase: u16) {
self.0.set_user_fee_increase(user_fee_increase);
}

#[wasm_bindgen(js_name=toObject)]
pub fn to_object(&self, options: JsValue) -> Result<JsValue, JsValue> {
let opts: super::to_object::ToObjectOptions = if options.is_object() {
Expand Down Expand Up @@ -400,6 +410,11 @@ impl IdentityCreditWithdrawalTransitionWasm {
.set_signature(BinaryData::new(signature.unwrap_or_default()))
}

#[wasm_bindgen(js_name=getSignaturePublicKeyId)]
pub fn get_signature_public_key_id(&self) -> u32 {
self.0.signature_public_key_id()
}

#[wasm_bindgen]
pub fn sign(
&mut self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@ impl IdentityTopUpTransitionWasm {
self.0.owner_id().to_owned().into()
}

#[wasm_bindgen(js_name=getUserFeeIncrease)]
pub fn get_user_fee_increase(&self) -> u16 {
self.0.user_fee_increase() as u16
}

#[wasm_bindgen(js_name=setUserFeeIncrease)]
pub fn set_user_fee_increase(&mut self, user_fee_increase: u16) {
self.0.set_user_fee_increase(user_fee_increase);
}

#[wasm_bindgen(js_name=toObject)]
pub fn to_object(&self, options: JsValue) -> Result<JsValue, JsValue> {
let opts: super::to_object::ToObjectOptions = if options.is_object() {
Expand Down
Loading
Loading