From 8ff010b16f23357bda4d4a345febfaba5612ea6c Mon Sep 17 00:00:00 2001 From: Surinder Singh Matoo Date: Fri, 10 Jan 2025 20:26:42 +0530 Subject: [PATCH] maturity progress helper functions --- wallet/core/src/storage/transaction/record.rs | 11 ++++++ wallet/core/src/wasm/api/message.rs | 28 +++++++++++++++ wallet/core/src/wasm/utils.rs | 34 ++++++++++++++++++- 3 files changed, 72 insertions(+), 1 deletion(-) diff --git a/wallet/core/src/storage/transaction/record.rs b/wallet/core/src/storage/transaction/record.rs index 05be3b69f..05deb09b7 100644 --- a/wallet/core/src/storage/transaction/record.rs +++ b/wallet/core/src/storage/transaction/record.rs @@ -341,6 +341,17 @@ pub struct TransactionRecord { pub metadata: Option, } +#[wasm_bindgen] +impl TransactionRecord { + #[wasm_bindgen(js_name = maturityProgress)] + #[allow(non_snake_case)] + pub fn maturity_progress_js(&self, currentDaaScore: BigInt) -> String { + self.maturity_progress(currentDaaScore.try_as_u64().unwrap_or_default()) + .map(|progress| format!("{}", (progress * 100.) as usize)) + .unwrap_or_default() + } +} + impl TransactionRecord { const STORAGE_MAGIC: u32 = 0x5854414b; const STORAGE_VERSION: u32 = 0; diff --git a/wallet/core/src/wasm/api/message.rs b/wallet/core/src/wasm/api/message.rs index b4d791690..b94a24546 100644 --- a/wallet/core/src/wasm/api/message.rs +++ b/wallet/core/src/wasm/api/message.rs @@ -1700,6 +1700,34 @@ try_from! ( args: TransactionsDataGetResponse, ITransactionsDataGetResponse, { // --- +declare! { + INetworkParams, + r#" + /** + * + * + * @category Wallet API + */ + export interface INetworkParams { + coinbaseTransactionMaturityPeriodDaa : number; + coinbaseTransactionStasisPeriodDaa : number; + userTransactionMaturityPeriodDaa : number; + additionalCompoundTransactionMass : number; + } + "#, +} + +try_from! ( args: &NetworkParams, INetworkParams, { + let response = INetworkParams::default(); + response.set("coinbaseTransactionMaturityPeriodDaa", &to_value(&args.coinbase_transaction_maturity_period_daa)?)?; + response.set("coinbaseTransactionStasisPeriodDaa", &to_value(&args.coinbase_transaction_stasis_period_daa)?)?; + response.set("userTransactionMaturityPeriodDaa", &to_value(&args.user_transaction_maturity_period_daa)?)?; + response.set("additionalCompoundTransactionMass", &to_value(&args.additional_compound_transaction_mass)?)?; + Ok(response) +}); + +// --- + declare! { ITransactionsReplaceNoteRequest, r#" diff --git a/wallet/core/src/wasm/utils.rs b/wallet/core/src/wasm/utils.rs index a06c6136a..435b28fb7 100644 --- a/wallet/core/src/wasm/utils.rs +++ b/wallet/core/src/wasm/utils.rs @@ -1,6 +1,8 @@ +use crate::imports::NetworkParams; use crate::result::Result; +use crate::wasm::api::message::INetworkParams; use js_sys::BigInt; -use kaspa_consensus_core::network::{NetworkType, NetworkTypeT}; +use kaspa_consensus_core::network::{NetworkIdT, NetworkType, NetworkTypeT}; use wasm_bindgen::prelude::*; use workflow_wasm::prelude::*; @@ -44,3 +46,33 @@ pub fn sompi_to_kaspa_string_with_suffix(sompi: ISompiToKaspa, network: &Network let network_type = NetworkType::try_from(network)?; Ok(crate::utils::sompi_to_kaspa_string_with_suffix(sompi, &network_type)) } + +#[wasm_bindgen(js_name = "getNetworkParams")] +#[allow(non_snake_case)] +pub fn get_network_params(networkId: NetworkIdT) -> Result { + let params = NetworkParams::from(*networkId.try_into_cast()?); + Ok(params.try_into()?) +} + +#[wasm_bindgen(js_name = "getTransactionMaturityProgress")] +#[allow(non_snake_case)] +pub fn get_transaction_maturity_progress( + blockDaaScore: BigInt, + currentDaaScore: BigInt, + networkId: NetworkIdT, + isCoinbase: bool, +) -> Result { + let network_id = *networkId.try_into_cast()?; + let params = NetworkParams::from(network_id); + let block_daa_score = blockDaaScore.try_as_u64()?; + let current_daa_score = currentDaaScore.try_as_u64()?; + let maturity = + if isCoinbase { params.coinbase_transaction_maturity_period_daa() } else { params.user_transaction_maturity_period_daa() }; + + if current_daa_score < block_daa_score + maturity { + let progress = (current_daa_score - block_daa_score) as f64 / maturity as f64; + Ok(format!("{}", (progress * 100.) as usize)) + } else { + Ok("".to_string()) + } +}