web3js – How to Read Data from Smart Contract Using web3.js?

contract-invocationweb3js

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.

Best Answer

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.

Related Topic