#[derive(Debug)]
pub struct GasEstimate {
pub estimated_gas: u64,
pub max_gas: u64,
}
pub fn estimate_gas(contract_size: usize, complexity: u64) -> GasEstimate {
let base_gas = 21_000;
let size_gas = contract_size as u64 * 10; let complexity_gas = complexity * 50;
let estimated_gas = base_gas + size_gas + complexity_gas;
let max_gas = estimated_gas * 2;
GasEstimate {
estimated_gas,
max_gas,
}
}
pub fn check_gas_limit(gas_limit: u64, estimate: &GasEstimate) -> Result<(), String> {
if gas_limit < estimate.estimated_gas {
return Err(format!(
"Insufficient gas limit: {} provided, but {} estimated.",
gas_limit, estimate.estimated_gas
));
}
Ok(())
}
pub fn optimize_gas_dynamically(contract_size: usize, complexity: u64) -> u64 {
let estimate = estimate_gas(contract_size, complexity);
let gas_limit = (estimate.estimated_gas as f64 * 1.2) as u64; gas_limit
}