selectChain method
Future<void>
selectChain(
- W3MChainInfo? chainInfo, {
- bool switchChain = false,
- 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(
W3MChainInfo? 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(chainInfo.namespace);
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);
}
}
}