Skip to content

Commit

Permalink
Merge pull request #140 from aspectron/maturity_progress
Browse files Browse the repository at this point in the history
maturity progress helper functions
  • Loading branch information
aspect authored Jan 10, 2025
2 parents c6b2764 + 8ff010b commit 9873cee
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
11 changes: 11 additions & 0 deletions wallet/core/src/storage/transaction/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,17 @@ pub struct TransactionRecord {
pub metadata: Option<String>,
}

#[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;
Expand Down
28 changes: 28 additions & 0 deletions wallet/core/src/wasm/api/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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#"
Expand Down
34 changes: 33 additions & 1 deletion wallet/core/src/wasm/utils.rs
Original file line number Diff line number Diff line change
@@ -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::*;

Expand Down Expand Up @@ -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<INetworkParams> {
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<String> {
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())
}
}

0 comments on commit 9873cee

Please sign in to comment.