sendRequest method

  1. @override
Future sendRequest(
  1. String topic,
  2. String method,
  3. dynamic params, {
  4. int? id,
  5. int? ttl,
  6. EncodeOptions? encodeOptions,
  7. String? appLink,
  8. 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;

  if ((appLink ?? '').isNotEmpty) {
    // during wc_sessionAuthenticate we don't need to openURL as it will be done by the host dapp
    core.logger.t(
      'pairing sendRequest LinkMode, '
      'id: $id topic: $topic, method: $method, params: $params, ttl: $ttl',
    );
    if (openUrl) {
      final redirectURL = ReownCoreUtils.getLinkModeURL(
        appLink!,
        topic,
        message,
      );
      await ReownCoreUtils.openURL(redirectURL);
    }
  } else {
    core.logger.t(
      'pairing sendRequest Relay, '
      'id: $id topic: $topic, method: $method, params: $params, ttl: $ttl',
    );
    // 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;
  }
}