estimateGas method

Future<int?> estimateGas({
  1. EthereumAddress? address,
  2. EthereumAddress? from,
  3. int? gas,
  4. int? gasPrice,
  5. int? value,
  6. EthereumData? data,
})

Estimate gas Makes a call or transaction, which won't be added to the blockchain and returns the used gas, which can be used for estimating the used gas. See eth_call parameters, expect that all properties are optional. If no gas limit is specified geth uses the block gas limit from the pending block as an upper bound. As a result the returned estimate might not be enough to executed the call/transaction when the amount of gas is higher than the pending block gas limit. Returns the amount of gas used.

Implementation

Future<int?> estimateGas(
    {EthereumAddress? address,
    EthereumAddress? from,
    int? gas,
    int? gasPrice,
    int? value,
    EthereumData? data}) async {
  var paramBlock = <String, String?>{
    'from': from?.asString,
    'to': address?.asString,
    'gas': gas == null ? null : EthereumUtilities.intToHex(gas),
    'gasPrice':
        gasPrice == null ? null : EthereumUtilities.intToHex(gasPrice),
    'value': value == null ? null : EthereumUtilities.intToHex(value),
    'data': data?.asString
  };
  paramBlock =
      EthereumUtilities.removeNull(paramBlock) as Map<String, String?>;
  final dynamic params = <dynamic>[paramBlock];
  const method = EthereumRpcMethods.estimateGas;
  final dynamic res = await _client.rpcClient.request(method, params);
  if (res != null && res.containsKey(EthereumConstants.ethResultKey)) {
    return EthereumUtilities.hexToInt(res[EthereumConstants.ethResultKey]);
  }
  _client.processError(method, res);
  return null;
}