Recipe: Factories

Thomas Jay Rush
4 min readJan 6, 2023

--

Getting a list of all contracts created by an address

In this simple and short article, we show you how to use TrueBlocks, the Unchained Index, and chifra to get a list of every smart contract created by an address (including smart contracts that create other smart contracts, that is, factories).

We’ll dive in without fanfare. You can find installation instructions for TrueBlocks on our website (or here for the docker install).

Getting contract creations from an EOA

Let’s start by finding all smart contracts created by our own address, trueblocks.eth. Run the following command.

chifra export --traces --factory trueblocks.eth

Note that we had to add the — trace option since only through traces can we find every smart contract creation. Looking at receipts is not enough, as there’s only room for one created address on a receipt. Note also that chifra supports ENS.

The above command finds 22 smart contract creations by this address, but — my goodness — what took so long? Let’s do some performance testing.

Improving Performance Through Local Caching

First, let’s clean the cache of this address and all its artifacts. This will make the testing fair:

chifra monitors --decache trueblocks.eth

The above command removes the existing monitor (if it exists) and any blocks, transactions, traces, or reconciliations in this address’s transactional history.

Now that we’ve cleaned the cache, we’re ready to test. Run this command:

time chifra export --traces --factory trueblocks.eth --cache

This command takes 656.74 seconds for 2,886 transactions. That’s 10 minutes and 57 seconds. 4.39 transactions per second. That’s pretty slow.

Why is it so slow? Because Erigon has to replay the transactions in order to deliver trace data. That’s one of the tradeoffs they made to ensure a smaller disc footprint. It’s worth it, but can we do better?

Yes.

Notice that I included --cache in the above command. This causes any queried data from the node to be cached locally in TrueBlocks’ cache. It does not speed up the current query, but it has a huge effect on the speed of subsequent queries (as is true of caches).

This becomes obvious if we re-run the command. Let’s do that:

time chifra export --traces --factory trueblocks.eth --cache

This took 3.01 seconds. 958.8 transactions per second. That’s 218.40 times faster than the non-cached run.

The upshot? If you’re doing repeated analysis on an address, use the --cache option. You may use it repeatedly, even if the item is already in the cache. We only write the cache the first time data is queried. Immutable data doesn’t change. There’s never a reason to rewrite it.

Now that we have caching under our belt, let’s run the --factory extraction against an interesting address. How about UniSwap?

How Many Contracts Has Uniswap Created?

First, we need to find Uniswap’s address. Perhaps the names command will help. Let’s try:

chifra names uniswap.eth

This returns 0x1a9c8182c09f50c8318d769245bea52c32be35bc, but is this correct? Not necessarily. Let’s double-check:

chifra explore uniswap.eth

The explore command opens the configured explorer to an address (or a block number, a block hash, a transaction id, a transaction hash, or an address).

The command opens EtherScan (the configure explorer) to address: https://etherscan.io/address/0x1a9c8182c09f50c8318d769245bea52c32be35bc. Looking at the page, though, we can see it’s not what we’re looking for.

What we’re looking we find here through a regular Google search: 0x1F98431c8aD98523631AE4a59f267346ea31F984.

So, to see every smart contract created by V.3 of Uniswap, do this:

chifra export 0x1F98431c8aD98523631AE4a59f267346ea31F984 --traces --factory

Be warned, though. This takes a very long time the first time you run it. Perhaps we can make that a bit more palatable.

When?

Maybe we only want contracts created by UniSwap during the month of December 2022. We need some block numbers:

chifra when 2022-12-01 2023-01-01

returns

blockNumber   timestamp    date
------------------------------------------------------
16086233 1669852799 2022-11-30 23:59:59 UTC
16308189 1672531199 2022-12-31 23:59:59 UTC

These are the first (minus one) block numbers in the month of December and the last block number in the month of December 2022. chifra when returns the block number most recent to the given date and time (and visa-versa).

Adjusting the above command to show only contracts created during December 2022, we get:

chifra export \
--first_block 16086234 --last_block 16308189 \
--traces --factory --no_header --cache \
0x1F98431c8aD98523631AE4a59f267346ea31F984 | tee results

Using — cache speeds things up the next time we look at this month. — no_header does not generate the header rows. Notice also, we’ve redirected the output into a file called results.

We can now post-process this file to extract the block numbers when the contracts were created.

cat results | cut -f1 | tee blocks-only

And then use that file with this command

cat results | xargs chifra when

and use that result

blockNumber   timestamp    date
16086316 1669853807 2022-12-01 00:16:47 UTC
16087682 1669870259 2022-12-01 04:50:59 UTC
16087724 1669870775 2022-12-01 04:59:35 UTC
16088330 1669878083 2022-12-01 07:01:23 UTC
16088458 1669879631 2022-12-01 07:27:11 UTC
...

to generate this (admittedly kind of boring) chart in MS Excel.

Not the greatest chart I’ve ever seen, but kind of cool.

Conclusion

I wanted to share a recipe for finding contracts created by a factory contract, so that’s what I did. Have fun.

Support Our Work

TrueBlocks is funded from our own personal funds and grants from The Ethereum Foundation (2018), Consensys (2019), Moloch DAO (2021), Filecoin/IPFS (2021), our GitCoin donors, and, of course, The Ethereum Foundation (2022).

If you like this article and wish to support our work, please donate to our GitCoin grant https://gitcoin.co/grants/184/trueblocks. Even small amounts have a big impact.

If you’d rather, feel free to send ETH or any other token to us directly at trueblocks.eth or 0xf503017d7baf7fbc0fff7492b751025c6a78179b.

--

--

Thomas Jay Rush

Blockchain Enthusiast, Founder TrueBlocks, LLC and Philadelphia Ethereum Meetup, MS Computer Science UPenn