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 W3MServiceException('Session is null');
  }
  String reqChainId = chainId;

  final isValidChainId = NamespaceUtils.isValidChainId(chainId);
  if (!isValidChainId) {
    if (selectedChain!.namespace.contains(chainId)) {
      reqChainId = selectedChain!.namespace;
    } else {
      throw Errors.getSdkError(
        Errors.UNSUPPORTED_CHAINS,
        context: 'chainId should conform to "namespace:chainId" 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 _web3App.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 W3MCoinbaseException) {
        // 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 W3MServiceException('Coinbase Wallet Error');
      }
      rethrow;
    }
  }
}