【问题标题】:How to get ETH transaction status from web3swift?如何从 web3swift 获取 ETH 交易状态?
【发布时间】:2022-12-14 22:49:37
【问题描述】:

我在 web3swift 和 Infura 端点的帮助下完成了 ETH 交易。我无法获得该交易的状态。我使用以下代码生成了交易哈希。

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 获取实时交易状态?谢谢!

【问题讨论】:

    标签: swift ethereum web3swift


    【解决方案1】:

    您可以通过调用 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)
        }
    

    【讨论】:

      猜你喜欢
      • 2018-10-03
      • 1970-01-01
      • 2018-02-26
      • 2022-01-07
      • 2018-07-22
      • 2022-11-04
      • 1970-01-01
      • 1970-01-01
      • 2021-10-07
      相关资源
      最近更新 更多