TL;DR a slick contract and NPM package for efficient and fast balance checks. Scroll to the bottom to try it out and find the package.

A common task that has plagued wallet providers and block explorers since ERC20 tokens came out is fetching all of the value associated with a single address. With tokens included, you need to both call eth_getBalance , as well as balanceOf for each token. But what if I told you we could bundle those into a single call, and not just for a single address, but as many addresses as we want? And you can do so without having to run custom software, since it works on any Ethereum node? Let me explain.

Credits for this method go to DeltaBalances for the idea & Henry Nguyen for the final implementation

At ETHSanFrancisco this year, myself and a team of 3 others put together a chrome extension that keeps track of your addresses called Safu. One of the key features of the extension was it needed to to check all of the balances of all of your addresses, and fast.

Most applications’ approach for doing this so far has usually been to fire off dozens or hundreds of requests to a node, and fill in the balances as they come back. The more speed conscious developers may have even utilized JSON RPC’s batch spec, seeing as all of the main Ethereum clients support it. But that still wasn’t good enough for Safu, since we wanted to be showing all balances for multiple addresses at once, and we wanted it fast.

An underused trick to do this much faster, and much more efficiently is by using a smart contract. Most people only think of using smart contracts for their ability to store data, and handling the permissions around who can store and remove what. But you can actually make read-only contracts that perform useful functions for you that are completely stateless. So that’s exactly what we’ve got, a smart contract that fetches all of the desired balances for us. This is considerably faster, as we don’t have to waste time with HTTP round trips and clogging up a node’s available connections & threads. It’s all done internally in the EVM in one go. Here’s a comparison of the network requests between the old multi-request way, and the new contract way: