0

I want to access the balance of contract(i.e. 0x552F3AfaA0394632f4aEfa9E923fA3e2bbDAF5FE) and its totalSupply(state variable). I could access the balancec of this contract , but why I could not call its function i.e. getTotalSupply() I have following code;

async function myContractAddress() {
    myContAddr = '0x552F3AfaA0394632f4aEfa9E923fA3e2bbDAF5FE';
    web3.eth.getBalance(myContAddr).then(console.log);
    tokens = await myContAddr.methods.getTotalSupply().call();
    tokens = tokens.toString(10);
    console.log("Toten supply is : ",tokens);

  }
  myContractAddress();

here is my error;

(node:6028) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'getTotalSupply' of undefined
    at myContractAddress (C:\Users\jj\Desktop\temp\deploy.js:36:39)
    at C:\Users\jj\Desktop\temp\deploy.js:26:1
    at process._tickCallback (internal/process/next_tick.js:68:7)
(node:6028) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This
error originated either by throwing inside of an async function without a catch
block, or by rejecting a promise which was not handled with .catch(). (rejectio
 id: 1)
(node:6028) [DEP0018] DeprecationWarning: Unhandled promise rejections are depr
cated. In the future, promise rejections that are not handled will terminate th
 Node.js process with a non-zero exit code.
0 // actually this is my contract balance , which have shown
0

What you are doing wrong is in your code myContAddr is string and you are trying access parameters of this variable. To access .methods.getTotalSupply().call() you need to create contract instance using the following code:

async function myContractAddress() {
    var myContAddr = '0x552F3AfaA0394632f4aEfa9E923fA3e2bbDAF5FE';
    var myContractAbiDefenition = 'CONTRACT_ABI';
    var myContractInstance = new web3.eth.Contract(myContractAbiDefenition, myContAddr);
    tokens = await myContractInstance.methods.getTotalSupply().call();
    console.log("Tokens: ",tokens);
}
myContractAddress();
  • but above address is already deployed on ethereum testnet, then why one function considered it OK (as a contract and gives balance) while other considered it not OK and gives error.... – Amir Ali Nov 12 at 5:32
  • thanks it worked for me .. – Amir Ali Nov 12 at 6:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.