2023-08-27 14:43:12 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
|
2023-11-20 15:55:28 +00:00
|
|
|
import 'package:stackwallet/networking/http.dart';
|
|
|
|
import 'package:stackwallet/services/tor_service.dart';
|
2023-08-27 14:43:12 +00:00
|
|
|
import 'package:stackwallet/utilities/logger.dart';
|
2023-11-20 15:55:28 +00:00
|
|
|
import 'package:stackwallet/utilities/prefs.dart';
|
|
|
|
|
|
|
|
abstract final class TezosRpcAPI {
|
|
|
|
static final HTTP _client = HTTP();
|
2023-08-27 14:43:12 +00:00
|
|
|
|
2023-11-20 15:55:28 +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 {
|
|
|
|
String balanceCall =
|
|
|
|
"${nodeInfo.host}:${nodeInfo.port}/chains/main/blocks/head/context/contracts/$address/balance";
|
2023-11-20 15:55:28 +00:00
|
|
|
|
|
|
|
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(
|
2023-11-20 15:55:28 +00:00
|
|
|
"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;
|
|
|
|
}
|
|
|
|
|
2023-11-20 15:55:28 +00:00
|
|
|
static Future<int?> getChainHeight({
|
|
|
|
required ({String host, int port}) nodeInfo,
|
|
|
|
}) async {
|
2023-08-27 14:43:12 +00:00
|
|
|
try {
|
2023-11-20 15:55:28 +00:00
|
|
|
final api =
|
2023-08-27 14:43:12 +00:00
|
|
|
"${nodeInfo.host}:${nodeInfo.port}/chains/main/blocks/head/header/shell";
|
2023-11-20 15:55:28 +00:00
|
|
|
|
|
|
|
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(
|
2023-11-20 15:55:28 +00:00
|
|
|
"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;
|
|
|
|
}
|
|
|
|
|
2023-11-20 15:55:28 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|