1

I do not know why with this, even though in the previous version (web3 + Metamask) can issue real data. But now used as hashing (output). I took the example in the code and output below (to get the TotalSupply on the ERC20 Token):

const contractInstance = web3.eth.contract(contractAbi).at(contractAddress);
const total_supply = contractInstance.totalSupply.getData();
console.log(total_supply);

Output : 0x18160ddd (this is my problem)

How to showing real data? In a sense it doesn't come out hashing. Thanks

  • In case my answer solved your problem please mark it as an answer. Thanks! – Niklas Feurstein Sep 2 at 20:46
2

In web3.js v0.x:

The expression contractInstance.totalSupply.getData() would get you the encoded ABI data (byte-code) of a call to function totalSupply.

In order to actually call the function and retrieve the return-value asynchronously, you should use:

contractInstance.totalSupply.call().then(total_supply => {
    console.log(total_supply);
});

In web3.js v1.x:

The expression contractInstance.methods.totalSupply().encodeABI() would get you the encoded ABI data (byte-code) of a call to function totalSupply.

In order to actually call the function and retrieve the return-value asynchronously, you should use:

contractInstance.methods.totalSupply().call().then(total_supply => {
    console.log(total_supply);
});
4

The output you receive is in hex format. You need to conver hex to an int.

Here is the full code you need to use:

const contractInstance = web3.eth.contract(contractAbi).at(contractAddress);
const total_supply = parseInt(contractInstance.totalSupply.getData());
console.log(total_supply);

I added parseInt() to convert your hex number into a human readable int.

  • I think that contractInstance.totalSupply.getData() gives the function selector, not the total-supply. I think that await contractInstance.totalSupply.call() gives the total supply (web3.js v0.x). So parseInt is the wrong kind of remedy here. – goodvibration Sep 3 at 0:58

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.