selectChain method

  1. @override
Future<void> selectChain(
  1. ReownAppKitModalNetworkInfo? chainInfo, {
  2. bool switchChain = false,
  3. bool logEvent = true,
})
override

Sets the selectedChain and gets the chainBalance. If the wallet is already connected, it will request the chain to be changed and will update the session with the new chain. If chainInfo is null this will disconnect the wallet.

Implementation

@override
Future<void> selectChain(
  ReownAppKitModalNetworkInfo? chainInfo, {
  bool switchChain = false,
  bool logEvent = true,
}) async {
  _checkInitialized();

  if (chainInfo?.chainId == _currentSelectedChainId) {
    return;
  }

  // If the chain is null, disconnect and stop.
  if (chainInfo == null) {
    await disconnect();
    return;
  }

  _chainBalance = null;

  if (_currentSession?.sessionService.isMagic == true) {
    await magicService.instance.switchNetwork(chainId: chainInfo.chainId);
    // onModalNetworkChange.broadcast(ModalNetworkChange(
    //   chainId: chainInfo.namespace,
    // ));
  } else {
    final hasValidSession = _isConnected && _currentSession != null;
    if (switchChain && hasValidSession && _currentSelectedChainId != null) {
      final approvedChains = _currentSession!.getApprovedChains() ?? [];
      final hasChainAlready = approvedChains.contains(
        '${CoreConstants.namespace}:${chainInfo.chainId}',
      );
      if (!hasChainAlready) {
        requestSwitchToChain(chainInfo);
        final hasSwitchMethod = _currentSession!.hasSwitchMethod();
        if (hasSwitchMethod) {
          launchConnectedWallet();
        }
      } else {
        await _setLocalEthChain(chainInfo.chainId, logEvent: logEvent);
      }
    } else {
      await _setLocalEthChain(chainInfo.chainId, logEvent: logEvent);
    }
  }
}