The Dynamic Swift SDK provides wallet management functionality for Ethereum-based wallets. This guide covers the core wallet operations demonstrated in the sample application: creating wallets, checking balances, signing messages, and sending transactions.
To create a new wallet for an authenticated user, use the createWalletAccount function:
Copy
Ask AI
import DynamicSwiftSDKlet dynamicClient: DynamicClient// Create a new wallet accountdo { let accountAddress = try await createWalletAccount(client: dynamicClient) print("✅ Wallet created successfully!") print("Account Address: \(accountAddress)") // The wallet is automatically added to the user's verified credentials} catch { print("❌ Failed to create wallet: \(error)")}
// Transaction parameters (as used in sample app)let amount = BigUInt(10000000000000000) // 0.01 ETH in weilet recipient = EthereumAddress("0xd4f748199B91c22095150d2d4Cca3Fe6175B0CbA") let chainId = SupportedEthereumNetwork.sepoliaTestnet.chainConfig.chainIddo { // Get network client for Sepolia let networkClient = try await ethereumWallet.getNetworkClient(for: chainId) // Get current gas price and set gas limit let gasPrice = try await networkClient.eth_gasPriceBigInt() let gasLimit = BigUInt(21_000) // Standard ETH transfer print("🌐 Network: Ethereum Sepolia (Chain ID: \(chainId))") print("💰 Amount: 0.01 ETH") print("📍 From: \(ethereumWallet.address.asString())") print("📍 To: \(recipient.asString())") print("⛽ Gas Price: \(gasPrice) wei") print("⛽ Gas Limit: \(gasLimit)") // Create transaction let transaction = EthereumTransaction( from: ethereumWallet.address, to: recipient, value: amount, data: Data(), nonce: nil, gasPrice: gasPrice, gasLimit: gasLimit, chainId: chainId ) print("📝 Transaction created successfully") // Send transaction let txHash = try await ethereumWallet.sendTransaction(transaction) print("✅ Transaction sent!") print("🔗 Transaction Hash: \(txHash)") // Get network details for block explorer URL if let supportedNetwork = SupportedEthereumNetwork.fromChainId(chainId) { let networkConfig = supportedNetwork.chainConfig if let explorerUrl = networkConfig.blockExplorerUrls.first { print("🔍 View on \(networkConfig.name): \(explorerUrl)/tx/\(txHash)") } }} catch { print("❌ Transaction failed: \(error)")}
do { let wallet = try EthereumWallet(address: "invalid_address", client: dynamicClient)} catch { if let nsError = error as NSError? { switch nsError.code { case 1003: print("Could not determine wallet ID from address") default: print("Wallet creation error: \(error)") } }}