Tutorial: Query Your Ethereum 2.0 Validator Balance With the Infura API

How to query validator balances programmatically with the Infura Eth 2.0 Beacon Chain API.

Consensys
ConsenSys Media
3 min readJun 12, 2020

--

Every day is bringing us closer to the genesis of Ethereum 2.0 Phase 0. Major multi-client testnet efforts are well underway with the current witti testnet and the upcoming altona testnet. Interest and participation in staking on these testnets continues to rise, with over 65% of all respondents planning to stake their ETH, according to a new Ethereum 2.0 Report.

One of the most basic needs for those staking on ETH 2.0 will be monitoring the balance of their validators. It is crucial to know if they are accruing the rewards that are expected. Of course there are several great block explorers available where you can see this information, but in this post, we will show how you can query it programmatically with the Infura ETH 2.0 Beacon Chain API.

The ability to query your balance programmatically is important because it enables you to easily take action on the data. You might want to write a program that monitors the balance and sends you a text message if it unexpectedly drops!

Let’s take a look how to accomplish this in a simple typescript program.

First we will import the axios library and create an HTTP client with some basic defaults, loading our Infura project ID from an environment variable:

We define some helpful constants related to the basic parameters of the network. The first two will help us calculate the current epoch, and the last helps us convert a gwei balance to the more familiar eth unit:

Then we will query the genesis for the network, and use it to calculate the current epoch:

Using that, we can then query for the current balance of a validator (here using the public key of the highest balance validator on the network at time of writing):

When we run the full program, we will see an output like:

Now we can expand the program to do something useful with the information!

For convenience, here is the full program in one snippet:

In preparation for the launch of Ethereum 2.0, we’ve planned a limited time private beta release of Infura’s Eth2 Beacon Chain API. If you would like access, apply now and register your interest. Documentation will be sent out directly to users who are accepted into the beta.

We greatly value all our testers. It’s your feedback that will help us refine our Eth2 Beacon Chain API, ensuring that the final product is as relevant and performant as possible, before making it more widely available.

Get Started With Infura

Start using Infura’s Ethereum API to develop on Web 3.0.

Originally published at https://blog.infura.io on June 12, 2020.

--

--

Consensys
ConsenSys Media

A complete suite of products to create and participate in web3.