Web3j

web3j.ethSendRawTransaction 等到交易被成功探勘 - Web3j

  • February 10, 2022

我試圖用 Web3j 將 0 乙太幣發送到同一個帳戶,但是當我執行此程式碼時,它會直接給我交易雜湊,即使交易尚未被探勘並且仍處於待處理狀態。我不能使用 Transfer.sendFund(..) 方法,因為我需要我提供隨機數。

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 )

有沒有人有解決這個問題的想法?

(“web3j.ethSendRawTransaction(hexValue).send()”不能解決這個問題)

(觀察每個新區塊中的所有新交易,直到我的交易發生是一個解決方案,但有沒有更好的方法)?

這對我有用:

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

Web3j 有兩個實現org.web3j.tx.response.TransactionReceiptProcessorPollingTransactionReceiptProcessorQueuingTransactionReceiptProcessor

例子:

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

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

引用自:https://ethereum.stackexchange.com/questions/61975