Skip to content

Commit

Permalink
separate priority fee and transaction fee from fee calculation (solan…
Browse files Browse the repository at this point in the history
…a-labs#34757)

add function calculate_fee_details() that returns FeeDetails with base and priority fee separated
  • Loading branch information
tao-stones authored Jan 26, 2024
1 parent 5da06c5 commit 5ecc47e
Showing 1 changed file with 39 additions and 8 deletions.
47 changes: 39 additions & 8 deletions sdk/src/fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ pub struct FeeStructure {
pub compute_fee_bins: Vec<FeeBin>,
}

/// Return type of calculate_fee(...)
#[derive(Debug, Default, Clone, Eq, PartialEq)]
pub struct FeeDetails {
transaction_fee: u64,
prioritization_fee: u64,
}

impl FeeDetails {
pub fn total_fee(&self) -> u64 {
self.transaction_fee.saturating_add(self.prioritization_fee)
}
}

pub const ACCOUNT_DATA_COST_PAGE_SIZE: u64 = 32_u64.saturating_mul(1024);

impl FeeStructure {
Expand Down Expand Up @@ -75,15 +88,32 @@ impl FeeStructure {
.saturating_mul(heap_cost)
}

/// Calculate fee for `SanitizedMessage`
#[cfg(not(target_os = "solana"))]
pub fn calculate_fee(
&self,
message: &SanitizedMessage,
_lamports_per_signature: u64,
lamports_per_signature: u64,
budget_limits: &FeeBudgetLimits,
include_loaded_account_data_size_in_fee: bool,
) -> u64 {
self.calculate_fee_details(
message,
lamports_per_signature,
budget_limits,
include_loaded_account_data_size_in_fee,
)
.total_fee()
}

/// Calculate fee details for `SanitizedMessage`
#[cfg(not(target_os = "solana"))]
pub fn calculate_fee_details(
&self,
message: &SanitizedMessage,
_lamports_per_signature: u64,
budget_limits: &FeeBudgetLimits,
include_loaded_account_data_size_in_fee: bool,
) -> FeeDetails {
let signature_fee = message
.num_signatures()
.saturating_mul(self.lamports_per_signature);
Expand Down Expand Up @@ -115,12 +145,13 @@ impl FeeStructure {
.unwrap_or_default()
});

(budget_limits
.prioritization_fee
.saturating_add(signature_fee)
.saturating_add(write_lock_fee)
.saturating_add(compute_fee) as f64)
.round() as u64
FeeDetails {
transaction_fee: (signature_fee
.saturating_add(write_lock_fee)
.saturating_add(compute_fee) as f64)
.round() as u64,
prioritization_fee: budget_limits.prioritization_fee,
}
}
}

Expand Down

0 comments on commit 5ecc47e

Please sign in to comment.