getBalance method

  1. @override
Future getBalance({
  1. required String address,
  2. required String namespace,
  3. required String chainId,
})
override

Implementation

@override
Future<dynamic> getBalance({
  required String address,
  required String namespace,
  required String chainId,
}) async {
  final uri = Uri.parse(_baseUrl);
  final queryParams = {..._requiredParams, 'chainId': '$namespace:$chainId'};
  final response = await http.post(
    uri.replace(queryParameters: queryParams),
    headers: {..._requiredHeaders, 'Content-Type': 'application/json'},
    body: jsonEncode({
      'id': 1,
      'jsonrpc': '2.0',
      'method': _balanceMetod(namespace),
      'params': [
        address,
        if (namespace == NetworkUtils.eip155) 'latest',
      ],
    }),
  );
  _core.logger.i(
    '[$runtimeType] getBalance $namespace, $chainId, $address => ${response.body}',
  );
  if (response.statusCode == 200 && response.body.isNotEmpty) {
    try {
      return _parseBalanceResult(namespace, response.body);
    } catch (e) {
      _core.logger.e('[$runtimeType] getBalance, parse result error => $e');
      throw Exception('Failed to load balance. $e');
    }
  }
  try {
    final errorData = jsonDecode(response.body) as Map<String, dynamic>;
    final reasons = errorData['reasons'] as List<dynamic>;
    final reason = reasons.isNotEmpty
        ? reasons.first['description'] ?? ''
        : response.body;
    throw Exception(reason);
  } catch (e) {
    _core.logger.e('[$runtimeType] getBalance, decode result error => $e');
    rethrow;
  }
}