/// <summary> /// Calculates the gas usage of a rawTransaction by combining the intrinsic gas cost with /// the cost of a test run interacting with a potential contract /// </summary> /// <param name="client"></param> /// <returns>The total gas cost of a transaction</returns> public async Task <ulong> CalculateTotalGasCost(IVeChainClient client, string caller = null) { var executionCost = await CalculateExecutionGasCost(client, caller); var intrinsicCost = CalculateIntrinsicGasCost(); return(executionCost + intrinsicCost); }
/// <summary> /// Calculates the execution gas cost of a transaction by submitting it to the contract on the client /// instance of the blockchain. /// </summary> /// <param name="client"></param> /// <returns>The execution gas cost of the transaction</returns> public async Task <ulong> CalculateExecutionGasCost(IVeChainClient client, string caller = null) { var callResults = (await client.ExecuteAddressCode(clauses, caller: caller, gasPrice: gasPriceCoef)).ToArray(); if (callResults.Any(result => result.reverted)) { throw new InvalidOperationException($"Execution reverted during gas estimation: {string.Join(", ", callResults.Select(result => result.vmError))}"); } return((ulong)callResults.Sum(result => (decimal)result.gasUsed)); }