Transactions

如何從收據中找到交易成功?

  • March 14, 2021

下面是一個成功的例子:

{
 "hash": "0x17e0b789c7645eef46316d475419c02391181881f70eb0451bb13c29a2dd5133",
 "nonce": "0x14d",
 "blockHash": "0x7368ed3ff6d034cb38b8c521e272aa67a9fd50fe34ebbf0960fe47949d10a15d",
 "blockNumber": "0xd8e00e",
 "transactionIndex": "0x0",
 "from": "0x2096882843192129dd5b778531adefe4dfbeebed",
 "to": "0xbc948ec5aaa61a66b1641b1d403965701effacec",
 "value": "0x0",
 "gasPrice": "0x77359400",
 "gas": "0x4c4b40",
 "input": "0xea598cb00000000000000000000000000000000000000000000000000000000000002710",
 "publicKey": "0xbac5c6ab222cf37bbed7d34bd6d2ac87e5225730724dd0defb4724f9a8985e9eb6de8cb793f9c9c8f9277c53d7f5d6da814ed63570d3bc67bb78b070f18b78a0",
 "raw": "0xf88a82014d8477359400834c4b4094bc948ec5aaa61a66b1641b1d403965701effacec80a4ea598cb000000000000000000000000000000000000000000000000000000000000027101ca0a3e7242eeb8726f8b19643c3005af84e45fe4257f65e4f76e964a30aeede642aa057cf987e9afca2bc618dbe1e947075c4a0b9635a56e16f8af060a344ef3d096a",
 "r": "0xa3e7242eeb8726f8b19643c3005af84e45fe4257f65e4f76e964a30aeede642a",
 "s": "0x57cf987e9afca2bc618dbe1e947075c4a0b9635a56e16f8af060a344ef3d096a",
 "v": 28
}

以下是還原交易的範例:

{
 "hash": "0xad3595a42afda6f3d340258448bf38b7aed995b1f3f830009b9e0aec708ba96a",
 "nonce": "0x106",
 "blockHash": "0xde32076d0028df0c2d800505ec91be55601ca4ae8d5d62a378d57447fe1129f1",
 "blockNumber": "0x16ceb65",
 "transactionIndex": "0x32",
 "from": "0xa9b8902b77004c2d9faa1bd5b044ea316cf8a1a3",
 "to": "0x78f2ae797f09915f51cc1cf64fd70b4f564290aa",
 "value": "0x38d7ea4c68000",
 "gasPrice": "0x2540be400",
 "gas": "0x5208",
 "input": "0x",
 "publicKey": "0x9149f6f823a88a351f1952e98f805b066899ec3a9f21c23b24f3e82914526cfbb41580a827db88c07cabedd9a690773706c66aab73cf9e3c255535a87ef6d0ad",
 "raw": "0xf86d8201068502540be4008252089478f2ae797f09915f51cc1cf64fd70b4f564290aa87038d7ea4c680008078a0f08254590fa90a7d570b239af144e1e3189574c6a18a99035809ea64d38fd201a04c31d1937694262b8e854626e6488ae7186b054aeb192cb2dd1cf3733e7f3094",
 "r": "0xf08254590fa90a7d570b239af144e1e3189574c6a18a99035809ea64d38fd201",
 "s": "0x4c31d1937694262b8e854626e6488ae7186b054aeb192cb2dd1cf3733e7f3094",
 "v": 120
}

我怎麼知道哪個是成功的,哪個是失敗的?適用於所有交易的一般規則是什麼?

我使用此程式碼

import Logic.Connection.HttpClient;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.methods.response.EthGetTransactionReceipt;
import org.web3j.protocol.http.HttpService;

import java.io.IOException;

public class CheckReceipt {

   public String checkTransaction(String txhash, String node) throws IOException {
       HttpClient httpClient = new HttpClient();
       httpClient.openClient();
       
       Web3j web3j = Web3j.build(new HttpService(node, httpClient.getHttpClient()));
       EthGetTransactionReceipt transactionReceipt = web3j.ethGetTransactionReceipt(txhash).send();
       String status = "success";
       if (transactionReceipt.getResult() != null && !transactionReceipt.hasError()) {
           if (transactionReceipt.getTransactionReceipt().isPresent()) {
               String statusOfTx = transactionReceipt.getTransactionReceipt().get().getStatus();
//                System.out.printf("The status is %s%n", statusOfTx);
               if (!statusOfTx.equals("0x1")) {
//                    System.out.printf("Transaction reverted: %s%n", transactionReceipt.getTransactionReceipt().get().toString());
                   status = "reverted";
               }
           }
           else{
               status = "failed";
           }
       }
       else{
           status = "failed";
       }
       httpClient.closeClient();
       
       return status;
   }
}

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