From 813463bc2a723c3221f06e404da8c7e13a9a59be Mon Sep 17 00:00:00 2001 From: Quantum Explorer Date: Sun, 27 Sep 2026 18:45:12 +0700 Subject: [PATCH] fix(wasm-sdk): keep StateTransitionResult.ownerBalance exact in JSON #4887 added owner_balance to StateTransitionResult as a public u64 field without json_safe_fields, so toJSON emitted a plain number that loses precision above 2^53 credits, and wasm-bindgen exposed it as a writable snake_case property. Mark the struct json_safe_fields (large values become a string in the JSON form, as for every other credit amount) and expose the field through a read-only ownerBalance getter. Co-Authored-By: Claude Opus 5.5 --- packages/wasm-sdk/src/queries/system.rs | 36 ++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/packages/wasm-sdk/src/queries/system.rs b/packages/wasm-sdk/src/queries/system.rs index 71716411e9e..8f2460da8e1 100644 --- a/packages/wasm-sdk/src/queries/system.rs +++ b/packages/wasm-sdk/src/queries/system.rs @@ -1158,6 +1158,7 @@ impl PathElementWasm { } } +#[dpp_json_convertible_derive::json_safe_fields(crate = "dash_sdk::dpp")] #[wasm_bindgen(js_name = "StateTransitionResult")] #[derive(Clone, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] @@ -1172,7 +1173,7 @@ pub struct StateTransitionResultWasm { /// DAPI read it without a proof. Present when the SDK did not ask for a /// proof (it then asks for the balance); a proved wait of an owned, /// fee-paying transition carries the balance inside the proof instead. - pub owner_balance: Option, + owner_balance: Option, } impl StateTransitionResultWasm { @@ -1191,6 +1192,15 @@ impl StateTransitionResultWasm { } } +#[wasm_bindgen(js_class = StateTransitionResult)] +impl StateTransitionResultWasm { + /// The credit balance of the transition's owner after it executed, when DAPI reported it. + #[wasm_bindgen(getter = "ownerBalance")] + pub fn owner_balance(&self) -> Option { + self.owner_balance + } +} + #[wasm_bindgen] impl WasmSdk { #[wasm_bindgen(js_name = "getStatus")] @@ -1777,6 +1787,30 @@ impl WasmSdk { #[cfg(test)] mod tests { + /// A balance above JavaScript's safe integer range must survive the JSON form, which + /// `json_safe_fields` makes a string there, and stay a number below it. + #[test] + fn should_keep_a_large_owner_balance_exact_in_json() { + let large = (1u64 << 53) + 1; + let result = StateTransitionResultWasm::new( + "hash".to_string(), + "SUCCESS".to_string(), + None, + Some(large), + ); + let json = serde_json::to_value(&result).expect("expected to serialize"); + assert_eq!(json["ownerBalance"], serde_json::json!(large.to_string())); + + let small = StateTransitionResultWasm::new( + "hash".to_string(), + "SUCCESS".to_string(), + None, + Some(1_000), + ); + let json = serde_json::to_value(&small).expect("expected to serialize"); + assert_eq!(json["ownerBalance"], serde_json::json!(1_000)); + } + use super::*; use dash_sdk::drive::grovedb::element::reference_path::ReferencePathType;