Skip to main content
Switch between blockchain networks and query the current network.

Get Current Network

Retrieve the current network for a wallet:
import com.dynamic.sdk.android.DynamicSDK
import com.dynamic.sdk.android.Models.BaseWallet

val sdk = DynamicSDK.getInstance()
val wallet: BaseWallet = sdk.wallets.userWallets.first()

val network = sdk.wallets.getNetwork(wallet)
println("Current network: $network")
println("Chain ID: ${network.chainId}")

Switch Network

Switch a wallet to a different network:
import com.dynamic.sdk.android.DynamicSDK
import com.dynamic.sdk.android.Models.BaseWallet
import com.dynamic.sdk.android.Models.Network
import kotlinx.serialization.json.JsonPrimitive

val sdk = DynamicSDK.getInstance()
val wallet: BaseWallet = sdk.wallets.userWallets.first { it.chain == "EVM" }

// Switch to Polygon (chain ID 137)
val polygonNetwork = Network(JsonPrimitive(137))
sdk.wallets.switchNetwork(wallet, polygonNetwork)

// Switch to Ethereum Mainnet (chain ID 1)
val mainnetNetwork = Network(JsonPrimitive(1))
sdk.wallets.switchNetwork(wallet, mainnetNetwork)

// Switch to Base (chain ID 8453)
val baseNetwork = Network(JsonPrimitive(8453))
sdk.wallets.switchNetwork(wallet, baseNetwork)

Common Network IDs

Here are some commonly used network chain IDs:

Ethereum Networks

NetworkChain ID
Ethereum Mainnet1
Sepolia Testnet11155111
Holesky Testnet17000

Layer 2 Networks

NetworkChain ID
Polygon137
Polygon Mumbai (Testnet)80001
Arbitrum One42161
Arbitrum Sepolia421614
Optimism10
Optimism Sepolia11155420
Base8453
Base Sepolia84532

Best Practices

1. Handle Network Switching Gracefully

Always handle errors and provide feedback to users:
try {
    sdk.wallets.switchNetwork(wallet, network)
} catch (e: Exception) {
    when {
        e.message?.contains("user rejected") == true -> {
            showMessage("Network switch cancelled")
        }
        e.message?.contains("unsupported") == true -> {
            showMessage("This network is not supported")
        }
        else -> {
            showMessage("Failed to switch network: ${e.message}")
        }
    }
}

2. Verify Network After Switching

Confirm the network switch was successful:
val network = Network(JsonPrimitive(chainId))
sdk.wallets.switchNetwork(wallet, network)

// Verify the switch
val currentNetwork = sdk.wallets.getNetwork(wallet)
if (currentNetwork.chainId == chainId.toString()) {
    println("Successfully switched to chain $chainId")
}

3. Support Testnet Networks

Make it easy for developers to test:
val isTestnet = when (chainId) {
    11155111, 80001, 421614, 11155420, 84532 -> true
    else -> false
}

if (isTestnet) {
    showWarning("You are connected to a testnet")
}

Troubleshooting

Network Switch Fails

  • Verify the chain ID is correct
  • Check that the network is enabled in your Dynamic dashboard
  • Ensure the wallet type supports the network (EVM wallets only)

Wrong Network Displayed

  • Call getNetwork() after switching to verify
  • Check for any pending transactions on the old network

What’s Next