Calling Smart Contracts with `chifra state --call`

The Commands

There are two basic forms of the command. Both are part of the chifra state tool. In particular, both use the chifra state --call option.

Using the Call Option

Here’s a very quick example of using the --call option. There are other ways to do this, of course, in particular our amazing Dynamic Traverser feature, but that requires C++ programming. This runs from a command line.

Install TrueBlocks

Blah, blah, blah. See the many articles we’ve written describing installing TrueBlocks:

Running the Commands

First, we’ll pick an ERC20 token at random:

chifra names ERC20 | head -614 | tail -8 | head -1
Tag             Address         Name       Symbol Decimals
50-Tokens:ERC20 0x2287b...f49bf SoundMoney SDM 18
chifra list 0x2287bd440a11c4585058fdc090455786130f49bf
  1. It visits every chunk of the TrueBlock index and finds every appearance of this address anywhere on the chain,
  2. As it does this, depending on your setup, it downloads and caches (i.e. pins on IPFS) those portions of the index that you’ve expressed interest in. In this way, you become part of the solution to making access to the Ethereum data permissionless.
  3. It caches the list of appearances so the next time you run it, it’s almost instantaneous.
chifra list 0x2287bd440a11c4585058fdc090455786130f49bf --count

Extracting and Cacheing Ethereum Data

The next command extracts the actual transactional data from the chain for the above address:

chifra export 0x2287bd440a11c4585058fdc090455786130f49bf
time chifra export --cache_tx 0x2287b...f49bf

Using the Chifra --call Option

See the above description of the --call option. We’ll need it. First, let’s do this command:

chifra abis 0x2287bd440a11c4585058fdc090455786130f49bf
name          four-byte   signature
totalSupply 0x18160ddd totalSupply()
chifra state --call "0x2287b...f49bf | totalSupply()"
chifra state --call "0x2287b...f49bf | totalSupply()(uint)"
chifra state --call "0x2287b...f49bf | totalSupply() returns (uint)"
bn       addr          signature     encoding    compressedresult
13496062 0x2287...49bf totalSupply() 0x18160ddd 429

All Appearances

We’ll finish now with this series of commands left unexplained. We invite the reader to figure out what’s going on for themselves:

# Store the blocks this address appears in blocks.txt
chifra list 0x2287bd440a11c4585058fdc090455786130f49bf --no_header \
| cut -f2 \
| sort -u -n >blocks.txt
# count how many unique blocks appear (we get 152 blocks)
wc blocks.txt
# Cache the extracted transactions (this is optional, but may
# speed things up

chifra export --cache_txs 0x2287bd440a11c4585058fdc090455786130f49bf
# Using the blocks.txt file call the totalSupply routine
# on the address at every block

chifra state --call \
"0x2287bd440a11c4585058fdc090455786130f49bf|totalSupply()()" \
--file blocks.txt

Support Our Work

TrueBlocks is totally self-funded from our own personal funds and a few grants such as The Etheruem Foundation (2018), Consensys (2019), Moloch DAO (2021), and most recently Filecoin/IPFS (2021).



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Thomas Jay Rush

Thomas Jay Rush


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