tronprotocol / tronweb

Javascript API Library for interacting with the TRON Network
MIT License
388 stars 259 forks source link

Error in Interact with the contract "Property 'balanceOf' does not exist on type 'Contract'. #491

Closed LACT17 closed 2 months ago

LACT17 commented 4 months ago

I'm encountering an issue retrieving the USDT (TRC20 token) balance. I've followed the steps outlined in the TronWeb documentation for calling contracts (https://tronweb.network/docu/docs/6.0.0-beta.0/Interact%20with%20contract#call), but I'm getting an error message stating "Property 'balanceOf' does not exist on type 'Contract'." This functionality previously worked with TronWeb version prior to 6.0, but no longer functions with the current version.

start940315 commented 4 months ago

I guess you are using tronweb beta version which is written using typescript. It's a compilation problem. You have to define a proper type for the contract you are going to call. Or you want to save time, just add an // @ts-ignore before the code line.

LACT17 commented 4 months ago

I'm trying to follow the tronWeb documentation (https://tronweb.network/docu/docs/6.0.0-beta.1/Interact%20with%20contract#call) to retrieve the USDT balance from a wallet address. However, I'm encountering an error message: "Property 'balanceOf' does not exist on type 'Contract'." This is the code:


let abi = [
  {
    'outputs': [{ 'type': 'uint256' }],
    'constant': true,
    'inputs': [{ 'name': 'who', 'type': 'address' }],
    'name': 'balanceOf',
    'stateMutability': 'View',
    'type': 'Function'
  },
  {
    'outputs': [{ 'type': 'bool' }],
    'inputs': [
      { 'name': '_to', 'type': 'address' },
      { 'name': '_value', 'type': 'uint256' }
    ],
    'name': 'transfer',
    'stateMutability': 'Nonpayable',
    'type': 'Function'
  }
];
let contract = await tronWeb.contract(abi, 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t');
let result = await contract.balanceOf('TKbfAqmG1UFRiBFSXCtk4PXFgqkaxJ1SSy').call();
console.log(result.toString(10));

Can you help by giving me an example?, please.

start940315 commented 4 months ago

try this:

let abi = [
  {
    'outputs': [{ 'type': 'uint256' }],
    'constant': true,
    'inputs': [{ 'name': 'who', 'type': 'address' }],
    'name': 'balanceOf',
    'stateMutability': 'View',
    'type': 'Function'
  },
  {
    'outputs': [{ 'type': 'bool' }],
    'inputs': [
      { 'name': '_to', 'type': 'address' },
      { 'name': '_value', 'type': 'uint256' }
    ],
    'name': 'transfer',
    'stateMutability': 'Nonpayable',
    'type': 'Function'
  }
];
let contract = await tronWeb.contract(abi, 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t');
// @ts-ignore
let result = await contract.balanceOf('TKbfAqmG1UFRiBFSXCtk4PXFgqkaxJ1SSy').call();
console.log(result.toString(10));
LACT17 commented 4 months ago

I tried with the // @ts-ignore and I get an error of "Property 'balanceOf' does not exist on type 'any[]'.". Can you help me with an example ?, please.

start940315 commented 4 months ago

Please upload this screenshot of the error. So we can locate the problem.