I want to build something similar to the Read Contract tab feature of etherscan which reads a contract and return all the public props like: - name, totalSupply, decimals, etc.

However I have not found any information on web3 documentation.

up vote 3 down vote accepted

Etherscan is only able to provide that for contracts that verify their source code, or for contracts that follow a specification, such as ERC20.

This is because reading a contract (or writing it) requires that you know the ABI for that contract. If you have the source code, you can generate it. If you don't have the code, but the contract follows a known spec, you can use the ABI for that spec (note that any additional methods implemented outside the spec will not be accessible in that case).

If you have the ABI, it's simply a matter of calling specific functions on the contract. Web3 contract interactions are covered in the documentation here.

  • yes I have the ABI, so you saying that I could do myContract.myProp or myContract.myProp.call() like if I wanna get the tokenSupply prop: myContract.tokenSuply? – Ernane Luis Aug 1 at 12:27
  • 1
    You should be able to do myContract.tokenSupply.call() – Raghav Sood Aug 1 at 12:28
  • Should it be myContract.methods.tokenSupply.call() ? – SSH This Oct 9 at 14:32
  • 1
    @SSHThis Possibly, my web3.js conventions are the best, rarely use it – Raghav Sood Oct 9 at 14:34

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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