0

I did The ETH transaction with help of web3swift and Infura endpoint. I can't able to get the status of that transaction. I have generated a transaction hash by using the following code.

guard
        let fromAddress = walletAddress,
        let walletAddress = EthereumAddress(fromAddress),
        let toaddress = EthereumAddress(toAddress),
        let amountDouble = Web3.Utils.parseToBigUInt(eth, units: .eth),
        let gasPrice = Web3.Utils.parseToBigUInt(String(format: "%.10f", gasPrice), units: .eth)
    else { throw LocalError.walletError }
    var options = TransactionOptions.defaultOptions
    options.gasLimit = .manual(BigUInt(gasLimit))
    options.from = walletAddress
    options.value = amountDouble
    options.gasPrice = .manual(gasPrice)
    options.to = toaddress
    let param: [ AnyObject ] = [toaddress, amountDouble] as [ AnyObject ]

    guard
        let intermediateSend = self.web3Instance?.contract(Web3.Utils.coldWalletABI, at: toaddress, abiVersion: 2),
        let transaction = intermediateSend.write(parameters: param, extraData: Data(), transactionOptions: options),
        let walletPassword = mainAccount.walletPassword
    else { throw LocalError.walletError }
    DispatchQueue.main.async {
        NotificationCenter.default.post(name: Notification.transactionInitiated, object: nil)
    }
    let sendResult = try transaction.send(password: walletPassword)
    Log.s(sendResult)

And this is my code for getting a transaction receipt

let receipt = try self.web3Instance.eth.getTransactionReceipt(sendResult.hash)

The receipt was generated after a few seconds. how to get real-time transaction status using web3swift and infura API? Thank you!

1 Answers1

1

You can fetch the receipt of transaction performed at blockchain by calling the getTransactionReceipt function. First of all, you need to convert your hash string to data bytes and then call the function. getTransactionReceipt function returns result of type TransactionReceipt and from receipt you can fetch status, block number, contract address etc...

guard let bytecode = Data.fromHex(hash) else {
        completion(Result.failure(NSError.init(domain: "Error in byte code", code: 3)))
        return
    }
do {
        let receipt = try web3Instance.eth.getTransactionReceipt(bytecode)
        print(receipt.status)
    }catch {
        print(error)
    }
Umer Farooq
  • 583
  • 3
  • 17