Icon LinkEstimating contract call cost

With the function estimate_transaction_cost(tolerance: Option<f64>) provided by ContractCallHandler and ContractMultiCallHandler, you can get a cost estimation for a specific call. The return type, TransactionCost, is a struct that contains relevant information for the estimation:

TransactionCost {
    min_gas_price: u64,
    min_byte_price: u64,
    gas_price: u64,
    gas_used: u64,
    metered_bytes_size: u64,
    total_fee: f64, // where total_fee is the sum of the gas and byte fees
}

Below are examples that show how to get the estimated transaction cost from single and multi call transactions.

let contract_instance = MyContract::new(contract_id, wallet);
 
let tolerance = 0.0;
let transaction_cost = contract_instance
    .methods()
    .initialize_counter(42) // Build the ABI call
    .estimate_transaction_cost(Some(tolerance)) // Get estimated transaction cost
    .await?;
let mut multi_call_handler = MultiContractCallHandler::new(wallet.clone());
 
let call_handler_1 = contract_methods.initialize_counter(42);
let call_handler_2 = contract_methods.get_array([42; 2]);
 
multi_call_handler
    .add_call(call_handler_1)
    .add_call(call_handler_2);
 
let tolerance = 0.0;
let transaction_cost = multi_call_handler
    .estimate_transaction_cost(Some(tolerance)) // Get estimated transaction cost
    .await?;

The transaction cost estimation can be used to set the gas limit for an actual call, or to show the user the estimated cost.