如何为未发布的 ABI 调用 balanceOf() 方法

How to call balanceOf() method for non published ABI

我试图在特定的 Tron 合约 (TYukBQZ2XXCcRCReAUguyXncCWNY9CEiDQ) 上调用 balanceOf() 方法,但我得到的是 Failed to execute 。如果我不提供参数,我会得到 Invalid argument count provided - 这意味着在某种程度上它适用于该合约。

有趣的是,它在 JustSwap Factory 合约创建的合约以外的合约上运行良好,例如。 https://tronscan.io/#/contract/TYukBQZ2XXCcRCReAUguyXncCWNY9CEiDQ/code。 该代码包括标准的 TRC20 方法 - 包括 balanceOf() - 我被卡住了并尝试了所有可能的方法,但我们只是说我不熟悉 tronweb api.

我的代码:

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

此函数可以提供帮助(获取 JustSwap 对价格的示例):

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

我的成绩不错:

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

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

对我有用。

transactionBuilder.triggerConstantContract 也可以,但我的更简单