request method

  1. @override
Future request({
  1. required String? topic,
  2. required String chainId,
  3. required SessionRequestParams request,
  4. String? switchToChainId,
})
override

Make a request

Implementation

@override
Future<dynamic> request({
  required String? topic,
  required String chainId,
  required SessionRequestParams request,
  String? switchToChainId,
}) async {
  if (_currentSession == null) {
    throw ReownAppKitModalException('Session is null');
  }
  if (!NamespaceUtils.isValidChainId(chainId)) {
    throw Errors.getSdkError(
      Errors.UNSUPPORTED_CHAINS,
      context: 'chainId should conform to "CAIP-2" format',
    ).toSignError();
  }
  //
  _appKit.core.logger.d(
    '[$runtimeType] request, chainId: $chainId, '
    '${jsonEncode(request.toJson())}',
  );
  try {
    if (_currentSession!.sessionService.isMagic) {
      return await _magicService.request(chainId: chainId, request: request);
    }
    if (_currentSession!.sessionService.isCoinbase) {
      return await _coinbaseService.request(
        chainId: switchToChainId ?? chainId,
        request: request,
      );
    }
    if (_currentSession!.sessionService.isPhantom) {
      return await _phantomService.request(
        chainId: chainId,
        request: request,
      );
    }
    if (_currentSession!.sessionService.isSolflare) {
      return await _solflareService.request(
        chainId: chainId,
        request: request,
      );
    }

    final requestId = JsonRpcUtils.payloadId();
    final pendingRequest = _appKit.request(
      requestId: requestId,
      topic: topic!,
      chainId: chainId,
      request: request,
    );

    _launchRequestOnWallet(requestId);

    return await pendingRequest;
  } catch (e) {
    if (_isUserRejectedError(e)) {
      onModalError.broadcast(UserRejectedRequest());
      if (request.method == MethodsConstants.walletSwitchEthChain ||
          request.method == MethodsConstants.walletAddEthChain) {
        rethrow;
      }
      return Errors.getSdkError(Errors.USER_REJECTED).toJson();
    } else {
      if (e is CoinbaseServiceException) {
        // If the error is due to no session on Coinbase Wallet we disconnnect the session on Modal.
        // This is the only way to detect a missing session since Coinbase Wallet is not sending any event.
        throw ReownAppKitModalException('Coinbase Wallet Error');
      }
      rethrow;
    }
  }
}