Switch chains
Get the current chain
To get the current network, you can use the eth_chainId request (with no params)
Usage
do {
guard let provider = try await OFSDK.shared.getEthereumProvider(params: OFGetEthereumProviderParams()) else {
print("Provider not available")
return nil
}
let req = RPCRequest<[String]>(id: 1, jsonrpc: "2.0", method: "eth_chainId", params: [])
provider.send(request: req) { (resp: Web3Response<String>) in
if let chainIdHex: String = resp.result {
let chainIdDec = Int(chainIdHex.dropFirst(2), radix: 16) ?? -1
print("chainId (hex): \(chainIdHex), (dec): \(chainIdDec)")
}
}
} catch {
print("Failed to get current network: \(error)")
}Switch chain
To switch the smart wallet to a different network, send a wallet_switchEthereumChain JSON-RPC request to the wallet's EIP-1193 provider. In the request's params, specify your target chainId as a hexadecimal string.
Usage
do {
guard let provider = try await OFSDK.shared.getEthereumProvider(params: OFGetEthereumProviderParams()) else {
print("Provider not available")
return nil
}
let req = RPCRequest<[[String: String]]>(id: 1, jsonrpc: "2.0", method: "wallet_switchEthereumChain", params: [["chainId": "0x5"]])
provider.send(request: req) { (resp: Web3Response<String>) in
if let chainIdHex: String = resp.result {
let chainIdDec = Int(chainIdHex.dropFirst(2), radix: 16) ?? -1
print("chainId (hex): \(chainIdHex), (dec): \(chainIdDec)")
}
}
} catch {
print("Failed to switch chain: \(error)")
}:::