如何从Web3swift获得ETH交易状态?

发布于 2025-02-09 17:18:54 字数 1517 浏览 0 评论 0原文

我在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获得实时交易状态?谢谢你!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

折戟 2025-02-16 17:18:54

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

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)
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文