Search code examples
multiversx

How to get Smart Contract Result data?


Let's say for example we have a transaction with this Input Data and this Smart Contract Result

  • Input data: issueNonFungible@415344@415344
  • Result: @ok@4153442d383661353439

It's a simple creation of a new NFT Collection. It can be seen on the devnet at this txHash: 290498e8730975ea6d2703f4c5e0dc2c657b9480b9afc2a62c4c8818636cf062

Input Data and Smart Contract Results - Elrond explorer

I am trying to get the response of the smart contract. This is the code I have now with the consts.walletAddress being the wallet address of the sender of the initial transaction.

var txHash = new TransactionHash("290498e8730975ea6d2703f4c5e0dc2c657b9480b9afc2a62c4c8818636cf062");
var toTest = await this.provider.getTransaction(txHash, new Address(consts.walletAddress), true);
var scResults = await toTest.getSmartContractResults();
console.log(scResults)

Here is what the console.log outputs: SmartContractResults

The problem is that no data is outputted for the Smart Contract Results so I can not get the response of the smart contract. Am I doing something wrong or is this a bug in erdJs?


Solution

  • Your code is perfectly fine. There is just a small bug in the latest erdjs version that is distributed via npm.

    In the TransactionOnNetwork class the code in the package currently looks like this:

        static fromHttpResponse(response) {
            let transactionOnNetwork = new TransactionOnNetwork();
            transactionOnNetwork.type = new TransactionOnNetworkType(response.type || "");
            transactionOnNetwork.nonce = new nonce_1.Nonce(response.nonce || 0);
            transactionOnNetwork.round = response.round;
            transactionOnNetwork.epoch = response.epoch || 0;
            transactionOnNetwork.value = balance_1.Balance.fromString(response.value);
            transactionOnNetwork.sender = address_1.Address.fromBech32(response.sender);
            transactionOnNetwork.receiver = address_1.Address.fromBech32(response.receiver);
            transactionOnNetwork.gasPrice = new networkParams_1.GasPrice(response.gasPrice);
            transactionOnNetwork.gasLimit = new networkParams_1.GasLimit(response.gasLimit);
            transactionOnNetwork.data = transactionPayload_1.TransactionPayload.fromEncoded(response.data);
            transactionOnNetwork.status = new transaction_1.TransactionStatus(response.status);
            transactionOnNetwork.hyperblockNonce = new nonce_1.Nonce(response.hyperblockNonce || 0);
            transactionOnNetwork.hyperblockHash = new hash_1.Hash(response.hyperblockHash);
            transactionOnNetwork.receipt = Receipt.fromHttpResponse(response.receipt || {});
            transactionOnNetwork.results = smartContractResults_1.SmartContractResults.fromHttpResponse(response.results || []);
            return transactionOnNetwork;
        }
    

    As you can see it tries to parse the SmartContract results from the results field in the response.

    However it would be correct to parse the smartContractResults field instead.

    This can also be seen in the official github where this issue seems to be already fixed.

    So for now you can either manually patch the transactionOnNetwork.js file in your node_modules to use smartContractResults or you downgrade to a lower version until this issue is resolved in the latest npm package.