Interacting with TRON smart contract

24 Views Asked by At

I deployed the following smart contract.

    // SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract HelloWorld {
    function hello() public pure returns (string memory) {
        return "Hello, World!";
    }
}

And my code to interact with it is as following:

var TronWeb = require('tronweb');
var tronWeb = new TronWeb({
    fullHost: 'http://localhost:26667',
    privateKey: 'xxxxxxx'
});

async function fetchAbi(contractAddress) {
    try {
        let contractInfo = await tronWeb.trx.getContract(contractAddress);
        return contractInfo.abi; // Return the ABI
    } catch (error) {
        console.error("Error in fetchAbi function:", error);
        throw error; // Rethrow to handle it in the caller
    }
}

async function main(abiData) {
    try {
        let contractAddress = 'TYiwthLXHcarLqRxWA5FKT1NgJicLCanhK';
        // Ensure the property name used here matches what fetchAbi returns. It seems it should be `abi`, not `entrys`.
        let actualAbi = abiData.entrys; // Adding a fallback in case the property name is different
        //let realabi = [{"outputs":[{"type":"string"}],"name":"hello","stateMutability":"Pure","type":"Function"}]
        let contract = await tronWeb.contract(actualAbi, contractAddress);
        let result = await contract.hello().call();
        console.log(result);
    } catch (error) {
        console.error("Error in main function:", error);
    }
}

(async () => {
    try {
        const contractAddress = 'TGTMtrSkN41e49vBNTBRifAm9PbXfEuhQi';
        const abi = await fetchAbi(contractAddress);
        console.log(abi)
        await main(abi);
    } catch (error) {
        console.error("Error in executing main with fetched ABI:", error);
    }
})();

I am getting this output which displays the ABI and an error.

{
  entrys: [ { name: 'hello', stateMutability: 'View', type: 'Function' } ]
}
Error in main function: The call has been reverted or has thrown an error.

Not sure what I am doing wrong. I have verified the contract is at the contract address being used.

0

There are 0 best solutions below