v0.2.12-pre.008
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
// file: crates/ksp-app-wallet-desk/src/app_state.rs
|
||||
// version: 19
|
||||
// version: 20
|
||||
|
||||
//! Shared backend state owned by the Wallet Desk Tauri application.
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
pub(crate) struct AppState {
|
||||
config_management: ksp_config_lib::ConfigManagement,
|
||||
logging_runtime: std::sync::Mutex<LoggingRuntimeState>,
|
||||
offchain_runtime: std::option::Option<crate::OffchainTransportRuntime>,
|
||||
splash_settings: crate::SplashSettings,
|
||||
splash_sequence_started: std::sync::atomic::AtomicBool,
|
||||
transport_runtime: crate::TransportRuntime,
|
||||
@@ -41,6 +42,20 @@ impl AppState {
|
||||
return std::result::Result::Err(error);
|
||||
},
|
||||
};
|
||||
let offchain_runtime = crate::initialize_offchain_transport(&config_management);
|
||||
let offchain_runtime = match offchain_runtime {
|
||||
std::result::Result::Ok(value) => std::option::Option::Some(value),
|
||||
std::result::Result::Err(error) => {
|
||||
ksp_logging_lib::warn!(
|
||||
target: crate::TRACING_TARGET,
|
||||
domain = crate::TRACING_DOMAIN_OFFCHAIN_TRANSPORT,
|
||||
error_domain = error.code().domain(),
|
||||
error_code = error.code().code(),
|
||||
"Wallet Desk Off-chain Transport is unavailable; SOL/USD average will be N.A."
|
||||
);
|
||||
std::option::Option::None
|
||||
},
|
||||
};
|
||||
let wallet_config_startup = crate::initialize_wallet_config(&config_management);
|
||||
let wallet_config_startup = match wallet_config_startup {
|
||||
std::result::Result::Ok(value) => value,
|
||||
@@ -66,6 +81,7 @@ impl AppState {
|
||||
fallback_active: logging_startup.fallback_active,
|
||||
startup_diagnostic: logging_startup.startup_diagnostic,
|
||||
}),
|
||||
offchain_runtime,
|
||||
splash_settings,
|
||||
splash_sequence_started: std::sync::atomic::AtomicBool::new(false),
|
||||
transport_runtime,
|
||||
@@ -1126,8 +1142,10 @@ impl AppState {
|
||||
std::option::Option::Some(ksp_onchain_transport_lib::SolanaCommitment::Confirmed),
|
||||
std::option::Option::None,
|
||||
);
|
||||
ksp_logging_lib::debug!(target: crate::TRACING_TARGET, domain = crate::TRACING_DOMAIN_TRANSPORT, wallet_id = context.wallet_id.as_str(), capability = context.capability, transport_profile = self.transport_runtime.profile_id(), transport_role = self.transport_runtime.role().as_str(), "Wallet balance refresh started");
|
||||
let result = self.transport_runtime.pool().get_balance(self.transport_runtime.role(), &context.pubkey, std::option::Option::Some(&config)).await;
|
||||
ksp_logging_lib::debug!(target: crate::TRACING_TARGET, domain = crate::TRACING_DOMAIN_TRANSPORT, wallet_id = context.wallet_id.as_str(), capability = context.capability, transport_profile = self.transport_runtime.profile_id(), transport_role = self.transport_runtime.role().as_str(), "Wallet balance and auxiliary SOL/USD refresh started");
|
||||
let balance_future = self.transport_runtime.pool().get_balance(self.transport_runtime.role(), &context.pubkey, std::option::Option::Some(&config));
|
||||
let price_future = self.refresh_sol_usd_average();
|
||||
let (result, sol_usd_average) = tokio::join!(balance_future, price_future);
|
||||
let result = match result {
|
||||
std::result::Result::Ok(value) => value,
|
||||
std::result::Result::Err(error) => {
|
||||
@@ -1147,14 +1165,52 @@ impl AppState {
|
||||
lamports: lamports.to_string(),
|
||||
slot: result.context().slot(),
|
||||
sol: crate::format_lamports_as_sol(lamports),
|
||||
sol_usd_average,
|
||||
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, "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(), "Wallet balance refresh succeeded");
|
||||
return std::result::Result::Ok(dto);
|
||||
}
|
||||
|
||||
async fn refresh_sol_usd_average(&self) -> std::option::Option<String> {
|
||||
let runtime = match &self.offchain_runtime {
|
||||
std::option::Option::Some(value) => value,
|
||||
std::option::Option::None => return std::option::Option::None,
|
||||
};
|
||||
let outcomes = runtime.service().refresh_all().await;
|
||||
let outcomes = match outcomes {
|
||||
std::result::Result::Ok(value) => value,
|
||||
std::result::Result::Err(error) => {
|
||||
ksp_logging_lib::warn!(
|
||||
target: crate::TRACING_TARGET,
|
||||
domain = crate::TRACING_DOMAIN_OFFCHAIN_TRANSPORT,
|
||||
offchain_profile = runtime.profile_id(),
|
||||
error_domain = error.code().domain(),
|
||||
error_code = error.code().code(),
|
||||
"Wallet Desk SOL/USD provider refresh failed; average is unavailable"
|
||||
);
|
||||
return std::option::Option::None;
|
||||
},
|
||||
};
|
||||
let prices = outcomes
|
||||
.iter()
|
||||
.filter_map(|outcome| return outcome.observation().map(|observation| return observation.price()))
|
||||
.collect::<std::vec::Vec<_>>();
|
||||
let average = crate::average_sol_usd_prices(prices.as_slice());
|
||||
ksp_logging_lib::debug!(
|
||||
target: crate::TRACING_TARGET,
|
||||
domain = crate::TRACING_DOMAIN_OFFCHAIN_TRANSPORT,
|
||||
offchain_profile = runtime.profile_id(),
|
||||
provider_count = outcomes.len(),
|
||||
observed_provider_count = prices.len(),
|
||||
average_available = average.is_some(),
|
||||
"Wallet Desk SOL/USD refresh completed"
|
||||
);
|
||||
return average;
|
||||
}
|
||||
|
||||
fn authorized_balance_context(&self) -> ksp_core_lib::Result<WalletBalanceContext> {
|
||||
let session = self.wallet_session.lock();
|
||||
let session = match session {
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
// file: crates/ksp-app-wallet-desk/src/constants.rs
|
||||
// version: 8
|
||||
// version: 9
|
||||
|
||||
//! Logging targets, domains and composite component identifiers owned by Wallet Desk.
|
||||
|
||||
/// Composite-local identifier for the Logging standard document.
|
||||
pub(crate) const COMPOSITE_COMPONENT_ID_LOGGING: &str = "logging";
|
||||
/// Composite-local identifier for the Off-chain Transport standard document.
|
||||
pub(crate) const COMPOSITE_COMPONENT_ID_OFFCHAIN_TRANSPORT: &str = "offchain_transport";
|
||||
/// Composite-local identifier for the HTTP Transport standard document.
|
||||
pub(crate) const COMPOSITE_COMPONENT_ID_TRANSPORT: &str = "transport";
|
||||
/// Composite-local identifier for the Wallet standard document.
|
||||
@@ -13,6 +15,8 @@ pub(crate) const COMPOSITE_COMPONENT_ID_WALLET: &str = "wallet";
|
||||
pub(crate) const TRACING_DOMAIN_BOOTSTRAP: &str = "wallet.bootstrap";
|
||||
/// Structured domain used by technical frontend events.
|
||||
pub(crate) const TRACING_DOMAIN_FRONTEND: &str = "frontend";
|
||||
/// Structured domain used by Wallet Desk Off-chain Transport and SOL/USD average operations.
|
||||
pub(crate) const TRACING_DOMAIN_OFFCHAIN_TRANSPORT: &str = "wallet.offchain_transport";
|
||||
/// Structured domain used by the Wallet Desk shell.
|
||||
pub(crate) const TRACING_DOMAIN_SHELL: &str = "wallet.shell";
|
||||
/// Structured domain used by Wallet Desk HTTP Transport and balance operations.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// file: crates/ksp-app-wallet-desk/src/lib.rs
|
||||
// version: 13
|
||||
// version: 14
|
||||
|
||||
//! Tauri desktop application shell for KSP Wallet management and inspection.
|
||||
|
||||
@@ -14,6 +14,7 @@ mod dto_common;
|
||||
mod errors;
|
||||
mod frontend_logging;
|
||||
mod logging_runtime;
|
||||
mod offchain_runtime;
|
||||
mod splash;
|
||||
mod tauri;
|
||||
mod transport_runtime;
|
||||
@@ -47,6 +48,8 @@ pub(crate) use self::bootstrap::load_wallet_desk_composite;
|
||||
pub(crate) use self::bootstrap::required_composite_component_profile;
|
||||
/// Composite-local identifier for the Logging standard document.
|
||||
pub(crate) use self::constants::COMPOSITE_COMPONENT_ID_LOGGING;
|
||||
/// Composite-local identifier for the Off-chain Transport standard document.
|
||||
pub(crate) use self::constants::COMPOSITE_COMPONENT_ID_OFFCHAIN_TRANSPORT;
|
||||
/// Composite-local identifier for the HTTP Transport standard document.
|
||||
pub(crate) use self::constants::COMPOSITE_COMPONENT_ID_TRANSPORT;
|
||||
/// Composite-local identifier for the Wallet standard document.
|
||||
@@ -55,6 +58,8 @@ pub(crate) use self::constants::COMPOSITE_COMPONENT_ID_WALLET;
|
||||
pub(crate) use self::constants::TRACING_DOMAIN_BOOTSTRAP;
|
||||
/// Structured domain used by technical frontend events.
|
||||
pub(crate) use self::constants::TRACING_DOMAIN_FRONTEND;
|
||||
/// Structured domain used by Wallet Desk Off-chain Transport and SOL/USD average operations.
|
||||
pub(crate) use self::constants::TRACING_DOMAIN_OFFCHAIN_TRANSPORT;
|
||||
/// Structured domain used by the Wallet Desk shell.
|
||||
pub(crate) use self::constants::TRACING_DOMAIN_SHELL;
|
||||
/// Structured domain used by Wallet Desk HTTP Transport and balance operations.
|
||||
@@ -137,6 +142,10 @@ pub(crate) use self::frontend_logging::FrontendLogPayloadDto;
|
||||
pub(crate) use self::frontend_logging::emit_frontend_log_event;
|
||||
/// Creates the stable runtime identity for this Wallet Desk process launch.
|
||||
pub(crate) use self::logging_runtime::launch_identity;
|
||||
/// Provider-neutral Off-chain Transport runtime used by the Wallet balance projection.
|
||||
pub(crate) use self::offchain_runtime::OffchainTransportRuntime;
|
||||
/// Resolves the optional composite-selected Off-chain Transport runtime.
|
||||
pub(crate) use self::offchain_runtime::initialize_offchain_transport;
|
||||
/// Command emitted by Rust to the splash frontend.
|
||||
pub(crate) use self::splash::SplashOrderDto;
|
||||
/// Runtime timings used by the common desk splash lifecycle.
|
||||
@@ -155,6 +164,8 @@ pub(crate) use self::tw_splash::require_splash_window;
|
||||
pub(crate) use self::tw_splash::splash_frontend_ready_service;
|
||||
/// Safe balance snapshot for the currently authorized Wallet.
|
||||
pub(crate) use self::wallet_balance::WalletBalanceDto;
|
||||
/// Computes the Wallet Desk SOL/USD arithmetic mean without floating point.
|
||||
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;
|
||||
/// Resolved Wallet Config and directory preparation status captured during application bootstrap.
|
||||
|
||||
60
crates/ksp-app-wallet-desk/src/offchain_runtime.rs
Normal file
60
crates/ksp-app-wallet-desk/src/offchain_runtime.rs
Normal file
@@ -0,0 +1,60 @@
|
||||
// file: crates/ksp-app-wallet-desk/src/offchain_runtime.rs
|
||||
// version: 1
|
||||
|
||||
//! Optional composite-selected Off-chain Transport runtime used by Wallet Desk SOL/USD presentation.
|
||||
|
||||
/// Safe provider-neutral Off-chain Transport runtime retained by Wallet Desk when Config resolution succeeds.
|
||||
pub(crate) struct OffchainTransportRuntime {
|
||||
resolved: ksp_config_lib::ResolvedOffchainTransportConfig,
|
||||
}
|
||||
|
||||
impl OffchainTransportRuntime {
|
||||
/// Returns the generic market-price service built and owned by Config.
|
||||
#[must_use]
|
||||
pub(crate) const fn service(&self) -> &ksp_offchain_transport_lib::MarketPriceService {
|
||||
return self.resolved.service();
|
||||
}
|
||||
|
||||
/// Returns the composite-selected Off-chain Transport profile identifier.
|
||||
#[must_use]
|
||||
pub(crate) fn profile_id(&self) -> &str {
|
||||
return self.resolved.profile_id();
|
||||
}
|
||||
}
|
||||
|
||||
/// Resolves the Wallet Desk Off-chain Transport component through Config.
|
||||
pub(crate) fn initialize_offchain_transport(management: &ksp_config_lib::ConfigManagement) -> ksp_core_lib::Result<OffchainTransportRuntime> {
|
||||
let environment = ksp_config_lib::ConfigEnvironment::load();
|
||||
let environment = match environment {
|
||||
std::result::Result::Ok(value) => value,
|
||||
std::result::Result::Err(error) => return std::result::Result::Err(error),
|
||||
};
|
||||
let composite = crate::load_wallet_desk_composite(management);
|
||||
let composite = match composite {
|
||||
std::result::Result::Ok(value) => value,
|
||||
std::result::Result::Err(error) => return std::result::Result::Err(error),
|
||||
};
|
||||
let profile = crate::required_composite_component_profile(
|
||||
&composite,
|
||||
crate::COMPOSITE_COMPONENT_ID_OFFCHAIN_TRANSPORT,
|
||||
ksp_config_lib::FILE_ID_STD_OFFCHAIN_TRANSPORT,
|
||||
);
|
||||
let profile = match profile {
|
||||
std::result::Result::Ok(value) => value,
|
||||
std::result::Result::Err(error) => return std::result::Result::Err(error),
|
||||
};
|
||||
let resolved = management.engine().resolve_offchain_transport_config_profile(&profile, &environment);
|
||||
let resolved = match resolved {
|
||||
std::result::Result::Ok(value) => value,
|
||||
std::result::Result::Err(error) => return std::result::Result::Err(error),
|
||||
};
|
||||
let provider_count = resolved.service().registry().len();
|
||||
ksp_logging_lib::info!(
|
||||
target: crate::TRACING_TARGET,
|
||||
domain = crate::TRACING_DOMAIN_OFFCHAIN_TRANSPORT,
|
||||
offchain_profile = resolved.profile_id(),
|
||||
provider_count,
|
||||
"initialized Wallet Desk Off-chain Transport from composite-managed configuration"
|
||||
);
|
||||
return std::result::Result::Ok(OffchainTransportRuntime { resolved });
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
// file: crates/ksp-app-wallet-desk/src/wallet_balance.rs
|
||||
// version: 1
|
||||
// version: 2
|
||||
|
||||
//! Safe Wallet balance projection produced by the typed HTTP Transport path.
|
||||
|
||||
@@ -18,6 +18,8 @@ pub(crate) struct WalletBalanceDto {
|
||||
pub(crate) slot: u64,
|
||||
/// Exact decimal SOL presentation derived from lamports without floating point.
|
||||
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>,
|
||||
/// Composite-selected Transport profile used for the RPC.
|
||||
pub(crate) transport_profile: String,
|
||||
/// Logical Transport role used for the RPC.
|
||||
@@ -34,6 +36,98 @@ pub(crate) fn format_lamports_as_sol(lamports: u64) -> String {
|
||||
return format!("{whole}.{fractional:09}");
|
||||
}
|
||||
|
||||
/// Computes a deterministic SOL/USD arithmetic mean without IEEE-754 conversion.
|
||||
///
|
||||
/// The output is rounded half-up to at most eight fractional digits and trailing zeroes are removed. Arithmetic overflow degrades to `None`, which Wallet Desk
|
||||
/// renders as `N.A.` without affecting the authoritative SOL balance.
|
||||
#[must_use]
|
||||
pub(crate) fn average_sol_usd_prices(prices: &[ksp_offchain_transport_lib::MarketPriceDecimal]) -> std::option::Option<String> {
|
||||
const OUTPUT_SCALE: u8 = 8;
|
||||
if prices.is_empty() {
|
||||
return std::option::Option::None;
|
||||
}
|
||||
let max_scale = prices.iter().map(ksp_offchain_transport_lib::MarketPriceDecimal::scale).max();
|
||||
let max_scale = match max_scale {
|
||||
std::option::Option::Some(value) => value,
|
||||
std::option::Option::None => return std::option::Option::None,
|
||||
};
|
||||
let mut sum = 0_u128;
|
||||
for price in prices {
|
||||
let exponent = u32::from(max_scale.saturating_sub(price.scale()));
|
||||
let factor = 10_u128.checked_pow(exponent);
|
||||
let factor = match factor {
|
||||
std::option::Option::Some(value) => value,
|
||||
std::option::Option::None => return std::option::Option::None,
|
||||
};
|
||||
let coefficient = price.coefficient().checked_mul(factor);
|
||||
let coefficient = match coefficient {
|
||||
std::option::Option::Some(value) => value,
|
||||
std::option::Option::None => return std::option::Option::None,
|
||||
};
|
||||
sum = match sum.checked_add(coefficient) {
|
||||
std::option::Option::Some(value) => value,
|
||||
std::option::Option::None => return std::option::Option::None,
|
||||
};
|
||||
}
|
||||
let count = match u128::try_from(prices.len()) {
|
||||
std::result::Result::Ok(value) if value > 0 => value,
|
||||
_ => return std::option::Option::None,
|
||||
};
|
||||
let target_scale = OUTPUT_SCALE;
|
||||
let (numerator, denominator) = if max_scale <= target_scale {
|
||||
let factor = 10_u128.checked_pow(u32::from(target_scale - max_scale));
|
||||
let factor = match factor {
|
||||
std::option::Option::Some(value) => value,
|
||||
std::option::Option::None => return std::option::Option::None,
|
||||
};
|
||||
let numerator = match sum.checked_mul(factor) {
|
||||
std::option::Option::Some(value) => value,
|
||||
std::option::Option::None => return std::option::Option::None,
|
||||
};
|
||||
(numerator, count)
|
||||
} else {
|
||||
let factor = 10_u128.checked_pow(u32::from(max_scale - target_scale));
|
||||
let factor = match factor {
|
||||
std::option::Option::Some(value) => value,
|
||||
std::option::Option::None => return std::option::Option::None,
|
||||
};
|
||||
let denominator = match count.checked_mul(factor) {
|
||||
std::option::Option::Some(value) => value,
|
||||
std::option::Option::None => return std::option::Option::None,
|
||||
};
|
||||
(sum, denominator)
|
||||
};
|
||||
let quotient = numerator / denominator;
|
||||
let remainder = numerator % denominator;
|
||||
let round_up = remainder >= denominator.div_ceil(2);
|
||||
let coefficient = if round_up {
|
||||
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(coefficient, target_scale));
|
||||
}
|
||||
|
||||
fn format_scaled_decimal(mut coefficient: u128, mut scale: u8) -> String {
|
||||
while scale > 0 && coefficient % 10 == 0 {
|
||||
coefficient /= 10;
|
||||
scale -= 1;
|
||||
}
|
||||
let digits = coefficient.to_string();
|
||||
if scale == 0 {
|
||||
return digits;
|
||||
}
|
||||
let scale = usize::from(scale);
|
||||
if digits.len() > scale {
|
||||
let split = digits.len() - scale;
|
||||
return format!("{}.{}", &digits[..split], &digits[split..]);
|
||||
}
|
||||
return format!("0.{}{}", "0".repeat(scale - digits.len()), digits);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "../unit_tests/wallet_balance.rs"]
|
||||
mod tests;
|
||||
|
||||
Reference in New Issue
Block a user