forked from moneytrackio/tezart
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rpc_interface.dart
308 lines (260 loc) · 9.96 KB
/
rpc_interface.dart
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import 'dart:async';
import 'dart:convert';
import 'package:logging/logging.dart';
import 'package:memoize/memoize.dart';
import 'package:tezart/src/core/rpc/impl/operations_monitor.dart';
import 'package:tezart/src/models/operations_list/operations_list.dart';
import 'rpc_interface_paths.dart' as paths;
import 'tezart_http_client.dart';
/// A class that handles the calls to a Tezos RPC Node
///
/// - [url] is the url of the node
class RpcInterface {
static const _randomSignature =
'edsigu165B7VFf3Dpw2QABVzEtCxJY2gsNBNcE3Ti7rRxtDUjqTFRpg67EdAQmY6YWPE5tKJDMnSTJDFu65gic8uLjbW2YwGvAZ';
final TezartHttpClient httpClient;
final log = Logger('RpcInterface');
RpcInterface(String url, {String? proxy})
: httpClient = TezartHttpClient(url, proxy: proxy);
/// Returns the block's hash of [chain] and [level]
Future<String> branch([chain = 'main', level = 'head']) async {
log.info('request for branch [ chain:$chain, level:$level]');
var response =
await httpClient.get(paths.branch(chain: chain, level: level));
return response.data;
}
/// Returns the chain id of [chain]
Future<String> chainId([chain = 'main']) async {
log.info('request for chainId [ chain:$chain ]');
var response = await httpClient.get(paths.chainId(chain));
return response.data;
}
/// Returns the protocol of [chain] and [level]
Future<String> protocol([chain = 'main', level = 'head']) async {
log.info('request for protocol [ chain:$chain, level:$level]');
var response =
await httpClient.get(paths.protocol(chain: chain, level: level));
return response.data['protocol'];
}
/// Returns the counter of [source] in the chain defined by [chain] and [level]
Future<int> counter(String source, [chain = 'main', level = 'head']) async {
log.info('request for counter [ chain:$chain, level:$level]');
final response = await httpClient
.get(paths.counter(source: source, chain: chain, level: level));
return int.parse(response.data);
}
/// Returns the pending operations of [chain]
Future<Map<String, dynamic>> pendingOperations([chain = 'main']) async {
log.info('request for pendingOperations [ chain:$chain]');
final response = await httpClient.get(paths.pendingOperations(chain));
return response.data;
}
/// Injects the forged operation [data] in [chain] and returns the operation id
Future<String> injectOperation(String data, [chain = 'main']) async {
log.info('request for injectOperation [ chain:$chain]');
final response = await httpClient.post(paths.injectOperation(chain),
data: jsonEncode(data));
return response.data;
}
/// Returns the forged operation of [operationsList] in the chain defined by [chain] and [level]
Future<String> forgeOperations(OperationsList operationsList,
[chain = 'main', level = 'head']) async {
log.info('request for forgeOperations [ chain:$chain, level:$level]');
var content = {
'branch': await branch(),
'contents': operationsList.operations
.map((operation) => operation.toJson())
.toList(),
};
return memo1<Map<String, Object>, Future<String>>(
(Map<String, Object> content) async {
final response = await httpClient.post(
paths.forgeOperations(chain: chain, level: level),
data: content);
return response.data;
})(content);
}
/// Preapplies [operationsList] using [signature] in the chain defined by [chain] and [level] and returns the simlation result
Future<List<dynamic>> preapplyOperations({
required OperationsList operationsList,
required String signature,
chain = 'main',
level = 'head',
}) async {
log.info('request for preapplyOperations [ chain:$chain, level:$level]');
final content = [
{
'branch': await branch(),
'contents': operationsList.operations
.map((operation) => operation.toJson())
.toList(),
'signature': signature,
'protocol': await protocol(chain, level),
}
];
var response = await httpClient.post(
paths.preapplyOperations(
chain: chain,
level: level,
),
data: content,
);
// TODO: understand why array ? difference with run ??
return response.data.first['contents'];
}
/// Same as [preapplyOperations] but uses a random signature
Future<List<dynamic>> runOperations(OperationsList operationsList,
[chain = 'main', level = 'head']) async {
log.info('request for runOperations [ chain:$chain, level:$level]');
var content = {
'operation': {
'branch': await branch(),
'contents': operationsList.operations
.map((operation) => operation.toJson())
.toList(),
'signature': _randomSignature
},
'chain_id': await chainId()
};
var response = await httpClient
.post(paths.runOperations(chain: chain, level: level), data: content);
return response.data['contents'];
}
/// Returns the public key of [address]
///
/// If the address is unknown by the node, it returns null
Future<String?> managerKey(String address,
[chain = 'main', level = 'head']) async {
log.info('request for managerKey [ chain:$chain, level:$level]');
var response = await httpClient
.get(paths.managerKey(address: address, chain: chain, level: level));
return response.data;
}
/// Returns the balance of [address] in the chain defined by [chain] and [level]
Future<int> balance(String address, [chain = 'main', level = 'head']) async {
log.info('request for balance [ chain:$chain, level:$level]');
var response = await httpClient
.get(paths.balance(chain: chain, level: level, address: address));
return int.parse(response.data['balance']);
}
/// Returns the complete status of the contract whom address is [address]
Future<Map<String, dynamic>> getContract(String address,
[chain = 'main', level = 'head']) async {
log.info('request for contract : $address');
var response = await httpClient.get(
paths.contract(chain: chain, level: level, contractAddress: address));
return response.data;
}
/// Returns a map containing the entrypoints and their types of a contract defined by [address]
Future<Map<String, dynamic>> getContractEntrypoints(String address,
[chain = 'main', level = 'head']) async {
log.info('request for contract entrypoints : $address');
return memo1<String, Future<Map<String, dynamic>>>((String address) async {
var response = await httpClient.get(paths.contractEntrypoints(
chain: chain,
level: level,
contractAddress: address,
));
return response.data['entrypoints'];
})(address);
}
/// Returns the type of [entrypoint] of the contract defined by [address]
Future<Map<String, dynamic>> getContractEntrypointType({
required String address,
required String entrypoint,
chain = 'main',
level = 'head',
}) async {
log.info('request for contract : $address, entrypoint: $entrypoint');
return memo2<String, String, Future<Map<String, dynamic>>>(
(String address, String entrypoint) async {
var response = await httpClient.get(paths.contractEntrypoint(
chain: chain,
level: level,
contractAddress: address,
entrypoint: entrypoint,
));
return response.data;
})(address, entrypoint);
}
/// Returns the applied transactions hashes in [chain] and [level]
Future<List<String>> transactionsOperationHashes({
required String level,
chain = 'main',
}) async {
final response = await httpClient.get(paths.operationHashes(
chain: chain,
level: level,
offset: 3,
));
return response.data.cast<String>().toList();
}
/// Waits for [operationId] to be included in a block
///
/// Throws a [TezartNodeErrorTypes.monitoringTimedOut] if the operation is not included in the next two blocks
// TODO: wait for multiple blocks
Future<String> monitorOperation({
required String operationId,
chain = 'main',
level = 'head',
}) async {
return _operationsMonitor.monitor(
chain: chain, level: level, operationId: operationId);
}
OperationsMonitor get _operationsMonitor =>
memo0(() => OperationsMonitor(this))();
/// Returns the block hash of [chain] and [level]
Future<Map<String, dynamic>> block(
{required String chain, required String level}) async {
final response =
await httpClient.get(paths.block(chain: chain, level: level));
return response.data;
}
/// Returns the constants of the chain defined by [chain] and [level]
Future<Map<String, dynamic>> constants(
[chain = 'main', level = 'head']) async {
return memo2<String, String, Future<Map<String, dynamic>>>(
(String chain, String level) async {
log.info('request to constants');
final response =
await httpClient.get(paths.constants(chain: chain, level: level));
return response.data;
})(chain, level);
}
/// Packs [data] using [type]
///
/// - [data] is the data to pack, it must be in Micheline
/// - [type] is the type of [data]
Future<String> pack({
required dynamic data,
required Map<String, dynamic> type,
chain = 'main',
level = 'head',
}) async {
final content = {
'data': data,
'type': type,
};
final response = await httpClient
.post(paths.pack(chain: chain, level: level), data: content);
return response.data['packed'];
}
/// Returns the value of a key in a big map
///
/// - [id] is the id of the big map
/// - [encodedScriptExpression] is the encoded version of the key we are trying to fetch
Future bigMapValue({
required String id,
required String encodedScriptExpression,
chain = 'main',
level = 'head',
}) async {
final response = await httpClient.get(paths.bigMapValue(
level: level,
chain: chain,
id: id,
encodedScriptExpression: encodedScriptExpression,
));
return response.data;
}
}