Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/backend_task/platform_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,16 @@ impl AppContext {
PlatformInfoTaskRequestType::CurrentEpochInfo => {
match ExtendedEpochInfo::fetch_current(&sdk).await {
Ok(epoch_info) => {
let formatted = format_extended_epoch_info(epoch_info, self.network, true);
// Cache the fee multiplier for UI fee estimation
let fee_multiplier = epoch_info.fee_multiplier_permille();
self.set_fee_multiplier_permille(fee_multiplier);

let mut formatted =
format_extended_epoch_info(epoch_info, self.network, true);
formatted.push_str(&format!(
"\n\n(Fee multiplier cache updated: {}x)",
fee_multiplier as f64 / 1000.0
));
Ok(BackendTaskSuccessResult::PlatformInfo(
PlatformInfoTaskResult::TextResult(formatted),
))
Expand Down
25 changes: 24 additions & 1 deletion src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::context_provider::Provider as RpcProvider;
use crate::context_provider_spv::SpvProvider;
use crate::database::Database;
use crate::model::contested_name::ContestedName;
use crate::model::fee_estimation::PlatformFeeEstimator;
use crate::model::password_info::PasswordInfo;
use crate::model::qualified_contract::QualifiedContract;
use crate::model::qualified_identity::{DPNSNameInfo, QualifiedIdentity};
Expand Down Expand Up @@ -49,7 +50,7 @@ use dash_sdk::query_types::IndexMap;
use egui::Context;
use rusqlite::Result;
use std::collections::{BTreeMap, HashMap};
use std::sync::atomic::{AtomicBool, AtomicU8, Ordering};
use std::sync::atomic::{AtomicBool, AtomicU8, AtomicU64, Ordering};
use std::sync::{Arc, Mutex, RwLock, RwLockWriteGuard};

const ANIMATION_REFRESH_TIME: std::time::Duration = std::time::Duration::from_millis(100);
Expand Down Expand Up @@ -106,6 +107,9 @@ pub struct AppContext {
pub(crate) selected_wallet_hash: Mutex<Option<WalletSeedHash>>,
/// Currently selected single key wallet (persisted across screen navigation)
pub(crate) selected_single_key_hash: Mutex<Option<SingleKeyHash>>,
/// Cached fee multiplier permille from current epoch (1000 = 1x, 2000 = 2x)
/// Updated when epoch info is fetched from Platform
fee_multiplier_permille: AtomicU64,
}

impl AppContext {
Expand Down Expand Up @@ -275,6 +279,7 @@ impl AppContext {
pending_wallet_selection: Mutex::new(None),
selected_wallet_hash: Mutex::new(selected_wallet_hash),
selected_single_key_hash: Mutex::new(selected_single_key_hash),
fee_multiplier_permille: AtomicU64::new(1000), // Default 1x multiplier
};

let app_context = Arc::new(app_context);
Expand Down Expand Up @@ -395,6 +400,24 @@ impl AppContext {
}
}

/// Get the cached fee multiplier permille (1000 = 1x, 2000 = 2x)
pub fn fee_multiplier_permille(&self) -> u64 {
self.fee_multiplier_permille.load(Ordering::Relaxed)
}

/// Update the cached fee multiplier from epoch info
pub fn set_fee_multiplier_permille(&self, multiplier: u64) {
self.fee_multiplier_permille
.store(multiplier, Ordering::Relaxed);
}

/// Get a fee estimator configured with the cached fee multiplier.
/// Use this instead of `PlatformFeeEstimator::new()` to get accurate fee estimates
/// that reflect the current network fee multiplier.
pub fn fee_estimator(&self) -> PlatformFeeEstimator {
PlatformFeeEstimator::with_fee_multiplier(self.fee_multiplier_permille())
}

pub fn spv_manager(&self) -> &Arc<SpvManager> {
&self.spv_manager
}
Expand Down
46 changes: 41 additions & 5 deletions src/model/fee_estimation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ pub struct PlatformFeeEstimator {
min_fees: StateTransitionMinFees,
storage_fees: StorageFeeConstants,
registration_fees: DataContractRegistrationFees,
/// Fee multiplier in permille (1000 = 1x, 2000 = 2x, etc.)
/// This comes from the current epoch's fee_multiplier_permille()
fee_multiplier_permille: u64,
}

impl Default for PlatformFeeEstimator {
Expand All @@ -150,11 +153,25 @@ impl Default for PlatformFeeEstimator {
}

impl PlatformFeeEstimator {
/// Default fee multiplier (1x = 1000 permille)
pub const DEFAULT_FEE_MULTIPLIER_PERMILLE: u64 = 1000;

pub fn new() -> Self {
Self {
min_fees: StateTransitionMinFees::default(),
storage_fees: StorageFeeConstants::default(),
registration_fees: DataContractRegistrationFees::default(),
fee_multiplier_permille: Self::DEFAULT_FEE_MULTIPLIER_PERMILLE,
}
}

/// Create an estimator with a specific fee multiplier (from epoch info)
pub fn with_fee_multiplier(fee_multiplier_permille: u64) -> Self {
Self {
min_fees: StateTransitionMinFees::default(),
storage_fees: StorageFeeConstants::default(),
registration_fees: DataContractRegistrationFees::default(),
fee_multiplier_permille,
}
}

Expand All @@ -164,6 +181,19 @@ impl PlatformFeeEstimator {
Self::new()
}

/// Apply the fee multiplier to a base fee amount.
/// Multiplier is in permille: 1000 = 1x, 1500 = 1.5x, 2000 = 2x
fn apply_multiplier(&self, base_fee: u64) -> u64 {
base_fee
.saturating_mul(self.fee_multiplier_permille)
.saturating_div(1000)
}

/// Get the current fee multiplier permille
pub fn fee_multiplier_permille(&self) -> u64 {
self.fee_multiplier_permille
}

/// Calculate storage fee for a given number of bytes.
/// This is the main cost component for storing data on Platform.
pub fn calculate_storage_fee(&self, bytes: usize) -> u64 {
Expand All @@ -183,10 +213,13 @@ impl PlatformFeeEstimator {

/// Estimate total storage-based fee for storing data.
/// Includes storage, processing, and estimated seek costs.
/// Applies the current fee multiplier.
pub fn estimate_storage_based_fee(&self, bytes: usize, estimated_seeks: usize) -> u64 {
self.calculate_storage_fee(bytes)
let base_fee = self
.calculate_storage_fee(bytes)
.saturating_add(self.calculate_processing_fee(bytes))
.saturating_add(self.calculate_seek_fee(estimated_seeks))
.saturating_add(self.calculate_seek_fee(estimated_seeks));
self.apply_multiplier(base_fee)
}

/// Estimate fee for credit transfer between identities
Expand Down Expand Up @@ -478,16 +511,19 @@ impl PlatformFeeEstimator {
self.min_fees.masternode_vote
}

/// Estimate fee for address funds transfer
/// Estimate fee for address funds transfer.
/// Applies the current fee multiplier.
pub fn estimate_address_funds_transfer(&self, input_count: usize, output_count: usize) -> u64 {
self.min_fees
let base_fee = self
.min_fees
.address_funds_transfer_input_cost
.saturating_mul(input_count as u64)
.saturating_add(
self.min_fees
.address_funds_transfer_output_cost
.saturating_mul(output_count.max(1) as u64),
)
);
self.apply_multiplier(base_fee)
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

/// Get the raw minimum fees structure
Expand Down
4 changes: 2 additions & 2 deletions src/ui/contracts_documents/document_action_screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::backend_task::BackendTaskSuccessResult;
use crate::backend_task::FeeResult;
use crate::backend_task::{BackendTask, document::DocumentTask};
use crate::context::AppContext;
use crate::model::fee_estimation::{PlatformFeeEstimator, format_credits_as_dash};
use crate::model::fee_estimation::format_credits_as_dash;
use crate::model::qualified_contract::QualifiedContract;
use crate::model::qualified_identity::QualifiedIdentity;
use crate::model::wallet::Wallet;
Expand Down Expand Up @@ -871,7 +871,7 @@ impl DocumentActionScreen {
let mut action = AppAction::None;

// Fee estimation display
let fee_estimator = PlatformFeeEstimator::new();
let fee_estimator = self.app_context.fee_estimator();
let estimated_fee = match self.action_type {
DocumentActionType::Create => fee_estimator.estimate_document_create(),
DocumentActionType::Delete => fee_estimator.estimate_document_delete(),
Expand Down
4 changes: 3 additions & 1 deletion src/ui/contracts_documents/register_contract_screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,9 @@ impl RegisterDataContractScreen {
let registration_fee = contract.registration_cost(platform_version).unwrap_or(0);
// Add storage and processing fees for the contract data
let contract_size = self.contract_json_input.len();
let storage_fee = crate::model::fee_estimation::PlatformFeeEstimator::new()
let storage_fee = self
.app_context
.fee_estimator()
.estimate_storage_based_fee(contract_size, 20); // ~20 seeks for tree operations
let estimated_fee = registration_fee.saturating_add(storage_fee);
ui.add_space(10.0);
Expand Down
4 changes: 2 additions & 2 deletions src/ui/dashpay/profile_screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::app::AppAction;
use crate::backend_task::dashpay::DashPayTask;
use crate::backend_task::{BackendTask, BackendTaskSuccessResult};
use crate::context::AppContext;
use crate::model::fee_estimation::{PlatformFeeEstimator, format_credits_as_dash};
use crate::model::fee_estimation::format_credits_as_dash;
use crate::model::qualified_identity::QualifiedIdentity;
use crate::model::wallet::Wallet;
use crate::ui::MessageType;
Expand Down Expand Up @@ -905,7 +905,7 @@ impl ProfileScreen {
});
} else {
// Fee estimation display
let fee_estimator = PlatformFeeEstimator::new();
let fee_estimator = self.app_context.fee_estimator();
// Profile creation/update is a document operation
let estimated_fee = if self.profile.is_some() {
fee_estimator.estimate_document_replace()
Expand Down
11 changes: 5 additions & 6 deletions src/ui/identities/add_new_identity_screen/by_platform_address.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::app::AppAction;
use crate::model::amount::Amount;
use crate::model::fee_estimation::{PlatformFeeEstimator, format_credits_as_dash};
use crate::model::fee_estimation::format_credits_as_dash;
use crate::ui::components::amount_input::AmountInput;
use crate::ui::components::component_trait::{Component, ComponentResponse};
use crate::ui::identities::add_new_identity_screen::{
Expand Down Expand Up @@ -197,11 +197,10 @@ impl AddNewIdentityScreen {
} else {
0
};
let estimated_fee = PlatformFeeEstimator::new().estimate_identity_create_from_addresses(
input_count,
false,
key_count,
);
let estimated_fee = self
.app_context
.fee_estimator()
.estimate_identity_create_from_addresses(input_count, false, key_count);
let dark_mode = ui.ctx().style().visuals.dark_mode;
egui::Frame::new()
.fill(crate::ui::theme::DashColors::surface(dark_mode))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::app::AppAction;
use crate::model::fee_estimation::{PlatformFeeEstimator, format_credits_as_dash};
use crate::model::fee_estimation::format_credits_as_dash;
use crate::ui::identities::add_new_identity_screen::{
AddNewIdentityScreen, FundingMethod, WalletFundedScreenStep,
};
Expand Down Expand Up @@ -104,7 +104,10 @@ impl AddNewIdentityScreen {

// Display estimated fee before action button
let key_count = self.identity_keys.keys_input.len() + 1; // +1 for master key
let estimated_fee = PlatformFeeEstimator::new().estimate_identity_create(key_count);
let estimated_fee = self
.app_context
.fee_estimator()
.estimate_identity_create(key_count);
ui.add_space(10.0);
let dark_mode = ui.ctx().style().visuals.dark_mode;
egui::Frame::new()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::app::AppAction;
use crate::model::fee_estimation::{PlatformFeeEstimator, format_credits_as_dash};
use crate::model::fee_estimation::format_credits_as_dash;
use crate::ui::identities::add_new_identity_screen::{
AddNewIdentityScreen, FundingMethod, WalletFundedScreenStep,
};
Expand Down Expand Up @@ -57,7 +57,10 @@ impl AddNewIdentityScreen {

// Display estimated fee before action button
let key_count = self.identity_keys.keys_input.len() + 1; // +1 for master key
let estimated_fee = PlatformFeeEstimator::new().estimate_identity_create(key_count);
let estimated_fee = self
.app_context
.fee_estimator()
.estimate_identity_create(key_count);
ui.add_space(10.0);
let dark_mode = ui.ctx().style().visuals.dark_mode;
egui::Frame::new()
Expand Down
4 changes: 2 additions & 2 deletions src/ui/identities/keys/add_key_screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::app::AppAction;
use crate::backend_task::identity::IdentityTask;
use crate::backend_task::{BackendTask, BackendTaskSuccessResult, FeeResult};
use crate::context::AppContext;
use crate::model::fee_estimation::{PlatformFeeEstimator, format_credits_as_dash};
use crate::model::fee_estimation::format_credits_as_dash;
use crate::model::qualified_identity::QualifiedIdentity;
use crate::model::qualified_identity::qualified_identity_public_key::QualifiedIdentityPublicKey;
use crate::model::wallet::Wallet;
Expand Down Expand Up @@ -607,7 +607,7 @@ impl ScreenLike for AddKeyScreen {
ui.add_space(20.0);

// Fee estimation display
let fee_estimator = PlatformFeeEstimator::new();
let fee_estimator = self.app_context.fee_estimator();
let estimated_fee = fee_estimator.estimate_identity_update();

let dark_mode = ui.ctx().style().visuals.dark_mode;
Expand Down
4 changes: 2 additions & 2 deletions src/ui/identities/register_dpns_name_screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::app::AppAction;
use crate::backend_task::identity::{IdentityTask, RegisterDpnsNameInput};
use crate::backend_task::{BackendTask, BackendTaskSuccessResult, FeeResult};
use crate::context::AppContext;
use crate::model::fee_estimation::{PlatformFeeEstimator, format_credits_as_dash};
use crate::model::fee_estimation::format_credits_as_dash;
use crate::model::qualified_identity::QualifiedIdentity;
use crate::model::wallet::Wallet;
use crate::ui::components::identity_selector::IdentitySelector;
Expand Down Expand Up @@ -473,7 +473,7 @@ impl ScreenLike for RegisterDpnsNameScreen {
ui.add_space(10.0);

// Fee estimation
let fee_estimator = PlatformFeeEstimator::new();
let fee_estimator = self.app_context.fee_estimator();
let estimated_fee = fee_estimator.estimate_document_create();
let dark_mode = ui.ctx().style().visuals.dark_mode;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::app::AppAction;
use crate::backend_task::BackendTask;
use crate::backend_task::identity::IdentityTask;
use crate::model::amount::Amount;
use crate::model::fee_estimation::{PlatformFeeEstimator, format_credits_as_dash};
use crate::model::fee_estimation::format_credits_as_dash;
use crate::model::wallet::WalletSeedHash;
use crate::ui::components::amount_input::AmountInput;
use crate::ui::components::component_trait::{Component, ComponentResponse};
Expand Down Expand Up @@ -131,7 +131,7 @@ impl TopUpIdentityScreen {
ui.add_space(10.0);

// Fee estimation display
let fee_estimator = PlatformFeeEstimator::new();
let fee_estimator = self.app_context.fee_estimator();
let estimated_fee = fee_estimator.estimate_identity_topup();

Frame::new()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::app::AppAction;
use crate::model::fee_estimation::{PlatformFeeEstimator, format_credits_as_dash};
use crate::model::fee_estimation::format_credits_as_dash;
use crate::ui::identities::add_new_identity_screen::FundingMethod;
use crate::ui::identities::top_up_identity_screen::{TopUpIdentityScreen, WalletFundedScreenStep};
use crate::ui::theme::DashColors;
Expand Down Expand Up @@ -95,7 +95,7 @@ impl TopUpIdentityScreen {
ui.add_space(10.0);

// Fee estimation display
let fee_estimator = PlatformFeeEstimator::new();
let fee_estimator = self.app_context.fee_estimator();
let estimated_fee = fee_estimator.estimate_identity_topup();

let dark_mode = ui.ctx().style().visuals.dark_mode;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::app::AppAction;
use crate::model::fee_estimation::{PlatformFeeEstimator, format_credits_as_dash};
use crate::model::fee_estimation::format_credits_as_dash;
use crate::ui::identities::add_new_identity_screen::FundingMethod;
use crate::ui::identities::top_up_identity_screen::{TopUpIdentityScreen, WalletFundedScreenStep};
use crate::ui::theme::DashColors;
Expand Down Expand Up @@ -48,7 +48,7 @@ impl TopUpIdentityScreen {
};

// Fee estimation display
let fee_estimator = PlatformFeeEstimator::new();
let fee_estimator = self.app_context.fee_estimator();
let estimated_fee = fee_estimator.estimate_identity_topup();

let dark_mode = ui.ctx().style().visuals.dark_mode;
Expand Down
4 changes: 2 additions & 2 deletions src/ui/identities/transfer_screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::backend_task::identity::IdentityTask;
use crate::backend_task::{BackendTask, BackendTaskSuccessResult, FeeResult};
use crate::context::AppContext;
use crate::model::amount::Amount;
use crate::model::fee_estimation::{PlatformFeeEstimator, format_credits_as_dash};
use crate::model::fee_estimation::format_credits_as_dash;
use crate::model::qualified_identity::QualifiedIdentity;
use crate::model::wallet::Wallet;
use crate::ui::components::amount_input::AmountInput;
Expand Down Expand Up @@ -667,7 +667,7 @@ impl ScreenLike for TransferScreen {
ui.add_space(10.0);

// Fee estimation
let fee_estimator = PlatformFeeEstimator::new();
let fee_estimator = self.app_context.fee_estimator();
let estimated_fee = match self.destination_type {
TransferDestinationType::Identity => fee_estimator.estimate_credit_transfer(),
TransferDestinationType::PlatformAddress => {
Expand Down
Loading
Loading