如何从Web3swift获得ETH交易状态?
我在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过调用
getTransActionReceipt
函数来获取在区块链上执行的事务的接收。首先,您需要将哈希字符串转换为数据字节,然后调用该函数。getTransactionReceipt
函数返回类型transactionReceipt
,从收据中您可以获取状态,块号,合同地址等...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 typeTransactionReceipt
and from receipt you can fetch status, block number, contract address etc...