-
Notifications
You must be signed in to change notification settings - Fork 113
Expand file tree
/
Copy pathtezos_rpc_api.dart
More file actions
79 lines (70 loc) · 2.27 KB
/
Copy pathtezos_rpc_api.dart
File metadata and controls
79 lines (70 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import 'dart:convert';
import '../../../app_config.dart';
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();
static Future<BigInt?> getBalance({
required ({String host, int port}) nodeInfo,
required String address,
}) async {
try {
final String balanceCall =
"${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: !AppConfig.hasFeature(AppFeature.tor)
? null
: Prefs.instance.useTor
? TorService.sharedInstance.getProxyInfo()
: null,
);
final balance = BigInt.parse(
response.body.substring(1, response.body.length - 2),
);
return balance;
} catch (e, s) {
Logging.instance.e(
"Error occurred in tezos_rpc_api.dart while getting balance for $address: $e",
error: e,
stackTrace: s,
);
}
return null;
}
static Future<int?> getChainHeight({
required ({String host, int port}) nodeInfo,
}) async {
try {
final api =
"${nodeInfo.host}:${nodeInfo.port}/chains/main/blocks/head/header";
final response = await _client.get(
url: Uri.parse(api),
headers: {'Content-Type': 'application/json'},
proxyInfo: !AppConfig.hasFeature(AppFeature.tor)
? null
: Prefs.instance.useTor
? TorService.sharedInstance.getProxyInfo()
: null,
);
final jsonParsedResponse = jsonDecode(response.body);
return int.parse(jsonParsedResponse["level"].toString());
} catch (e, s) {
Logging.instance.e(
"Error occurred in tezos_rpc_api.dart while getting chain height for tezos: $e",
error: e,
stackTrace: s,
);
}
return null;
}
static Future<bool> testNetworkConnection({
required ({String host, int port}) nodeInfo,
}) async {
final result = await getChainHeight(nodeInfo: nodeInfo);
return result != null;
}
}