How to call balanceOf() method for non published ABI

1.7k views Asked by At

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"
);
3

There are 3 answers

0
Alexandr Ovdienko On BEST ANSWER

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)));
0
Tony Chen On

try const tokenBalance = await instance.balanceOf(account).call({ _isConstant: true });

It works for me.

transactionBuilder.triggerConstantContract works too, but mine is simpler

0
Ivan Savchuk On

I have good result:

  const TronWeb = require("tronweb");
  const ethers = require('ethers')
 
  const MAINNET_RPC = "https://api.trongrid.io";
 
  const PLACEHOLDER_PRIVATE_KEY = "YOUR_PRIVATE_KEY";
  
  const HttpProvider = TronWeb.providers.HttpProvider;
  const fullNode = new HttpProvider(MAINNET_RPC);
  const solidityNode = new HttpProvider(MAINNET_RPC);
  const eventServer = new HttpProvider(MAINNET_RPC);

  const tronWeb = new TronWeb(fullNode,solidityNode,eventServer,PLACEHOLDER_PRIVATE_KEY);

  const startJustSwap = async () => {

    try {

      const contractTokenExchangeUSDT = 'TQn9Y2khEsLJW1ChVWFMSMeRDow5KcbLSE'; //S-USDT-TRX Token
      const parameter = [{type:`uint256`,value: 10000000}];
      const tx = await tronWebLocal.transactionBuilder.triggerConstantContract(contractToken, `trxToTokenSwapInput(uint256,uint256)`, {}, parameter);

      console.log(tx);

    } catch (e) {
      console.log(e);
    }
  };

  startJustSwap();