1
- // @dart=2.9
2
1
part of 'package:web3dart/web3dart.dart' ;
3
2
4
3
/// Signature for a function that opens a socket on which json-rpc operations
@@ -22,6 +21,16 @@ typedef SocketConnector = StreamChannel<String> Function();
22
21
/// to create transactions, you will instead have to obtain private keys of
23
22
/// accounts yourself.
24
23
class Web3Client {
24
+ /// Starts a client that connects to a JSON rpc API, available at [url] . The
25
+ /// [httpClient] will be used to send requests to the rpc server.
26
+ /// Am isolate will be used to perform expensive operations, such as signing
27
+ /// transactions or computing private keys.
28
+ Web3Client (String url, Client httpClient, {this .socketConnector})
29
+ : _jsonRpc = JsonRPC (url, httpClient) {
30
+ _operations = _ExpensiveOperations ();
31
+ _filters = _FilterEngine (this );
32
+ }
33
+
25
34
static const BlockNum _defaultBlock = BlockNum .current ();
26
35
27
36
final JsonRPC _jsonRpc;
@@ -31,32 +40,16 @@ class Web3Client {
31
40
/// event requests and parse responses. Can be null, in which case a polling
32
41
/// implementation for events will be used.
33
42
@experimental
34
- final SocketConnector socketConnector;
35
-
36
- rpc.Peer _streamRpcPeer;
43
+ final SocketConnector ? socketConnector;
37
44
38
- _ExpensiveOperations _operations;
39
- _FilterEngine _filters;
45
+ rpc.Peer ? _streamRpcPeer;
46
+ late _ExpensiveOperations _operations;
47
+ late _FilterEngine _filters;
40
48
41
49
///Whether errors, handled or not, should be printed to the console.
42
50
bool printErrors = false ;
43
51
44
- /// Starts a client that connects to a JSON rpc API, available at [url] . The
45
- /// [httpClient] will be used to send requests to the rpc server.
46
- /// The [runner] will be used to perform expensive operations, such as signing
47
- /// transactions or computing private keys. By default, a [Runner] on the same
48
- /// isolate will be used. You can use `IsolateRunner.spawn` to use a
49
- /// background runner instead.
50
- /// The runner will automatically be disposed by web3dart when [dispose] is
51
- /// called.
52
- Web3Client (String url, Client httpClient,
53
- {this .socketConnector, Runner runner})
54
- : _jsonRpc = JsonRPC (url, httpClient) {
55
- _operations = _ExpensiveOperations (runner ?? Runner ());
56
- _filters = _FilterEngine (this );
57
- }
58
-
59
- Future <T > _makeRPCCall <T >(String function, [List <dynamic > params]) async {
52
+ Future <T > _makeRPCCall <T >(String function, [List <dynamic >? params]) async {
60
53
try {
61
54
final data = await _jsonRpc.call (function, params);
62
55
// ignore: only_throw_errors
@@ -71,19 +64,19 @@ class Web3Client {
71
64
}
72
65
}
73
66
74
- rpc.Peer _connectWithPeer () {
75
- if (_streamRpcPeer != null && ! _streamRpcPeer.isClosed) {
67
+ rpc.Peer ? _connectWithPeer () {
68
+ if (_streamRpcPeer != null && ! _streamRpcPeer! .isClosed) {
76
69
return _streamRpcPeer;
77
70
}
78
71
if (socketConnector == null ) return null ;
79
72
80
- final socket = socketConnector ();
73
+ final socket = socketConnector ! ();
81
74
_streamRpcPeer = rpc.Peer (socket)
82
75
..registerMethod ('eth_subscription' , (rpc.Parameters params) {
83
76
_filters.handlePubSubNotification (params);
84
77
});
85
78
86
- _streamRpcPeer.listen ().then ((_) {
79
+ _streamRpcPeer? .listen ().then ((_) {
87
80
// .listen() will complete when the socket is closed, so reset client
88
81
_streamRpcPeer = null ;
89
82
_filters.handleConnectionClosed ();
@@ -92,7 +85,7 @@ class Web3Client {
92
85
return _streamRpcPeer;
93
86
}
94
87
95
- String _getBlockParam (BlockNum block) {
88
+ String _getBlockParam (BlockNum ? block) {
96
89
return (block ?? _defaultBlock).toBlockParam ();
97
90
}
98
91
@@ -191,7 +184,7 @@ class Web3Client {
191
184
///
192
185
/// This function allows specifying a custom block mined in the past to get
193
186
/// historical data. By default, [BlockNum.current] will be used.
194
- Future <EtherAmount > getBalance (EthereumAddress address, {BlockNum atBlock}) {
187
+ Future <EtherAmount > getBalance (EthereumAddress address, {BlockNum ? atBlock}) {
195
188
final blockParam = _getBlockParam (atBlock);
196
189
197
190
return _makeRPCCall <String >('eth_getBalance' , [address.hex, blockParam])
@@ -207,7 +200,7 @@ class Web3Client {
207
200
/// This function allows specifying a custom block mined in the past to get
208
201
/// historical data. By default, [BlockNum.current] will be used.
209
202
Future <Uint8List > getStorage (EthereumAddress address, BigInt position,
210
- {BlockNum atBlock}) {
203
+ {BlockNum ? atBlock}) {
211
204
final blockParam = _getBlockParam (atBlock);
212
205
213
206
return _makeRPCCall <String >('eth_getStorageAt' , [
@@ -221,7 +214,8 @@ class Web3Client {
221
214
///
222
215
/// This function allows specifying a custom block mined in the past to get
223
216
/// historical data. By default, [BlockNum.current] will be used.
224
- Future <int > getTransactionCount (EthereumAddress address, {BlockNum atBlock}) {
217
+ Future <int > getTransactionCount (EthereumAddress address,
218
+ {BlockNum ? atBlock}) {
225
219
final blockParam = _getBlockParam (atBlock);
226
220
227
221
return _makeRPCCall <String >(
@@ -241,14 +235,14 @@ class Web3Client {
241
235
Future <TransactionReceipt > getTransactionReceipt (String hash) {
242
236
return _makeRPCCall <Map <String , dynamic >>(
243
237
'eth_getTransactionReceipt' , [hash])
244
- .then ((s) => s != null ? TransactionReceipt .fromMap (s) : null );
238
+ .then ((s) => TransactionReceipt .fromMap (s));
245
239
}
246
240
247
241
/// Gets the code of a contract at the specified [address]
248
242
///
249
243
/// This function allows specifying a custom block mined in the past to get
250
244
/// historical data. By default, [BlockNum.current] will be used.
251
- Future <Uint8List > getCode (EthereumAddress address, {BlockNum atBlock}) {
245
+ Future <Uint8List > getCode (EthereumAddress address, {BlockNum ? atBlock}) {
252
246
return _makeRPCCall <String >(
253
247
'eth_getCode' , [address.hex, _getBlockParam (atBlock)]).then (hexToBytes);
254
248
}
@@ -262,7 +256,7 @@ class Web3Client {
262
256
final filter = _EventFilter (options);
263
257
return _makeRPCCall <List <dynamic >>(
264
258
'eth_getLogs' , [filter._createParamsObject (true )]).then ((logs) {
265
- return logs? .map (filter.parseChanges)? .toList ();
259
+ return logs.map (filter.parseChanges).toList ();
266
260
});
267
261
}
268
262
@@ -325,11 +319,11 @@ class Web3Client {
325
319
/// This function allows specifying a custom block mined in the past to get
326
320
/// historical data. By default, [BlockNum.current] will be used.
327
321
Future <List <dynamic >> call ({
328
- EthereumAddress sender,
329
- @ required DeployedContract contract,
330
- @ required ContractFunction function,
331
- @ required List <dynamic > params,
332
- BlockNum atBlock,
322
+ EthereumAddress ? sender,
323
+ required DeployedContract contract,
324
+ required ContractFunction function,
325
+ required List <dynamic > params,
326
+ BlockNum ? atBlock,
333
327
}) async {
334
328
final encodedResult = await callRaw (
335
329
sender: sender,
@@ -345,13 +339,13 @@ class Web3Client {
345
339
/// sent via [sendTransaction] . Note that the estimate may be significantly
346
340
/// higher than the amount of gas actually used by the transaction.
347
341
Future <BigInt > estimateGas ({
348
- EthereumAddress sender,
349
- EthereumAddress to,
350
- EtherAmount value,
351
- BigInt amountOfGas,
352
- EtherAmount gasPrice,
353
- Uint8List data,
354
- @Deprecated ('Parameter is ignored' ) BlockNum atBlock,
342
+ EthereumAddress ? sender,
343
+ EthereumAddress ? to,
344
+ EtherAmount ? value,
345
+ BigInt ? amountOfGas,
346
+ EtherAmount ? gasPrice,
347
+ Uint8List ? data,
348
+ @Deprecated ('Parameter is ignored' ) BlockNum ? atBlock,
355
349
}) async {
356
350
final amountHex = await _makeRPCCall <String >(
357
351
'eth_estimateGas' ,
@@ -385,10 +379,10 @@ class Web3Client {
385
379
/// - [call] , which automatically encodes function parameters and parses a
386
380
/// response.
387
381
Future <String > callRaw ({
388
- EthereumAddress sender,
389
- @ required EthereumAddress contract,
390
- @ required Uint8List data,
391
- BlockNum atBlock,
382
+ EthereumAddress ? sender,
383
+ required EthereumAddress contract,
384
+ required Uint8List data,
385
+ BlockNum ? atBlock,
392
386
}) {
393
387
final call = {
394
388
'to' : contract.hex,
@@ -440,7 +434,6 @@ class Web3Client {
440
434
/// Closes resources managed by this client, such as the optional background
441
435
/// isolate for calculations and managed streams.
442
436
Future <void > dispose () async {
443
- await _operations.stop ();
444
437
await _filters.dispose ();
445
438
await _streamRpcPeer? .close ();
446
439
}
0 commit comments