EAOE
EAOE

Reputation: 63

Getting decoded output from a smart contract transaction

I am executing functions of a smart contract through web3j using the following code :

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());

It executes fine and the function is run, however i don't know how to read the output given by the contract, how can i read that output ?

Upvotes: 2

Views: 2588

Answers (2)

EAOE
EAOE

Reputation: 63

this will return the hex value of the function call

  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());
        }

Upvotes: 1

Mikko Ohtamaa
Mikko Ohtamaa

Reputation: 83438

Ethereum transaction hash is the unique id of the transaction. With this transaction hash, you can query the transaction status from the network.

The underlying JSON-RPC call is called eth_getTransactionReceipt. Here is Web3.js documentation.

If your smart contract emits events, you can also read those.

Upvotes: 0

Related Questions