Search code examples
javascriptblockchaintrontronweb

How to call balanceOf() method for non published ABI


I'm trying to call balanceOf() method on a specific Tron contract (TYukBQZ2XXCcRCReAUguyXncCWNY9CEiDQ), but what I get is Failed to execute . If I do not provide the parameters I get Invalid argument count provided - meaning at some level it works for this contract.

What is interesting it works well on contracts other than the ones created with JustSwap Factory contract eg. https://tronscan.io/#/contract/TYukBQZ2XXCcRCReAUguyXncCWNY9CEiDQ/code . The code includes the standard TRC20 methods - including balanceOf() - I'm stuck and tried all that's possible form my side, but let's just say I'm not fluent in tronweb api.

My code:

export const getDataToken = async (contractAddress, account, account2) => {
  try {
  
    const instance = await window.tronWeb.contract(
      [
        {
          constant: true,
          inputs: [{ name: "owner", type: "address" }],
          name: "balanceOf",
          outputs: [{ name: "", type: "uint256" }],
          payable: false,
          stateMutability: "view",
          type: "function"
        }
      ],
      contractAddress
    );
    console.log(instance);
    if (instance.balanceOf) {
      console.log("dadadad if");
      const tokenBalance = await instance.balanceOf(account).call();
      const tokenBalance2 = await instance.balanceOf(account2).call();
      return {
        tokenBalance: (tokenBalance / Math.pow(10, 18)).toString(),
        tokenContract: instance,
        tokenBalance2: (tokenBalance2 / Math.pow(10, 18)).toString()
      };
    }
  } catch (message) {
    console.log("error  getData  :" + message);
  }
};

const { tokenBalance, tokenContract, tokenBalance2 } = getDataToken(
  "TYukBQZ2XXCcRCReAUguyXncCWNY9CEiDQ",
  "TL4HzzxGMc1LMfs3XCi4yTJikaBVubz5y4",
  "TTFp171XD4JdUB33sDq2ydXJyUEEZjNhLD"
);

Solution

  • This function can help (example for getting JustSwap pair price):

            async function takePrice(contractAddress, token){
                var functionSelector = 'getTokenToTrxInputPrice(uint256)';
                var parameter = [
                    {
                        type: 'uint256', 
                        value: token
                    }
                ]
                var options = {};
                transaction = await window.tronWeb.transactionBuilder.triggerConstantContract(contractAddress, functionSelector, options, parameter);
                return window.tronWeb.BigNumber("0x"+transaction['constant_result'][0]);
            }
            priceUSDTTRX =  window.tronWeb.fromSun(await takePrice(USDTTRX_ADDRESS, "1000000"));
            priceSomeTone18TRX = window.tronWeb.fromSun(await takePrice(SomeTone18TRX_ADDRESS, "1"+"0".repeat(18)));