sendRequest method
Future
sendRequest(
- String topic,
- String method,
- Map<
String, dynamic> params, { - int? id,
- int? ttl,
- EncodeOptions? encodeOptions,
- String? appLink,
- bool openUrl = true,
- TVFData? tvf,
override
Implementation
@override
Future<dynamic> sendRequest(
String topic,
String method,
Map<String, dynamic> params, {
int? id,
int? ttl,
EncodeOptions? encodeOptions,
String? appLink,
bool openUrl = true,
TVFData? tvf,
}) async {
final payload = JsonRpcUtils.formatJsonRpcRequest(
method,
params,
id: id,
);
final requestId = payload['id'] as int;
final isLinkMode = (appLink ?? '').isNotEmpty;
final message = await core.crypto.encode(
topic,
payload,
options: isLinkMode
? EncodeOptions(type: EncodeOptions.TYPE_2)
: encodeOptions,
);
if (message == null) {
return;
}
// print('adding payload to pending requests: $requestId');
final resp = PendingRequestResponse(completer: Completer(), method: method);
resp.completer.future.catchError(
(err) => core.events.recordEvent(BasicCoreEvent(
event: CoreEventType.ERROR,
properties: CoreEventProperties(topic: topic, method: resp.method),
)),
);
pendingRequests[requestId] = resp;
if (isLinkMode) {
// 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);
}
// Send Event through Events SDK
core.events.recordEvent(LinkModeRequestEvent(
direction: 'sent',
correlationId: requestId,
method: method,
));
core.logger.d(
'[$runtimeType] sendRequest linkMode ($appLink), '
'id: $requestId topic: $topic, method: $method, '
'params: $params, ttl: $ttl',
);
} else {
RpcOptions opts = MethodConstants.RPC_OPTS[method]!['req']!;
if (ttl != null) {
opts = opts.copyWith(ttl: ttl);
}
//
await core.relayClient.publish(
topic: topic,
message: message,
options: PublishOptions(
ttl: ttl ?? opts.ttl,
tag: opts.tag,
correlationId: requestId,
// tvf data is sent only on tvfMethods methods
tvf: _shouldSendTVF(opts.tag) ? tvf?.toJson() : null,
),
);
core.logger.d(
'[$runtimeType] sendRequest relayClient, '
'id: $requestId topic: $topic, method: $method, '
'params: $params, ttl: ${ttl ?? opts.ttl}',
);
}
// 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;
}
}