estimateGas method

  1. @override
Future<BigInt> estimateGas({
  1. required Map<String, dynamic> transaction,
  2. required String caip2Chain,
})
override

Implementation

@override
Future<BigInt> estimateGas({
  required Map<String, dynamic> transaction,
  required String caip2Chain,
}) async {
  final body = _buildJsonRpcRequest(
    method: 'eth_estimateGas',
    params: [transaction],
  );
  final url = _buildRpcUrl(caip2Chain);
  final response = await http.post(
    url,
    headers: _requiredHeaders,
    body: body,
  );
  _core.logger.i(
    '[$runtimeType] estimateGas $url, $body => ${response.body}',
  );

  try {
    return _handleResponse(
      response: response,
      parser: (body) => _parseEstimateGasResult(body),
      errorContext: 'estimateGas',
    );
  } on JsonRpcError catch (e) {
    _core.logger.e('[$runtimeType] estimateGas, parse error => $e');
    if ((e.message ?? '').toLowerCase().contains(
      'insufficient funds for gas',
    )) {
      throw JsonRpcError(code: e.code, message: 'Insufficient funds for gas');
    }
    throw JsonRpcError(code: e.code, message: 'Failed to estimate gas');
  }
}