Reputation: 243
I am using web3j to query the Ethereum blockchain. Now I want to check if a transaction was mined or just sent to the network. How can I achieve this?
Upvotes: 9
Views: 19340
Reputation: 115
Use org.web3j.protocol.core.Ethereum ethGetTransactionReceipt function to get status using hash
public Boolean getTransactionStatus(Web3j web3j, String transactionHash) throws Exception{
Optional<TransactionReceipt> receipt = null;
Boolean status=null;
try{
receipt = web3j.ethGetTransactionReceipt(transactionHash).send().getTransactionReceipt();
if(receipt.isPresent())
status = receipt.get().isStatusOk();
}catch(IOException e){
throw new Exception(e);
}
return status;
}
Upvotes: 0
Reputation: 11
/**
* 通过hash查询交易状态,处理状态。成功success,失败fail,未知unknown
* @param hash
* @return
*/
public String txStatus(String hash){
if(StringUtils.isBlank(hash)) {
return STATUS_TX_UNKNOWN;
}
try {
EthGetTransactionReceipt resp = web3j.ethGetTransactionReceipt(hash).send();
if(resp.getTransactionReceipt().isPresent()) {
TransactionReceipt receipt = resp.getTransactionReceipt().get();
String status = StringUtils.equals(receipt.getStatus(), "0x1") ?
"success" : "fail";
return status;
}
}catch (Exception e){
log.info("txStatusFail {}", e.getMessage(), e);
}
return "hash_unknown";
}
Upvotes: 1
Reputation: 1
As mentioned before, you can use web3.eth.getTransactionReceipt(hash [, callback])
It will return the object with status. It will be false for unsuccessful transactions.
Upvotes: 0
Reputation: 256
You can consider using web3.eth.getTransactionReceipt(hash [, callback])
.
It will return null
for pending transactions and an object if the transaction is successful.
Upvotes: 14