Overview

This guide covers network configuration, supported networks, and network switching with the Dynamic Swift SDK.

Prerequisites

Network Configuration

Supported Networks

import DynamicSwiftSDK

// Ethereum Sepolia Testnet (primary network in sample app)
let sepolia = SupportedEthereumNetwork.sepoliaTestnet.chainConfig
print("Sepolia Chain ID: \(sepolia.chainId)") // 11155111

Get Network Client

let dynamicClient: DynamicClient
let ethereumWallet: EthereumWallet
let chainId = SupportedEthereumNetwork.sepoliaTestnet.chainConfig.chainId

do {
    let networkClient = try await ethereumWallet.getNetworkClient(for: chainId)
    print("Connected to Sepolia network: \(chainId)")
    
    // Get gas price for transactions
    let gasPrice = try await networkClient.eth_gasPriceBigInt()
    print("Current gas price: \(gasPrice) wei")
} catch {
    print("Failed to get network client: \(error)")
}

Switch Network

// Switch to Sepolia testnet (sample app usage)
let sepoliaConfig = SupportedEthereumNetwork.sepoliaTestnet.chainConfig

do {
    try await ethereumWallet.switchNetwork(to: sepoliaConfig)
    print("🌐 Switched to Sepolia testnet")
} catch {
    print("❌ Failed to switch to Sepolia: \(error)")
}

Network Management

Switch Networks

// Switch to Sepolia testnet
let sepoliaNetwork = SupportedEthereumNetwork.sepoliaTestnet.chainConfig

do {
    try await ethereumWallet.switchNetwork(to: sepoliaNetwork)
    print("🌐 Switched to Ethereum Sepolia")
} catch {
    print("❌ Failed to switch to Sepolia: \(error)")
}

Get Network Client

let dynamicClient: DynamicClient
let ethereumWallet: EthereumWallet
let chainId = SupportedEthereumNetwork.sepoliaTestnet.chainConfig.chainId

do {
    let networkClient = try await ethereumWallet.getNetworkClient(for: chainId)
    print("Connected to Sepolia network: \(chainId)")
    
    // Get gas price for transactions
    let gasPrice = try await networkClient.eth_gasPriceBigInt()
    print("Current gas price: \(gasPrice) wei")
} catch {
    print("Failed to get network client: \(error)")
}

Best Practices

1. Network Switching

Always switch to the desired network before performing operations:
// Switch to desired network before operations
let targetNetwork = SupportedEthereumNetwork.sepoliaTestnet.chainConfig
try await ethereumWallet.switchNetwork(to: targetNetwork)

// Then perform operations
let balance = try await ethereumWallet.getBalance(.Latest)

2. Network Validation

// Validate network before switching
func isValidNetwork(_ chainId: UInt64) -> Bool {
    return SupportedEthereumNetwork.fromChainId(chainId) != nil
}

// Check before switching
let chainId = SupportedEthereumNetwork.sepoliaTestnet.chainConfig.chainId
guard isValidNetwork(chainId) else {
    print("❌ Unsupported network")
    return
}

3. Network State Management

// Track current network state
class NetworkManager {
    private var currentNetwork: SupportedEthereumNetwork?
    
    func switchToNetwork(_ network: SupportedEthereumNetwork) async throws {
        try await ethereumWallet.switchNetwork(to: network.chainConfig)
        currentNetwork = network
        print("🌐 Switched to \(network.chainConfig.name)")
    }
    
    func getCurrentNetwork() -> SupportedEthereumNetwork? {
        return currentNetwork
    }
}

Error Handling

do {
    let networkClient = try await ethereumWallet.getNetworkClient(for: chainId)
} catch {
    if let nsError = error as NSError? {
        switch nsError.code {
        case 3001:
            print("Network not supported")
        case 3002:
            print("Network connection failed")
        case 3003:
            print("Invalid chain ID")
        default:
            print("Network error: \(error)")
        }
    }
}

Next Steps

After mastering network management, you can: