簡體   English   中英

從智能合約交易中獲取解碼的 output

[英]Getting decoded output from a smart contract transaction

我正在使用以下代碼通過 web3j 執行智能合約的功能:

Credentials creds = getCredentialsFromPrivateKey("private-key");
            RawTransactionManager manager = new RawTransactionManager(web3j, creds);
            String contractAddress = "0x1278f8c858d799fe1010cfc0d1eeb56508243a4d";
            BigInteger sum = new BigInteger("10000000000"); // amount you want to send
            String data = encodeTransferData(sum);
            BigInteger gasPrice = web3j.ethGasPrice().send().getGasPrice();
            BigInteger gasLimit = BigInteger.valueOf(120000); // set gas limit here
            EthSendTransaction transaction = manager.sendTransaction(gasPrice, gasLimit, contractAddress, data, null);
            System.out.println(transaction.getTransactionHash());

它執行良好並且 function 正在運行,但是我不知道如何閱讀合同給出的 output,我如何閱讀 output?

以太坊交易 hash 是交易的唯一 id。 有了這個交易hash,就可以從網絡上查詢交易狀態。

底層 JSON-RPC 調用稱為eth_getTransactionReceipt 這是Web3.js 文檔

如果你的智能合約發出事件,你也可以閱讀那些.

這將返回 function 調用的十六進制值

  private static List<Type> executeCall(Function function) throws IOException {
          String encodedFunction = FunctionEncoder.encode(function);
          org.web3j.protocol.core.methods.response.EthCall ethCall = web3j.ethCall(
              Transaction.createEthCallTransaction(
                  "0x753ebAf6F6D5C2e3E6D469DEc5694Cd3Aa1A0c21", "0x47480bac30de77cd030b8a8dad2d6a2ecdb7f27a", encodedFunction),
              DefaultBlockParameterName.LATEST)
              .send();
          String value = ethCall.getValue();
          System.out.println(value);
          System.out.println(FunctionReturnDecoder.decode(value, function.getOutputParameters()));
          return FunctionReturnDecoder.decode(value, function.getOutputParameters());
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM