5

Im trying to send 0 ether to the same account with Web3j, however when i run this code it give me directly the transaction hash, even if the transaction has not been mined yet and still pending. I can't use the method Transfer.sendFund(..) because i need the nonce to be given by me.

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 )

Does anyone have an idea to solve this probelm?

("web3j.ethSendRawTransaction(hexValue).send()" dont solve this)

(watch all new transactions in each new blocks until my transaction occure is a solution, but is there a better way)?

2 Answers 2

4

That worked for me:

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

Web3j have two implementations of org.web3j.tx.response.TransactionReceiptProcessor: PollingTransactionReceiptProcessor and QueuingTransactionReceiptProcessor

Example:

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.