How to conduct an ETH transfer from web3swift?

268 Views Asked by At

How do I send GoerliETH from one wallet to considering I have my Wallet Address, Private Key, Infura Goerli API Key and the recipient Wallet Address.

I have tried to get balanceOf for the wallet so far, and that works with the following code I found elsewhere :

import web3swift
import BigInt

 struct Wallet {
            let address: String
            let data: Data
            let name: String
            let isHD: Bool
        }

        struct HDKey {
            let name: String?
            let address: String
        }
        let password = "SOME_PASSWORD"
        let key = "MY_PRIVATE_KEY" // Some private key
        let formattedKey = key.trimmingCharacters(in: .whitespacesAndNewlines)
        let dataKey = Data.fromHex(formattedKey)!
        let keystore = try! EthereumKeystoreV3(privateKey: dataKey, password: password)!
        let name = "MY_WALLET_NAME"
        let keyData = try! JSONEncoder().encode(keystore.keystoreParams)
        let address = keystore.addresses!.first!.address
        let wallet = Wallet(address: address, data: keyData, name: name, isHD: false)
        let data = wallet.data
        let keystoreManager: KeystoreManager
        if wallet.isHD {
            let keystore = BIP32Keystore(data)!
            keystoreManager = KeystoreManager([keystore])
        } else {
            let keystore = EthereumKeystoreV3(data)!
            keystoreManager = KeystoreManager([keystore])
        }
        
        let endpoint = "https://goerli.infura.io/v3/MY_API"
        let web3 = web3(provider: Web3HttpProvider(URL(string: endpoint)!)!)

    web3.addKeystoreManager(keystoreManager)
    let coldWalletAddress = EthereumAddress("0xMY_WALLET_ADDRESS")!
    
    let walletAddress = EthereumAddress(wallet.address)! //
    
        web3.transactionOptions.from = walletAddress
        web3.transactionOptions.chainID = 5
        let balanceResult = try! web3.eth.getBalance(address: walletAddress)
        let balanceString = Web3.Utils.formatToEthereumUnits(balanceResult, toUnits: .eth, decimals: 3)!

I now wish to figure out how I can transfer some of this ETH into another wallet further after this code. I understand this process might require me to create a transaction, sign it with my key and then broadcast this transaction... I just don't know how to. Hoping to get a clear example as an answer!

0

There are 0 best solutions below