-1

I'm learning ethereum blockchain.

I'm using web3.js with Ganache as a dev node and I want to query the information of a smart contract (i have the address and i want to get the code and the ABI)

I checked the documentation about web3.eth.Contract and found nothing ,

Also knowing that a contract is a type of account (like EOA). I tried using web3.eth.accounts, but didn't find a way to make it work Any suggestion? If not possible via Web3.js please suggest a blockchain explorer for local nodes? Or should I use Truffle?

TylerH
  • 20,799
  • 66
  • 75
  • 101

1 Answers1

1

Contract bytecode and ABI JSON are results of compilation of the source code.

Only the bytecode is stored onchain. So it's not possible to retrieve the source code nor the ABI JSON from just the bytecode.

Petr Hejda
  • 40,554
  • 8
  • 72
  • 100
  • Thank you so much Petr , A question , how etherscan can show the code and ABI ? Are there any tools to do such things ? – Khalfoun Mohamed El Mehdi May 15 '22 at 12:14
  • 1
    @KhalfounMohamedElMehdi Contract authors can upload the source code, that is compiled and validated against the already existing bytecode. If the validation is successful, Etherscan publishes the source code, as well as the ABI JSON resulting from the compilation of the source code. https://etherscan.io/verifyContract – Petr Hejda May 15 '22 at 13:18