簡體   English   中英

如何從 web3swift 獲取 ETH 交易狀態?

[英]How to get ETH transaction status from web3swift?

我在 web3swift 和 Infura 端點的幫助下完成了 ETH 交易。 我無法獲得該交易的狀態。 我使用以下代碼生成了事務 hash。

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)

這是我獲取交易收據的代碼

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

收據在幾秒鍾后生成。 如何使用 web3swift 和 infura API 獲取實時交易狀態? 謝謝!

您可以通過調用getTransactionReceipt function 獲取在區塊鏈上執行的交易收據。首先,您需要將 hash 字符串轉換為數據字節,然后調用 function。getTransactionReceipt getTransactionReceipt返回TransactionReceipt類型的結果,您可以從收據中獲取狀態,塊號,合約地址等...

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)
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM