web3j.ethSendRawTransaction warten, bis die Transaktion erfolgreich abgebaut wurde - Web3j

Ich versuche, 0 Ether an dasselbe Konto mit Web3j zu senden, aber wenn ich diesen Code ausführe, gibt er mir direkt den Transaktionshash, auch wenn die Transaktion noch nicht abgebaut wurde und noch aussteht. Ich kann die Methode Transfer.sendFund(..) nicht verwenden, weil ich die Nonce von mir geben muss.

RawTransaction rawTransaction = RawTransaction.createEtherTransaction(nonce, 
    gasPriceInWei, gasLimit, credentials.getAddress(), BigInteger.ZERO);

byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, 
    credentials);

String hexValue = Numeric.toHexString(signedMessage);

EthSendTransaction ethSendTransaction =
    web3j.ethSendRawTransaction(hexValue).sendAsync().get();

String transactionHash = ethSendTransaction.getTransactionHash();

System.out.println("transaction hash: " , transactionHash )

Hat jemand eine Idee zur Lösung dieses Problems?

("web3j.ethSendRawTransaction(hexValue).send()" löst das nicht)

(alle neuen Transaktionen in jedem neuen Block beobachten, bis meine Transaktion auftritt, ist eine Lösung, aber gibt es einen besseren Weg)?

Antworten (2)

Das hat bei mir funktioniert:

while (true) {
    EthGetTransactionReceipt transactionReceipt = web3j
        .ethGetTransactionReceipt(transactionHash)
        .send();
     if (transactionReceipt.getResult() != null) {
         break;
     }
     Thread.sleep(15000);
}

Web3j hat zwei Implementierungen von org.web3j.tx.response.TransactionReceiptProcessor: PollingTransactionReceiptProcessorundQueuingTransactionReceiptProcessor

Beispiel:

TransactionReceiptProcessor receiptProcessor = new PollingTransactionReceiptProcessor(
        web3,
        TransactionManager.DEFAULT_POLLING_FREQUENCY,
        TransactionManager.DEFAULT_POLLING_ATTEMPTS_PER_TX_HASH);

TransactionReceipt receipt = receiptProcessor
        .waitForTransactionReceipt(sendTransaction.getTransactionHash());