Skip to content

Commit

Permalink
feat(starknet_api): checked mul for gas vector
Browse files Browse the repository at this point in the history
  • Loading branch information
yoavGrs committed Nov 27, 2024
1 parent 27b9ddb commit b9eb760
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions crates/starknet_api/src/execution_resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ impl GasAmount {
pub fn checked_mul(self, rhs: GasPrice) -> Option<Fee> {
rhs.checked_mul(self)
}

pub fn checked_factor_mul(self, factor: u64) -> Option<Self> {
self.0.checked_mul(factor).map(Self)
}
}

#[cfg_attr(
Expand Down Expand Up @@ -109,6 +113,20 @@ impl GasVector {
}
}

pub fn checked_mul(self, factor: usize) -> Option<Self> {
let u64_factor = factor.try_into().ok()?;
match (
self.l1_gas.checked_factor_mul(u64_factor),
self.l1_data_gas.checked_factor_mul(u64_factor),
self.l2_gas.checked_factor_mul(u64_factor),
) {
(Some(l1_gas), Some(l1_data_gas), Some(l2_gas)) => {
Some(Self { l1_gas, l1_data_gas, l2_gas })
}
_ => None,
}
}

/// Computes the cost (in fee token units) of the gas vector (panicking on overflow).
pub fn cost(&self, gas_prices: &GasPriceVector) -> Fee {
let mut sum = Fee(0);
Expand Down

0 comments on commit b9eb760

Please sign in to comment.