在执行一个 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()); // defaults to http://localhost:8545/
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)