stack_wallet/lib/wallets/api/tezos/tezos_rpc_api.dart

72 lines
2.1 KiB
Dart
Raw Normal View History

2023-08-27 14:43:12 +00:00
import 'dart:convert';
import '../../../networking/http.dart';
import '../../../services/tor_service.dart';
import '../../../utilities/logger.dart';
import '../../../utilities/prefs.dart';
abstract final class TezosRpcAPI {
static final HTTP _client = HTTP();
2023-08-27 14:43:12 +00:00
static Future<BigInt?> getBalance({
required ({String host, int port}) nodeInfo,
required String address,
}) async {
2023-08-27 14:43:12 +00:00
try {
2024-05-27 23:56:22 +00:00
final String balanceCall =
2023-08-27 14:43:12 +00:00
"${nodeInfo.host}:${nodeInfo.port}/chains/main/blocks/head/context/contracts/$address/balance";
final response = await _client.get(
url: Uri.parse(balanceCall),
headers: {'Content-Type': 'application/json'},
proxyInfo: Prefs.instance.useTor
? TorService.sharedInstance.getProxyInfo()
: null,
);
final balance =
BigInt.parse(response.body.substring(1, response.body.length - 2));
2023-08-27 14:43:12 +00:00
return balance;
} catch (e) {
Logging.instance.log(
"Error occurred in tezos_rpc_api.dart while getting balance for $address: $e",
level: LogLevel.Error,
);
2023-08-27 14:43:12 +00:00
}
return null;
}
static Future<int?> getChainHeight({
required ({String host, int port}) nodeInfo,
}) async {
2023-08-27 14:43:12 +00:00
try {
final api =
2023-08-27 14:43:12 +00:00
"${nodeInfo.host}:${nodeInfo.port}/chains/main/blocks/head/header/shell";
final response = await _client.get(
url: Uri.parse(api),
headers: {'Content-Type': 'application/json'},
proxyInfo: Prefs.instance.useTor
? TorService.sharedInstance.getProxyInfo()
: null,
);
final jsonParsedResponse = jsonDecode(response.body);
2023-08-27 14:43:12 +00:00
return int.parse(jsonParsedResponse["level"].toString());
} catch (e) {
Logging.instance.log(
"Error occurred in tezos_rpc_api.dart while getting chain height for tezos: $e",
level: LogLevel.Error,
);
2023-08-27 14:43:12 +00:00
}
return null;
}
static Future<bool> testNetworkConnection({
required ({String host, int port}) nodeInfo,
}) async {
final result = await getChainHeight(nodeInfo: nodeInfo);
return result != null;
2023-08-27 14:43:12 +00:00
}
}