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');
  }
  String reqChainId = chainId;
  final isValidChainId = NamespaceUtils.isValidChainId(chainId);
  if (!isValidChainId) {
    if (selectedChain!.chainId == chainId) {
      reqChainId = '${CoreConstants.namespace}:$chainId';
    } else {
      throw Errors.getSdkError(
        Errors.UNSUPPORTED_CHAINS,
        context: 'chainId should conform to "CAIP-2" format',
      );
    }
  }
  //
  _logger.d(
    '[$runtimeType] request, chainId: $reqChainId, '
    '${jsonEncode(request.toJson())}',
  );
  try {
    if (_currentSession!.sessionService.isMagic) {
      return await magicService.instance.request(
        chainId: reqChainId,
        request: request,
      );
    }
    if (_currentSession!.sessionService.isCoinbase) {
      return await await coinbaseService.instance.request(
        chainId: switchToChainId ?? reqChainId,
        request: request,
      );
    }
    return await _appKit.request(
      topic: topic!,
      chainId: reqChainId,
      request: request,
    );
  } catch (e) {
    if (_isUserRejectedError(e)) {
      _logger.i('[$runtimeType] User declined request');
      onModalError.broadcast(UserRejectedConnection());
      if (request.method == MethodsConstants.walletSwitchEthChain ||
          request.method == MethodsConstants.walletAddEthChain) {
        rethrow;
      }
      return 'User rejected';
    } 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.
        // disconnect();
        throw ReownAppKitModalException('Coinbase Wallet Error');
      }
      rethrow;
    }
  }
}