在执行一个 ERC20 智能合约的transfer调用时发生的org.web3j的一个交易错误:
org.web3j.protocol.exceptions.TransactionException: Error processing request: unknown transaction。
Credentials credentials = null
try {
this.initWeb3Client()
credentials = WalletUtils.loadCredentials(pwd, path)
MyToken mt = MyToken.load(ADDRESS,web3j,
credentials, BigInteger.valueOf(20_000_000_000L),BigInteger.valueOf(4_300_000L))
TransactionReceipt tr = oso.transfer(address,value).send()
System.out.println("hash:"+tr.getTransactionHash())
} catch (IOException e) {
e.printStackTrace()
} catch (CipherException e) {
e.printStackTrace()
} catch (Exception e) {
e.printStackTrace()
}
交易被成功的提交到了Rinkeby测试链上,但是抛出了这个错误信息:
org.web3j.protocol.exceptions.TransactionException: Error processing request: unknown transaction
at org.web3j.tx.response.TransactionReceiptProcessor.sendTransactionReceiptRequest(TransactionReceiptProcessor.java:32)
at org.web3j.tx.response.PollingTransactionReceiptProcessor.getTransactionReceipt(PollingTransactionReceiptProcessor.java:37)
at org.web3j.tx.response.PollingTransactionReceiptProcessor.waitForTransactionReceipt(PollingTransactionReceiptProcessor.java:29)
at org.web3j.tx.TransactionManager.processResponse(TransactionManager.java:72)
at org.web3j.tx.TransactionManager.executeTransaction(TransactionManager.java:51)
at org.web3j.tx.ManagedTransaction.send(ManagedTransaction.java:70)
at org.web3j.tx.Contract.executeTransaction(Contract.java:223)
at org.web3j.tx.Contract.executeTransaction(Contract.java:207)
at org.web3j.tx.Contract.executeTransaction(Contract.java:201)
at org.web3j.tx.Contract.lambda$executeRemoteCallTransaction$3(Contract.java:240)
at org.web3j.protocol.core.RemoteCall.send(RemoteCall.java:30)
at com.novel.balbit.ports.contract.MyTokenTest.transfer(MyTokenTest.java:358)
at com.novel.balbit.ports.contract.MyTokenTest.main(MyTokenTest.java:396)
问题可能解决方法
这个问题应该发生在TransactionReceiptProcessor内部。
当调用web3j.ethGetTransactionReceipt(transactionHash).send()时内部会执行waitForTransactionReceipt。
你的节点可能还没有彻底准备好,无法给你提供一个有效的TransactionReceipt。
你可以通过手动构建交易来解决这个问题:
Web3j web3 = Web3j.build(new HttpService())
Credentials credentials = WalletUtils.loadCredentials("password", "/path/to/walletfile")
// get the next available nonce
EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount(
address, DefaultBlockParameterName.LATEST).sendAsync().get()
BigInteger nonce = ethGetTransactionCount.getTransactionCount()
// create our transaction
RawTransaction rawTransaction = RawTransaction.createEtherTransaction(
nonce, <gas price>, <gas limit>, <toAddress>, <value>)
// sign & send our transaction
byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials)
String hexValue = Hex.toHexString(signedMessage)
// FROM here you can get the tx hash.
EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(hexValue).send()
虽然可以使用自定义的交易管理器,但尽量尝试更改轮询的次数来增加获得txhash的概率。
public static final int DEFAULT_POLLING_ATTEMPTS_PER_TX_HASH = 40;
public static final long DEFAULT_POLLING_FREQUENCY = 1000 * 15;
例如:
new Transfer(client, new ClientTransactionManager(client ,fromAddress, 100))
另外一个明确的方法:
val txManager = new RawTransactionManager(client,credentials,100,1000 * 15)
val transfer = new Transfer(client,txManager)
transfer.sendFunds(
walletAddress,
amount,
currency.convert)
原文《以太坊常见问题和错误》中的:
http://cw.hubwiz.com/card/c/ethereum-FAQ/1/1/5/
另外推荐一些之前的教程:
- python以太坊,主要是针对python围绕web3.py进行区块链以太坊应用开发的讲解。
- web3j,主要是针对java和android程序员围绕web3j库进行区块链以太坊开发的讲解。
- php以太坊,主要是介绍使用php进行智能合约开发交互,进行账号创建、交易、转账、代币开发以及过滤器和事件等内容。
- 以太坊开发,主要是介绍使用node.js、mongodb、区块链、ipfs实现去中心化电商DApp实战,适合进阶。
- 以太坊教程,主要介绍智能合约与dapp应用开发,适合入门。