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 uri = Uri.parse(_baseUrl);
  final queryParams = {..._requiredParams, 'chainId': caip2Chain};
  final url = uri.replace(queryParameters: queryParams);
  final body = jsonEncode({
    'jsonrpc': '2.0',
    'method': 'eth_estimateGas',
    'params': [transaction],
    'id': 1,
  });
  final response = await http.post(
    url,
    headers: _requiredHeaders,
    body: body,
  );
  _core.logger.i(
    '[$runtimeType] estimateGas $url, $body => ${response.body}',
  );
  if (response.statusCode == 200 && response.body.isNotEmpty) {
    try {
      return _parseEstimateGasResult(response.body);
    } on JsonRpcError catch (e) {
      _core.logger.e('[$runtimeType] estimateGas, parse error => $e');
      if ((e.message ?? '')
          .toLowerCase()
          .contains('insufficient funds for gas')) {
        throw 'Insufficient funds for gas';
      }
      throw 'Failed to estimate gas';
    } catch (e) {
      _core.logger.e('[$runtimeType] estimateGas, parse error => $e');
      throw 'Failed to estimate gas.';
    }
  }
  try {
    final reason = _parseResponseError(response.body);
    throw Exception(reason);
  } catch (e) {
    _core.logger.e('[$runtimeType] getBalance, decode error => $e');
    rethrow;
  }
}