sendRequest method
Future
sendRequest(
- String topic,
- String method,
- dynamic params, {
- int? id,
- int? ttl,
- EncodeOptions? encodeOptions,
- String? appLink,
- bool openUrl = true,
override
Implementation
@override
Future<dynamic> sendRequest(
String topic,
String method,
dynamic params, {
int? id,
int? ttl,
EncodeOptions? encodeOptions,
String? appLink,
bool openUrl = true,
}) async {
final payload = JsonRpcUtils.formatJsonRpcRequest(
method,
params,
id: id,
);
final message = await core.crypto.encode(
topic,
payload,
options: encodeOptions,
);
if (message == null) {
return;
}
// print('adding payload to pending requests: ${payload['id']}');
final resp = PendingRequestResponse(completer: Completer());
resp.completer.future.catchError((err) {
// Catch the error so that it won't throw an uncaught error
});
pendingRequests[payload['id']] = resp;
core.logger.d(
'[$runtimeType] sendRequest appLink: $appLink, '
'id: $id topic: $topic, method: $method, params: $params, ttl: $ttl',
);
if ((appLink ?? '').isNotEmpty) {
// during wc_sessionAuthenticate we don't need to openURL as it will be done by the host dapp
if (openUrl) {
final redirectURL = ReownCoreUtils.getLinkModeURL(
appLink!,
topic,
message,
);
await ReownCoreUtils.openURL(redirectURL);
}
} else {
// RpcOptions opts = MethodConstants.RPC_OPTS[method]!['req']!;
RpcOptions opts = MethodConstants.RPC_OPTS[method]!['req']!;
if (ttl != null) {
opts = opts.copyWith(ttl: ttl);
}
//
await core.relayClient.publish(
topic: topic,
message: message,
ttl: ttl ?? opts.ttl,
tag: opts.tag,
);
}
// Get the result from the completer, if it's an error, throw it
try {
if (resp.error != null) {
throw resp.error!;
}
// print('checking if completed');
if (resp.completer.isCompleted) {
return resp.response;
}
return await resp.completer.future;
} catch (e) {
rethrow;
}
}