From 340c229291d4b44eb0001661c50b52af33a2b386 Mon Sep 17 00:00:00 2001 From: yoavGrs <97383386+yoavGrs@users.noreply.github.com> Date: Tue, 3 Dec 2024 11:15:19 +0200 Subject: [PATCH] feat(starknet_api): checked mul for gas vector (#2300) --- crates/starknet_api/src/execution_resources.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/crates/starknet_api/src/execution_resources.rs b/crates/starknet_api/src/execution_resources.rs index 9c56443464..c9831b34aa 100644 --- a/crates/starknet_api/src/execution_resources.rs +++ b/crates/starknet_api/src/execution_resources.rs @@ -66,6 +66,10 @@ impl GasAmount { pub fn checked_mul(self, rhs: GasPrice) -> Option { rhs.checked_mul(self) } + + pub fn checked_factor_mul(self, factor: u64) -> Option { + self.0.checked_mul(factor).map(Self) + } } #[cfg_attr( @@ -109,6 +113,19 @@ impl GasVector { } } + pub fn checked_scalar_mul(self, factor: u64) -> Option { + match ( + self.l1_gas.checked_factor_mul(factor), + self.l1_data_gas.checked_factor_mul(factor), + self.l2_gas.checked_factor_mul(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);