cairo_lang_sierra_to_casm/environment/
gas_wallet.rsuse std::fmt::Display;
use cairo_lang_sierra::extensions::gas::CostTokenType;
use cairo_lang_utils::collection_arithmetics::add_maps;
use cairo_lang_utils::ordered_hash_map::OrderedHashMap;
use thiserror::Error;
#[derive(Error, Debug, Eq, PartialEq)]
pub enum GasWalletError {
#[error(
"Ran out of gas ({token_type:?}) in the wallet, requested {request:?} while state is \
{state}."
)]
OutOfGas {
state: GasWallet,
request: Box<OrderedHashMap<CostTokenType, i64>>,
token_type: CostTokenType,
},
}
#[derive(Clone, Debug, Eq)]
pub enum GasWallet {
Value(OrderedHashMap<CostTokenType, i64>),
Disabled,
}
impl GasWallet {
pub fn update(
&self,
request: OrderedHashMap<CostTokenType, i64>,
) -> Result<Self, GasWalletError> {
match &self {
Self::Value(existing) => {
let new_value = add_maps(existing.clone(), request.iter().map(|(k, v)| (*k, *v)));
for (token_type, val) in new_value.iter() {
if *val < 0 {
return Err(GasWalletError::OutOfGas {
state: self.clone(),
request: Box::new(request),
token_type: *token_type,
});
}
}
Ok(GasWallet::Value(new_value))
}
Self::Disabled => Ok(Self::Disabled),
}
}
}
impl Display for GasWallet {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Value(value) => write!(f, "GasWallet::Value({value:?})"),
Self::Disabled => write!(f, "GasWallet::Disabled"),
}
}
}
impl PartialEq for GasWallet {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Value(l), Self::Value(r)) => l.eq_unordered(r),
_ => core::mem::discriminant(self) == core::mem::discriminant(other),
}
}
}