-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdart_wrappers.dart
More file actions
225 lines (190 loc) · 6.73 KB
/
dart_wrappers.dart
File metadata and controls
225 lines (190 loc) · 6.73 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
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
import 'dart:async';
import 'dart:js_interop';
import 'dart:js_util';
import 'package:webthree/src/core/exception_utils_js.dart'
if (dart.library.io) 'package:webthree/src/core/exception_utils_io.dart'
if (dart.library.js) 'package:webthree/src/core/exception_utils_js.dart';
import '../../../credentials.dart';
import '../../../json_rpc.dart';
import 'credentials.dart';
import 'javascript.dart';
/// This extension provides Dart methods around the raw [DartBinanceChain] JavaScript
/// object.
///
/// Extensions include [request] to turn request promises into Dart futures or
/// [on] to turn JavaScript event handlers into convenient Dart streams.
/// To use the raw ethereum client in a high-level `Web3Client`, use
/// [asRpcService].
extension DartBinanceChain on BinanceChainWallet {
/// Turns this raw client into an rpc client that can be used to create a
/// `Web3Client`:
///
/// ```dart
/// Future<void> main() async {
/// final bsc = window.BinanceChain;
/// if (bsc == null) {
/// print('Binance Wallet is not available');
/// return;
/// }
///
/// final client = Web3Client.custom(bsc.asRpcService());
/// }
/// ```
RpcService asRpcService() => _BinanceWalletRpcService(this);
/// Sends a raw rpc request using the injected Ethereum client.
///
/// If possible, prefer using [asRpcService] to construct a high-level client
/// instead.
///
/// See also:
/// - the rpc documentation under https://binance-wallet.gitbook.io/binance-chain-wallet/dev/get-started
Future<dynamic> rawRequest(String method, {JSAny? params}) {
final args = params == null
? RequestArguments(method: method)
: RequestArguments(method: method, params: params);
final jsPromise = request(args);
return jsPromise.toDart.catchError((error, stackTrace) {
ExceptionUtils.analyzeException(error!);
throw error;
});
}
/// Asks the user to select an account and give your application access to it.
Future<List<CredentialsWithKnownAddress>> requestAccounts() {
return rawRequest('eth_requestAccounts').then((res) {
final List<CredentialsWithKnownAddress> credentials = [];
for (final account in res) {
credentials.add(BinanceWalletCredentials(account as String, this));
}
return credentials;
});
}
/// Creates a stream of raw ethereum events.
///
/// The returned stream is a broadcast stream, meaning that it can be listened
/// to multiple times.
///
/// See also:
/// - https://docs.metamask.io/guide/ethereum-provider.html#events
Stream<dynamic> stream(String eventName) {
return _EventStream(this, eventName);
}
/// A broadcast stream emitting values when the selected chain is changed by the user.
Stream<dynamic> get chainChanged => stream('chainChanged').cast();
/// A broadcast stream emitting values when the selected account is changed by the user.
Stream<dynamic> get accountsChanged => stream('accountsChanged').cast();
/// A broadcast stream emitting values when the selected account is connected by the user.
Stream<dynamic> get connect => stream('connect').cast();
/// A broadcast stream emitting values when the selected account is disconnected by the user.
Stream<dynamic> get disconnect => stream('disconnect').cast();
/// A broadcast stream emitting values when a message is received to the connected account.
Stream<dynamic> get message => stream('message').cast();
}
class _BinanceWalletRpcService extends RpcService {
final BinanceChainWallet _binancechain;
_BinanceWalletRpcService(this._binancechain);
@override
Future<RPCResponse> call(String function, [List? params]) {
final jsParams = params != null ? jsify(params) as JSAny? : null;
return _binancechain.rawRequest(function, params: jsParams).then((res) {
return RPCResponse(0, res);
});
}
}
class _EventStream extends Stream<dynamic> {
final BinanceChainWallet _client;
final String _eventName;
_EventStream(this._client, this._eventName);
@override
bool get isBroadcast => true;
@override
Stream asBroadcastStream(
{void Function(StreamSubscription subscription)? onListen,
void Function(StreamSubscription subscription)? onCancel}) {
return this;
}
@override
StreamSubscription listen(void Function(dynamic event)? onData,
{Function? onError, void Function()? onDone, bool? cancelOnError}) {
final sub = _EventStreamSubscription(_client, _eventName, onData);
// onError, onDone and cancelOnErrors are not applicable here because event
// streams are infinite and don't transport errors.
return sub;
}
}
class _EventStreamSubscription implements StreamSubscription<dynamic> {
final BinanceChainWallet _client;
final String _eventName;
Function(dynamic)? _onData;
Function? _jsCallback;
int _activePauseRequests = 0;
bool _isCancelled = false;
_EventStreamSubscription(
this._client, this._eventName, Function(dynamic)? onData) {
if (_onData != null) {
_onData = Zone.current.bindUnaryCallback(onData!);
}
_resumeIfNecessary();
}
@override
Future<E> asFuture<E>([E? futureValue]) {
// Conceptionally, this should return a future completing for onDone or
// onError. Since neither can happen for an event stream, we just never
// complete
return Completer<Never>().future;
}
@override
Future<void> cancel() {
if (!_isCancelled) {
_stopListening();
_onData = null;
_isCancelled = true;
}
return Future.value();
}
@override
bool get isPaused => _activePauseRequests > 0;
@override
void onData(void Function(dynamic data)? handleData) {
if (_isCancelled) {
throw StateError('Subscription has been cancelled');
}
// Remove the current listener, then attach the new one
_stopListening();
_onData =
handleData == null ? null : Zone.current.bindUnaryCallback(handleData);
_resumeIfNecessary();
}
@override
void onDone(void Function()? handleDone) {
// Nothing to do, event streams are never done
}
@override
void onError(Function? handleError) {
// Nothing to do, event streams don't emit errors
}
@override
void pause([Future<void>? resumeSignal]) {
if (_isCancelled) return;
_activePauseRequests++;
_stopListening();
resumeSignal?.whenComplete(resume);
}
@override
void resume() {
if (_isCancelled || !isPaused) return;
_activePauseRequests--;
_resumeIfNecessary();
}
void _resumeIfNecessary() {
if (_onData != null && !isPaused) {
final cb = _jsCallback = _onData!;
_client.on(_eventName, cb as JSFunction);
}
}
void _stopListening() {
final callback = _jsCallback;
if (callback != null) {
_client.removeListener(_eventName, callback as JSFunction);
}
}
}