3

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.

1 Answer 1

5

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.

4
  • 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? Aug 1, 2018 at 12:27
  • 1
    You should be able to do myContract.tokenSupply.call() Aug 1, 2018 at 12:28
  • Should it be myContract.methods.tokenSupply.call() ?
    – SSH This
    Oct 9, 2018 at 14:32
  • 1
    @SSHThis Possibly, my web3.js conventions are the best, rarely use it Oct 9, 2018 at 14:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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