Web3.py meets Eth 2.0

Among a dozen other initiatives, the Ethereum 2.0 teams have been working to standardize how developers can interact with beacon nodes. This effort is ongoing, but the latest specification can be found here.

We're excited to announce that Web3.py now supports the Eth2 beacon node API! This feature is experimental and will continue to evolve as client teams adopt the spec. At the time of writing, Teku and Lighthouse clients are compatible with this API. Remote providers, like INFURA, will also be an option at some point, but are not yet available.

Demo

Let's run through a quick usage example.

1.) You'll need a beacon node running locally or remotely. Reference the instructions for the client of your choice. Mainnet or a testnet is fine; the following examples include Pyrmont testnet data.

2.) Install Web3.py:

$ pip install web3==5.14.0

(The version is specified to ensure these examples work if the API changes.)

3.) Import Web3.py's beacon module into your REPL, script, or app:

>>> from web3.beacon import Beacon

4.) Configure your beacon instance with the URL of the exposed HTTP API port. For example:

>>> beacon = Beacon("http://localhost:5051")

5.) Check the health of your beacon node. A synced and healthy node returns a 200 status code and 206 if the node is syncing, but can still return partial data.

>>> beacon.get_health()
200

6.) Check the sync status of your node:

>>> beacon.get_syncing()
{'data': {'head_slot': '153518', 'sync_distance': '65281'}}

7.) View a validator's balance and status:

>>> beacon.get_validator(79405)
{'data': {'index': '79405', 'balance': '32137722045', 'status': 'active_ongoing', 'validator': {'pubkey': '0xaf1edda2344862a226cccf824d1280e07cbb069cc51d63f0e5e8a9481cdab608757dd4fdd33d918eb267310f950c4d10', 'withdrawal_credentials': '0x00c99595d6dd8ba3f996e87bc6bf1bc710f526f6de6b0d493f83776191686816', 'effective_balance': '32000000000', 'slashed': False, 'activation_eligibility_epoch': '0', 'activation_epoch': '0', 'exit_epoch': '18446744073709551615', 'withdrawable_epoch': '18446744073709551615'}}}

Your turn

There are many more methods to explore; Web3.py documentation is in the works, but the endpoints are available in the spec, the source, or you can use your editor's autocomplete functionality to view what's available.

We're excited to see what doors are opened by this API. Now it's your turn to create specialized block explorers, monitor and alert for events, visualize Eth2 data, create generative art with that data, or whatever else strikes your fancy.

A final reminder: these APIs are still evolving and may change without warning! Feature requests and bug reports are welcome and appreciated in the Web3.py repo.