getRpcRequest method

  1. @override
Future getRpcRequest({
  1. required String method,
  2. required List params,
  3. required String chain,
})
override

Implementation

@override
Future<dynamic> getRpcRequest({
  required String method,
  required List<dynamic> params,
  required String chain,
}) async {
  final bool isChainId = NamespaceUtils.isValidChainId(chain);
  if (!isChainId) {
    throw Errors.getSdkError(
      Errors.UNSUPPORTED_CHAINS,
      context: '[$runtimeType] chain should be CAIP-2 valid',
    );
  }
  final uri = Uri.parse(_baseUrl);
  final queryParams = {..._requiredParams, 'chainId': chain};
  if (queryParams['clientId'] == null) {
    queryParams['clientId'] = await _core.crypto.getClientId();
  }
  final response = await http.post(
    uri.replace(queryParameters: queryParams),
    headers: {
      ..._requiredHeaders,
      'Content-Type': 'application/json',
    },
    body: jsonEncode({
      'id': 1,
      'jsonrpc': '2.0',
      'method': method,
      'params': params,
    }),
  );
  if (response.statusCode == 200 && response.body.isNotEmpty) {
    _retries = 1;
    try {
      final result = _parseRpcResultAs<String>(response.body);
      final amount = EtherAmount.fromBigInt(EtherUnit.wei, hexToInt(result));
      return amount.getValueInUnit(EtherUnit.ether);
    } catch (e) {
      rethrow;
    }
  } else {
    if (response.body.isEmpty && _retries > 0) {
      loggerService.instance.i('[$runtimeType] Empty body');
      _retries -= 1;
      await getRpcRequest(method: method, params: params, chain: chain);
    } else {
      loggerService.instance.i(
        '[$runtimeType] Failed to get request $method. '
        'Response: ${response.body}, Status code: ${response.statusCode}',
      );
    }
  }
}