Skip to content

Commit

Permalink
feat(dpp): extra methods for state transitions in wasm (#2401)
Browse files Browse the repository at this point in the history
  • Loading branch information
pshenmic committed Feb 14, 2025
1 parent 4349bfb commit 3ae5eee
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,45 @@ impl DocumentCreateTransitionWasm {
}
}

#[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) -> () {
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 @@ -200,7 +200,7 @@ impl DocumentReplaceTransitionWasm {
}

#[wasm_bindgen(js_name=setIdentityContractNonce)]
pub fn set_identity_contract_nonce(&mut self, identity_contract_nonce: u64) {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,34 @@ impl DocumentTransitionWasm {
}
}

#[wasm_bindgen(js_name=getReceiverId)]
pub fn get_receiver_id(&self) -> Option<IdentifierWrapper> {
match &self.0 {
DocumentTransition::Create(create) => None,
DocumentTransition::Replace(_) => None,
DocumentTransition::Delete(_) => None,
DocumentTransition::Transfer(transfer) => Some(transfer.recipient_owner_id().into()),
DocumentTransition::UpdatePrice(update_price) => None,
DocumentTransition::Purchase(purchase) => None,
}
}
#[wasm_bindgen(js_name=getEntropy)]
pub fn get_entropy(&self) -> Option<Vec<u8>> {
self.0.entropy()
}

#[wasm_bindgen(js_name=get_price)]
pub fn get_price(&self) -> Option<Credits> {
match &self.0 {
DocumentTransition::Create(create) => None,
DocumentTransition::Replace(_) => None,
DocumentTransition::Delete(_) => None,
DocumentTransition::Transfer(_) => None,
DocumentTransition::UpdatePrice(update_price) => Some(update_price.price()),
DocumentTransition::Purchase(purchase) => Some(purchase.price()),
}
}

#[wasm_bindgen(js_name=getReceiverId)]
pub fn get_receiver_id(&self) -> Option<IdentifierWrapper> {
match &self.0 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,26 @@ impl IdentityCreditTransferTransitionWasm {
self.0.set_nonce(nonce)
}

#[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) -> () {
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
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ impl IdentityUpdateTransitionWasm {
}

#[wasm_bindgen(js_name=setIdentityContractNonce)]
pub fn set_identity_contract_nonce(&mut self, identity_nonce: u64) {
pub fn set_identity_contract_nonce(&mut self, identity_nonce: u64) -> () {
self.0.set_nonce(identity_nonce)
}

Expand Down

0 comments on commit 3ae5eee

Please sign in to comment.