-
Notifications
You must be signed in to change notification settings - Fork 39
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(sdk): implement document transfers in WASM DPP #2406
Changes from 9 commits
3b511ce
d3383a1
fae5915
fd48355
e17f1a1
5fd4329
10998f7
fbe5dbc
8f79b69
9925a68
e8defe8
1f0f7c6
43cc7f1
cf99fe6
44df2e6
5894a7f
d7d5d90
2131f00
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { Identity, ExtendedDocument } from '@dashevo/wasm-dpp'; | ||
import { Platform } from '../../Platform'; | ||
import broadcastStateTransition from '../../broadcastStateTransition'; | ||
import { signStateTransition } from '../../signStateTransition'; | ||
/** | ||
* Transfer document in the platform | ||
* | ||
* @param {Platform} this - bound instance class | ||
* @param {string} typeLocator - type locator | ||
* @param identity - identity | ||
* @param {Object} [data] - options | ||
* @returns {StateTransition} | ||
*/ | ||
export async function transfer( | ||
this: Platform, | ||
document: ExtendedDocument, | ||
receiver: Identity, | ||
sender: Identity, | ||
): Promise<any> { | ||
this.logger.debug('[Document#transfer] Transfer document'); | ||
await this.initialize(); | ||
|
||
const identityContractNonce = await this.nonceManager | ||
.bumpIdentityContractNonce(sender.getId(), document.getDataContractId()); | ||
|
||
const documentsBatchTransition = document | ||
.createTransferTransition(receiver.getId(), BigInt(identityContractNonce)); | ||
|
||
await signStateTransition(this, documentsBatchTransition, sender, 1); | ||
|
||
await broadcastStateTransition(this, documentsBatchTransition); | ||
} | ||
|
||
export default transfer; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -270,9 +270,22 @@ impl SpecializedDocumentFactoryV0 { | |
nonce_counter, | ||
platform_version, | ||
), | ||
_ => Err(ProtocolError::InvalidStateTransitionType( | ||
"action type not accounted for".to_string(), | ||
DocumentTransitionActionType::Transfer => { | ||
Err(ProtocolError::InvalidStateTransitionType( | ||
"action type not accounted for Transfer".to_string(), | ||
)) | ||
}, | ||
DocumentTransitionActionType::Purchase => { | ||
Err(ProtocolError::InvalidStateTransitionType( | ||
"action type not accounted for Purchase".to_string(), | ||
)) | ||
} | ||
DocumentTransitionActionType::UpdatePrice => Err(ProtocolError::InvalidStateTransitionType( | ||
"action type not accounted for UpdatePrice".to_string(), | ||
)), | ||
DocumentTransitionActionType::IgnoreWhileBumpingRevision => Err(ProtocolError::InvalidStateTransitionType( | ||
"action type not accounted for IgnoreWhileBumpingRevision".to_string(), | ||
)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Missing Transfer transition support. |
||
}) | ||
.collect::<Result<Vec<_>, ProtocolError>>()? | ||
.into_iter() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,8 @@ | ||
use dpp::document::{ | ||
DocumentV0Getters, DocumentV0Setters, ExtendedDocument, EXTENDED_DOCUMENT_IDENTIFIER_FIELDS, | ||
}; | ||
use dpp::document::{DocumentV0Getters, DocumentV0Setters, ExtendedDocument, EXTENDED_DOCUMENT_IDENTIFIER_FIELDS}; | ||
use serde_json::Value as JsonValue; | ||
|
||
use dpp::platform_value::{Bytes32, Value}; | ||
use dpp::prelude::{Identifier, Revision, TimestampMillis}; | ||
use dpp::prelude::{Identifier, IdentityNonce, Revision, TimestampMillis, UserFeeIncrease}; | ||
Check warning on line 5 in packages/wasm-dpp/src/document/extended_document.rs GitHub Actions / Rust packages (wasm-dpp) / Lintingunused import: `UserFeeIncrease`
|
||
|
||
use dpp::util::json_value::JsonValueExt; | ||
|
||
|
@@ -16,12 +14,15 @@ | |
use serde::{Deserialize, Serialize}; | ||
use std::convert::TryInto; | ||
use wasm_bindgen::prelude::*; | ||
use dpp::state_transition::documents_batch_transition::document_transition::DocumentTransferTransition; | ||
use dpp::state_transition::documents_batch_transition::{DocumentsBatchTransition, DocumentsBatchTransitionV0}; | ||
|
||
use crate::buffer::Buffer; | ||
use crate::data_contract::DataContractWasm; | ||
#[allow(deprecated)] // BinaryType is unsed in unused code below | ||
use crate::document::BinaryType; | ||
use crate::document::{ConversionOptions, DocumentWasm}; | ||
use crate::document_batch_transition::DocumentsBatchTransitionWasm; | ||
use crate::errors::RustConversionError; | ||
use crate::identifier::{identifier_from_js_value, IdentifierWrapper}; | ||
use crate::lodash::lodash_set; | ||
|
@@ -235,6 +236,33 @@ | |
.set_created_at(ts.map(|t| t.get_time() as TimestampMillis)); | ||
} | ||
|
||
#[wasm_bindgen(js_name=createTransferStateTransition)] | ||
pub fn create_transfer_state_transition(&mut self, recipient: IdentifierWrapper, identity_contract_nonce: IdentityNonce) -> DocumentsBatchTransitionWasm { | ||
let mut cloned_document = self.0.document().clone(); | ||
|
||
cloned_document.set_revision(Some(cloned_document.revision().unwrap() + 1)); | ||
|
||
let transfer_transition = DocumentTransferTransition::from_document( | ||
cloned_document, | ||
self.0.document_type().unwrap(), | ||
identity_contract_nonce, | ||
recipient.into(), | ||
PlatformVersion::latest(), | ||
None, | ||
None, | ||
).unwrap(); | ||
|
||
let documents_batch_transition: DocumentsBatchTransition = DocumentsBatchTransitionV0 { | ||
owner_id: self.0.owner_id(), | ||
transitions: vec![transfer_transition.into()], | ||
user_fee_increase: Default::default(), | ||
signature_public_key_id: Default::default(), | ||
signature: Default::default(), | ||
}.into(); | ||
|
||
documents_batch_transition.into() | ||
} | ||
Comment on lines
+239
to
+264
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check for None revision before incrementing. let mut cloned_document = self.0.document().clone();
- cloned_document.set_revision(Some(cloned_document.revision().unwrap() + 1));
+ if let Some(current_revision) = cloned_document.revision() {
+ cloned_document.set_revision(Some(current_revision + 1));
+ } else {
+ // Decide how to handle the absence of a revision, e.g., return an error or set a default revision
+ return Err(JsValue::from_str("Document is missing a revision"));
+ }
|
||
|
||
#[wasm_bindgen(js_name=setUpdatedAt)] | ||
pub fn set_updated_at(&mut self, ts: Option<js_sys::Date>) { | ||
self.0 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mismatch in error message for
Purchase
actionWhen
DocumentTransitionActionType::Purchase
is encountered, the error message incorrectly states "action type not accounted for Transfer" instead of referring to "Purchase." This might confuse developers or users.Consider applying the following fix:
📝 Committable suggestion