v0.2.12-pre.008-fix.002

This commit is contained in:
2026-08-27 15:18:34 +02:00
parent fe46cbe3bd
commit f0493df09e
13 changed files with 248 additions and 38 deletions

View File

@@ -1,5 +1,5 @@
// file: crates/ksp-app-wallet-desk/src/app_state.rs
// version: 20
// version: 21
//! Shared backend state owned by the Wallet Desk Tauri application.
@@ -1160,17 +1160,19 @@ impl AppState {
));
}
let lamports = result.value();
let sol_usd_equivalent = crate::sol_usd_equivalent(lamports, sol_usd_average.as_deref());
let dto = crate::WalletBalanceDto {
api_version: result.context().api_version().map(str::to_owned),
lamports: lamports.to_string(),
slot: result.context().slot(),
sol: crate::format_lamports_as_sol(lamports),
sol_usd_average,
sol_usd_equivalent,
transport_profile: self.transport_runtime.profile_id().to_owned(),
transport_role: self.transport_runtime.role().as_str().to_owned(),
wallet_id: context.wallet_id.clone(),
};
ksp_logging_lib::info!(target: crate::TRACING_TARGET, domain = crate::TRACING_DOMAIN_TRANSPORT, wallet_id = context.wallet_id.as_str(), capability = context.capability, lamports, slot = dto.slot, sol_usd_average_available = dto.sol_usd_average.is_some(), "Wallet balance refresh succeeded");
ksp_logging_lib::info!(target: crate::TRACING_TARGET, domain = crate::TRACING_DOMAIN_TRANSPORT, wallet_id = context.wallet_id.as_str(), capability = context.capability, lamports, slot = dto.slot, sol_usd_average_available = dto.sol_usd_average.is_some(), sol_usd_equivalent_available = dto.sol_usd_equivalent.is_some(), "Wallet balance refresh succeeded");
return std::result::Result::Ok(dto);
}

View File

@@ -1,5 +1,5 @@
// file: crates/ksp-app-wallet-desk/src/lib.rs
// version: 14
// version: 15
//! Tauri desktop application shell for KSP Wallet management and inspection.
@@ -168,6 +168,8 @@ pub(crate) use self::wallet_balance::WalletBalanceDto;
pub(crate) use self::wallet_balance::average_sol_usd_prices;
/// Formats exact lamports as a decimal SOL string without floating point.
pub(crate) use self::wallet_balance::format_lamports_as_sol;
/// Computes the exact USD equivalent of a lamport balance from the auxiliary SOL/USD average.
pub(crate) use self::wallet_balance::sol_usd_equivalent;
/// Resolved Wallet Config and directory preparation status captured during application bootstrap.
pub(crate) use self::wallet_config::WalletConfigStartup;
/// Resolves the composite-selected Wallet Config and prepares its application-owned directory tree.

View File

@@ -1,5 +1,5 @@
// file: crates/ksp-app-wallet-desk/src/wallet_balance.rs
// version: 3
// version: 4
//! Safe Wallet balance projection produced by the typed HTTP Transport path.
@@ -20,6 +20,8 @@ pub(crate) struct WalletBalanceDto {
pub(crate) sol: String,
/// Arithmetic mean of successful SOL/USD observations produced by this refresh, or unavailable when no provider returned one.
pub(crate) sol_usd_average: std::option::Option<String>,
/// Exact USD equivalent of the current SOL balance using `sol_usd_average`, or unavailable when the auxiliary price is unavailable.
pub(crate) sol_usd_equivalent: std::option::Option<String>,
/// Composite-selected Transport profile used for the RPC.
pub(crate) transport_profile: String,
/// Logical Transport role used for the RPC.
@@ -111,6 +113,58 @@ pub(crate) fn average_sol_usd_prices(prices: &[ksp_offchain_transport_lib::Marke
return std::option::Option::Some(format_scaled_decimal(coefficient, target_scale));
}
/// Computes the exact USD equivalent of a lamport balance from the current SOL/USD average.
///
/// The output is rounded half-up to at most eight fractional digits. Missing or invalid auxiliary price data and arithmetic overflow degrade to `None`; the
/// authoritative SOL balance remains unaffected.
#[must_use]
pub(crate) fn sol_usd_equivalent(lamports: u64, sol_usd_average: std::option::Option<&str>) -> std::option::Option<String> {
const LAMPORT_SCALE: u8 = 9;
const OUTPUT_SCALE: u8 = 8;
let source = match sol_usd_average {
std::option::Option::Some(value) => value,
std::option::Option::None => return std::option::Option::None,
};
let average = match ksp_offchain_transport_lib::MarketPriceDecimal::parse(source) {
std::result::Result::Ok(value) => value,
std::result::Result::Err(_) => return std::option::Option::None,
};
let coefficient = match u128::from(lamports).checked_mul(average.coefficient()) {
std::option::Option::Some(value) => value,
std::option::Option::None => return std::option::Option::None,
};
let source_scale = u16::from(LAMPORT_SCALE) + u16::from(average.scale());
let target_scale = u16::from(OUTPUT_SCALE);
let rounded_coefficient = if source_scale <= target_scale {
let exponent = u32::from(target_scale - source_scale);
let factor = match 10_u128.checked_pow(exponent) {
std::option::Option::Some(value) => value,
std::option::Option::None => return std::option::Option::None,
};
match coefficient.checked_mul(factor) {
std::option::Option::Some(value) => value,
std::option::Option::None => return std::option::Option::None,
}
} else {
let exponent = u32::from(source_scale - target_scale);
let divisor = match 10_u128.checked_pow(exponent) {
std::option::Option::Some(value) => value,
std::option::Option::None => return std::option::Option::None,
};
let quotient = coefficient / divisor;
let remainder = coefficient % divisor;
if remainder >= divisor.div_ceil(2) {
match quotient.checked_add(1) {
std::option::Option::Some(value) => value,
std::option::Option::None => return std::option::Option::None,
}
} else {
quotient
}
};
return std::option::Option::Some(format_scaled_decimal(rounded_coefficient, OUTPUT_SCALE));
}
fn format_scaled_decimal(mut coefficient: u128, mut scale: u8) -> String {
while scale > 0 && coefficient.is_multiple_of(10) {
coefficient /= 10;