Skip to content

Commit

Permalink
feat(models): add helpers for prices/limits
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexagon committed Jan 24, 2025
1 parent 1e81354 commit 416fc75
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
48 changes: 48 additions & 0 deletions src/models/config/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,26 @@ pub struct StoragePrices {
pub mc_cell_price_ps: u64,
}

impl StoragePrices {
/// Computes the amount of fees for storing `stats` data for `delta` seconds.
pub fn compute_storage_fee(
&self,
is_masterchain: bool,
delta: u64,
stats: CellTreeStats,
) -> Tokens {
let mut res = if is_masterchain {
(stats.cell_count as u128 * self.mc_cell_price_ps as u128)
.saturating_add(stats.bit_count as u128 * self.mc_bit_price_ps as u128)
} else {
(stats.cell_count as u128 * self.cell_price_ps as u128)
.saturating_add(stats.bit_count as u128 * self.bit_price_ps as u128)
};
res = res.saturating_mul(delta as u128);
Tokens::new(shift_ceil_price(res))
}
}

/// Gas limits and prices.
#[derive(Default, Debug, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
Expand All @@ -333,6 +353,17 @@ pub struct GasLimitsPrices {
pub flat_gas_price: u64,
}

impl GasLimitsPrices {
/// Converts gas units into tokens.
pub fn compute_gas_fee(&self, gas_used: u64) -> Tokens {
let mut res = self.flat_gas_price as u128;
if let Some(extra_gas) = gas_used.checked_sub(self.flat_gas_limit) {
res = res.saturating_add(shift_ceil_price(self.gas_price as u128 * extra_gas as u128));
}
Tokens::new(res)
}
}

impl GasLimitsPrices {
const TAG_BASE: u8 = 0xdd;
const TAG_EXT: u8 = 0xde;
Expand Down Expand Up @@ -442,6 +473,18 @@ pub struct MsgForwardPrices {
pub next_frac: u16,
}

impl MsgForwardPrices {
/// Computes fees for forwarding the specified amount of data.
pub fn compute_fwd_fee(&self, stats: CellTreeStats) -> Tokens {
let lump = self.lump_price as u128;
let extra = shift_ceil_price(
(stats.cell_count as u128 * self.cell_price as u128)
.saturating_add(stats.bit_count as u128 * self.bit_price as u128),
);
Tokens::new(lump.saturating_add(extra))
}
}

/// Catchain configuration params.
#[cfg(not(feature = "tycho"))]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
Expand Down Expand Up @@ -1457,6 +1500,11 @@ pub struct SizeLimitsConfig {
pub defer_out_queue_size_limit: u32,
}

const fn shift_ceil_price(value: u128) -> u128 {
let r = value & 0xffff != 0;
(value >> 16) + r as u128
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
15 changes: 10 additions & 5 deletions src/models/vm/out_actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,16 @@ pub enum OutAction {
}

impl OutAction {
const TAG_SEND_MSG: u32 = 0x0ec3c86d;
const TAG_SET_CODE: u32 = 0xad4de08e;
const TAG_RESERVE: u32 = 0x36e6b809;
const TAG_CHANGE_LIB: u32 = 0x26fa1dd4;
const TAG_COPYLEFT: u32 = 0x24486f7a;
/// Tag for [`OutAction::SendMsg`].
pub const TAG_SEND_MSG: u32 = 0x0ec3c86d;
/// Tag for [`OutAction::SetCode`].
pub const TAG_SET_CODE: u32 = 0xad4de08e;
/// Tag for [`OutAction::ReserveCurrency`].
pub const TAG_RESERVE: u32 = 0x36e6b809;
/// Tag for [`OutAction::ChangeLibrary`].
pub const TAG_CHANGE_LIB: u32 = 0x26fa1dd4;
/// Tag for [`OutAction::CopyLeft`].
pub const TAG_COPYLEFT: u32 = 0x24486f7a;
}

impl Store for OutAction {
Expand Down

0 comments on commit 416fc75

Please sign in to comment.