10

I know one way to interact with contracts is, in the geth interface, to use the command: eth.contract(ABI).at(Address)

But for this I will need to know the ABI and address of the contract in advance. What if I want to browse the contracts currently in the blockchain (like browsing the apps in play-store)?

5

EDIT: This answer explains why you cannot list and view the source code of all contracts. The answer by @Xeenych is how to count the total number of contracts.

Some blockchain explorers show some contracts: example with ABI and source code, and example without source (filter out the "value transfers").

The blockchain only has EVM bytecode. Generally, you need the source code of the contract to get the ABI, or the contract author has to provide it to you. See Do I need a compiled contract just to get the ABI definition?

  • 3
    Thanks for answering. But don't you think that it is not only a useful but necessary feature. Is ethereum ever gonna have this feature, or this feature is not possible? Sorry if its a stupid question, I am new to the block-chain technology. – Sukhmaninder Jun 30 '16 at 12:00
  • This "feature" is available in Ethereum: if you write a contract, you can upload its source code to the "app store" you want to have it displayed, similar to what you have to do with existing "app stores". You can also upload your source code to Ethereum, but something like Swarm is better for storing extra data. – eth Jun 30 '16 at 19:32
  • Thanks for the useful links! Is a full/initial blockchain sync a way to track all contract creations? – mikezter Apr 25 '18 at 23:40
5

There are two types of contracts:

  1. Contracts, created by ordinary addresses
  2. Contracts, created by othe contracts

And there are three possible ways to find type 2 contracts:

  1. Check every block and every transaction in a block. Take “to” field of a transaction. Get code at address “to”. If code is not empty, than it is a contract. Add it to the database if not exists. Field “from” alway should contain ordinary address. (It is interesting to check if. If it is not, than someone managed to get a private key of a contract). Thus we will find only rhose type 2 contracts, that had any transactions to from ordinary addresses.
  2. Every transaction should be traced via EVM (Ethereum Virtaul Machine) and checked for contract creation. To make this you need to build a specially patched geth or parity node.
  3. Direct reading of node’s database. Database format is specified in geth’s or parity’s source code. It is LevelDB or RocksDB. I’ve failed in this at first. Somehow my db got corrupted and it took several days to rebuild it again. But the work in this direction continues.

For the present time (block ~ 5250000) there are ~2350000 contracts in ethereum blockchain. My search program worked for about a week.

  • 1
    Have you tried callTracer github.com/ethereum/go-ethereum/pull/15516? It allows retrieving all internal contract creations. The advantage is that you don't need to patch geth nor read the db directly. Not sure how fast it is though, I guess with IPC subscription as described in the PR it should be reasonably fast. – medvedev1088 Mar 19 '18 at 9:16
  • As far as I know debug_traceTransaction API call requires a full geth node. Never tried it. Syncing full node requires too much time. – Xeenych Xeenych Mar 20 '18 at 13:23
  • This is the actual answer. – mikezter Apr 25 '18 at 23:37
  • @mikezter Agree, but mainly for the use case of counting the number of contracts. The question asked about "browse the contracts" which suggested they wanted to see more about the contracts. – eth Jul 27 at 7:56

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.