estimateGasLimit method

Future<BigInt> estimateGasLimit({
  1. String? from,
  2. required String to,
  3. BigInt? amount,
  4. BigInt? gasPrice,
  5. String? data,
})

Estimate Gas Fee

Implementation

Future<BigInt> estimateGasLimit({
  String? from,
  required String to,
  BigInt? amount,
  BigInt? gasPrice,
  String? data,
}) async {
  try {
    final response = await _call<String>(
      'eth_estimateGas',
      args: [
        {
          if (from != null) 'from': from,
          'to': to,
          if (gasPrice != null) 'gasPrice': gasPrice.toHexWithPrefix,
          if (data != null) 'data': data,
          if (amount != null) 'value': amount.toHexWithPrefix,
        }
      ],
    );

    final gasFee = response.toBigIntOrNull;
    if (gasFee == null) throw Exception('Could not parse gas fee');
    return gasFee;
  } catch (e) {
    Logger.logError(
      e,
      hint: 'estimateGasLimit failed - falling back to hardcoded gasLimit',
    );
    return BigInt.from(95000);
  }
}