通过 ContractCallHandler
和 MultiContractCallHandler
提供的函数 estimate_transaction_cost(tolerance: Option<f64>, block_horizon: Option<u32>)
,您可以获得特定调用的成本估算。返回类型 TransactionCost
是一个包含相关信息的结构体,用于估算:
pub struct TransactionCost {
pub gas_price: u64,
pub gas_used: u64,
pub metered_bytes_size: u64,
pub total_fee: u64,
}
下面是显示如何从单个和多个调用事务获取估计事务成本的示例。
let contract_instance = MyContract::new(contract_id, wallet);
let tolerance = Some(0.0);
let block_horizon = Some(1);
let transaction_cost = contract_instance
.methods()
.initialize_counter(42) // Build the ABI call
.estimate_transaction_cost(tolerance, block_horizon) // 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 = Some(0.0);
let block_horizon = Some(1);
let transaction_cost = multi_call_handler
.estimate_transaction_cost(tolerance, block_horizon) // Get estimated transaction cost
.await?;
事务成本估算可用于设置实际调用的燃料限制,或向用户显示估算成本。
注意:脚本也具有相同的估算接口。