Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
42 changes: 38 additions & 4 deletions packages/wasm-sdk/src/state_transitions/contracts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
use dash_sdk::dpp::platform_value::string_encoding::Encoding;
use dash_sdk::dpp::state_transition::data_contract_update_transition::methods::DataContractUpdateTransitionMethodsV0;
use dash_sdk::dpp::state_transition::data_contract_update_transition::DataContractUpdateTransition;
use dash_sdk::dpp::state_transition::proof_result::StateTransitionProofResult;
use dash_sdk::dpp::state_transition::StateTransition;
use dash_sdk::platform::transition::broadcast::BroadcastStateTransition;
use dash_sdk::platform::transition::put_contract::PutContract;
use dash_sdk::platform::transition::waitable::Waitable;
use dash_sdk::platform::Fetch;
use js_sys;
use simple_signer::SingleKeySigner;
Expand Down Expand Up @@ -129,11 +132,24 @@
let signer = SingleKeySigner::from_string(&private_key_wif, self.network())
.map_err(WasmSdkError::invalid_argument)?;

// Create and broadcast the contract
let created_contract = data_contract
.put_to_platform_and_wait_for_response(&sdk, matching_key, &signer, None)
// Create the state transition (to get transaction hash)
let state_transition = data_contract
.put_to_platform(&sdk, matching_key, &signer, None)
.await?;

// Get transaction hash before broadcasting
let transaction_id_hex =
state_transition
.transaction_id()
.map(hex::encode)
.map_err(|e| {
WasmSdkError::generic(format!("Failed to compute transaction ID: {}", e))
})?;

// Broadcast and wait for the contract to be created
let created_contract =
DataContract::wait_for_response(&sdk, state_transition, None).await?;
Comment on lines +135 to +151
Copy link
Member

Choose a reason for hiding this comment

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

I think this opens us up to a race condition.


// Create JavaScript result object
let result_obj = js_sys::Object::new();

Expand Down Expand Up @@ -180,6 +196,13 @@
)
.map_err(|e| WasmSdkError::generic(format!("Failed to set documentTypes: {:?}", e)))?;

js_sys::Reflect::set(
&result_obj,
&JsValue::from_str("transitionHash"),
&JsValue::from_str(&transaction_id_hex),
)
.map_err(|e| WasmSdkError::generic(format!("Failed to set transitionHash: {:?}", e)))?;

js_sys::Reflect::set(
&result_obj,
&JsValue::from_str("message"),
Expand Down Expand Up @@ -354,11 +377,16 @@
.map_err(|e| WasmSdkError::generic(format!("Failed to create update transition: {}", e)))?;

// Broadcast the transition
use dash_sdk::dpp::state_transition::proof_result::StateTransitionProofResult;
let result = state_transition
.broadcast_and_wait::<StateTransitionProofResult>(&sdk, None)
.await?;

// Get transaction hash (consumes state_transition - no clone needed)
let st: StateTransition = state_transition.into();

Check failure on line 385 in packages/wasm-sdk/src/state_transitions/contracts/mod.rs

View workflow job for this annotation

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

useless conversion to the same type: `dash_sdk::dpp::state_transition::StateTransition`

error: useless conversion to the same type: `dash_sdk::dpp::state_transition::StateTransition` --> packages/wasm-sdk/src/state_transitions/contracts/mod.rs:385:35 | 385 | let st: StateTransition = state_transition.into(); | ^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into()`: `state_transition` | = help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.92.0/index.html#useless_conversion = note: `-D clippy::useless-conversion` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::useless_conversion)]`
let transaction_id_hex = st.transaction_id().map(hex::encode).map_err(|e| {
WasmSdkError::generic(format!("Failed to compute transaction ID: {}", e))
})?;

// Extract updated contract from result
let updated_version = match result {
StateTransitionProofResult::VerifiedDataContract(contract) => contract.version(),
Expand Down Expand Up @@ -386,6 +414,12 @@
&JsValue::from_f64(updated_version as f64),
)
.map_err(|e| WasmSdkError::generic(format!("Failed to set version: {:?}", e)))?;
js_sys::Reflect::set(
&result_obj,
&JsValue::from_str("transitionHash"),
&JsValue::from_str(&transaction_id_hex),
)
.map_err(|e| WasmSdkError::generic(format!("Failed to set transitionHash: {:?}", e)))?;
js_sys::Reflect::set(
&result_obj,
&JsValue::from_str("message"),
Expand Down
110 changes: 109 additions & 1 deletion packages/wasm-sdk/src/state_transitions/documents/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,12 @@
.await
.map_err(|e| WasmSdkError::generic(format!("Failed to broadcast transition: {}", e)))?;

// Get transaction hash (consumes state_transition - no clone needed)
let st: StateTransition = state_transition.into();

Check failure on line 330 in packages/wasm-sdk/src/state_transitions/documents/mod.rs

View workflow job for this annotation

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

useless conversion to the same type: `dash_sdk::dpp::state_transition::StateTransition`

error: useless conversion to the same type: `dash_sdk::dpp::state_transition::StateTransition` --> packages/wasm-sdk/src/state_transitions/documents/mod.rs:330:35 | 330 | let st: StateTransition = state_transition.into(); | ^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into()`: `state_transition` | = help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.92.0/index.html#useless_conversion
let transaction_id_hex = st.transaction_id().map(hex::encode).map_err(|e| {
WasmSdkError::generic(format!("Failed to compute transaction ID: {}", e))
})?;

// Log the result for debugging
tracing::debug!(
target = "wasm_sdk",
Expand Down Expand Up @@ -361,6 +367,13 @@
)
.unwrap();

js_sys::Reflect::set(
&js_result,
&JsValue::from_str("transitionHash"),
&JsValue::from_str(&transaction_id_hex),
)
.unwrap();

js_sys::Reflect::set(
&js_result,
&JsValue::from_str("documentId"),
Expand Down Expand Up @@ -470,6 +483,13 @@
)
.unwrap();

js_sys::Reflect::set(
&js_result,
&JsValue::from_str("transitionHash"),
&JsValue::from_str(&transaction_id_hex),
)
.unwrap();

js_sys::Reflect::set(
&js_result,
&JsValue::from_str("documentId"),
Expand Down Expand Up @@ -497,6 +517,13 @@
)
.unwrap();

js_sys::Reflect::set(
&js_result,
&JsValue::from_str("transitionHash"),
&JsValue::from_str(&transaction_id_hex),
)
.unwrap();

js_sys::Reflect::set(
&js_result,
&JsValue::from_str("documentId"),
Expand Down Expand Up @@ -525,6 +552,13 @@
)
.unwrap();

js_sys::Reflect::set(
&js_result,
&JsValue::from_str("transitionHash"),
&JsValue::from_str(&transaction_id_hex),
)
.unwrap();

js_sys::Reflect::set(
&js_result,
&JsValue::from_str("documentId"),
Expand Down Expand Up @@ -670,6 +704,12 @@
.await
.map_err(|e| WasmSdkError::generic(format!("Failed to broadcast transition: {}", e)))?;

// Get transaction hash (consumes state_transition - no clone needed)
let st: StateTransition = state_transition.into();

Check failure on line 708 in packages/wasm-sdk/src/state_transitions/documents/mod.rs

View workflow job for this annotation

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

useless conversion to the same type: `dash_sdk::dpp::state_transition::StateTransition`

error: useless conversion to the same type: `dash_sdk::dpp::state_transition::StateTransition` --> packages/wasm-sdk/src/state_transitions/documents/mod.rs:708:35 | 708 | let st: StateTransition = state_transition.into(); | ^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into()`: `state_transition` | = help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.92.0/index.html#useless_conversion
let transaction_id_hex = st.transaction_id().map(hex::encode).map_err(|e| {
WasmSdkError::generic(format!("Failed to compute transaction ID: {}", e))
})?;

// Convert result to JsValue based on the type
match proof_result {
StateTransitionProofResult::VerifiedDocuments(documents) => {
Expand All @@ -685,6 +725,13 @@
)
.unwrap();

js_sys::Reflect::set(
&js_result,
&JsValue::from_str("transitionHash"),
&JsValue::from_str(&transaction_id_hex),
)
.unwrap();

js_sys::Reflect::set(
&js_result,
&JsValue::from_str("documentId"),
Expand Down Expand Up @@ -794,6 +841,13 @@
)
.unwrap();

js_sys::Reflect::set(
&js_result,
&JsValue::from_str("transitionHash"),
&JsValue::from_str(&transaction_id_hex),
)
.unwrap();

js_sys::Reflect::set(
&js_result,
&JsValue::from_str("documentId"),
Expand Down Expand Up @@ -821,6 +875,13 @@
)
.unwrap();

js_sys::Reflect::set(
&js_result,
&JsValue::from_str("transitionHash"),
&JsValue::from_str(&transaction_id_hex),
)
.unwrap();

js_sys::Reflect::set(
&js_result,
&JsValue::from_str("documentId"),
Expand Down Expand Up @@ -849,6 +910,13 @@
)
.unwrap();

js_sys::Reflect::set(
&js_result,
&JsValue::from_str("transitionHash"),
&JsValue::from_str(&transaction_id_hex),
)
.unwrap();

js_sys::Reflect::set(
&js_result,
&JsValue::from_str("documentId"),
Expand Down Expand Up @@ -987,11 +1055,23 @@
.await
.map_err(|e| WasmSdkError::generic(format!("Failed to broadcast: {}", e)))?;

// Get transaction hash
let transaction_id_hex =
state_transition
.transaction_id()
.map(hex::encode)
.map_err(|e| {
WasmSdkError::generic(format!("Failed to compute transaction ID: {}", e))
})?;

// Return the result with document ID
Self::build_js_result_object(
"DocumentDeleted",
&document_id_base58,
vec![("deleted", JsValue::from_bool(true))],
vec![
("transitionHash", JsValue::from_str(&transaction_id_hex)),
("deleted", JsValue::from_bool(true)),
],
)
}

Expand Down Expand Up @@ -1118,11 +1198,21 @@
// Broadcast the state transition
state_transition.broadcast(&sdk, None).await?;

// Get transaction hash
let transaction_id_hex =
state_transition
.transaction_id()
.map(hex::encode)
.map_err(|e| {
WasmSdkError::generic(format!("Failed to compute transaction ID: {}", e))
})?;

// Return the result with document ID and new owner
Self::build_js_result_object(
"DocumentTransferred",
&document_id_base58,
vec![
("transitionHash", JsValue::from_str(&transaction_id_hex)),
("newOwnerId", JsValue::from_str(&recipient_base58)),
("transferred", JsValue::from_bool(true)),
],
Expand Down Expand Up @@ -1264,11 +1354,18 @@
.await
.map_err(|e| WasmSdkError::generic(format!("Failed to broadcast purchase: {}", e)))?;

// Get transaction hash (consumes transition - no clone needed)
let st: StateTransition = transition.into();

Check failure on line 1358 in packages/wasm-sdk/src/state_transitions/documents/mod.rs

View workflow job for this annotation

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

useless conversion to the same type: `dash_sdk::dpp::state_transition::StateTransition`

error: useless conversion to the same type: `dash_sdk::dpp::state_transition::StateTransition` --> packages/wasm-sdk/src/state_transitions/documents/mod.rs:1358:35 | 1358 | let st: StateTransition = transition.into(); | ^^^^^^^^^^^^^^^^^ help: consider removing `.into()`: `transition` | = help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.92.0/index.html#useless_conversion
let transaction_id_hex = st.transaction_id().map(hex::encode).map_err(|e| {
WasmSdkError::generic(format!("Failed to compute transaction ID: {}", e))
})?;

// Handle the proof result
match proof_result {
StateTransitionProofResult::VerifiedDocuments(documents) => {
// Document purchase was successful
let mut additional_fields = vec![
("transitionHash", JsValue::from_str(&transaction_id_hex)),
("status", JsValue::from_str("success")),
("newOwnerId", JsValue::from_str(&buyer_base58)),
("pricePaid", JsValue::from_f64(price as f64)),
Expand Down Expand Up @@ -1297,6 +1394,7 @@
"DocumentPurchased",
&document_id_base58,
vec![
("transitionHash", JsValue::from_str(&transaction_id_hex)),
("status", JsValue::from_str("success")),
("message", JsValue::from_str("Document purchase processed")),
],
Expand Down Expand Up @@ -1430,11 +1528,21 @@
// Broadcast the state transition
state_transition.broadcast(&sdk, None).await?;

// Get transaction hash
let transaction_id_hex =
state_transition
.transaction_id()
.map(hex::encode)
.map_err(|e| {
WasmSdkError::generic(format!("Failed to compute transaction ID: {}", e))
})?;

// Return the result with document ID and price
Self::build_js_result_object(
"DocumentPriceSet",
&document_id_base58,
vec![
("transitionHash", JsValue::from_str(&transaction_id_hex)),
("price", JsValue::from_f64(price as f64)),
("priceSet", JsValue::from_bool(true)),
],
Expand Down
Loading
Loading