Skip to main content
https://raw.githubusercontent.com/protolambda/eth2.py/master/docs/_static/logo.png

eth2

https://img.shields.io/pypi/l/eth2.svg https://img.shields.io/pypi/pyversions/eth2.svg https://img.shields.io/pypi/status/eth2.svg https://img.shields.io/pypi/implementation/eth2.svg https://github.com/protolambda/eth2.py/workflows/Eth2%20API%20Python%20CI/badge.svg

Eth2.py: Python API interface to the Eth2.0 API.

If you are looking for the Eth2 spec, see eth2spec on PyPi and GitHub.

Features

  • Uses the excellent httpx library, with support for Trio and AsyncIO async runtimes.

  • Full type annotations, type hints for every API method

  • Compatible with the eth2spec package types and remerkleable for SSZ.

  • Through the eth2spec package, Beacon types can be configured, to use the API for testnets or other Eth2 variants.

  • Extensive use of Python 3.8 Protocol typing (PEP 544). Testing has your code has never been easier, just mock the object, type-safe!.

  • Easy extension and definition of API routes. The API function signatures and object types are all you need.

Example

Getting started

import trio
from eth2.models import lighthouse
from eth2.core import ContentType
from eth2.providers.http import Eth2HttpClient, Eth2HttpOptions

async def start():
    # Customize this to use your Beacon endpoint
    address = 'http://localhost:4000'
    # Optionally bring in your own HTTP client
    # async with httpx.AsyncClient() as client:
    #     prov = Eth2HttpProvider(client, options=Eth2HttpOptions(api_base_url=address))
    async with Eth2HttpClient(options=Eth2HttpOptions(
            api_base_url=address,
            default_req_type=ContentType.json,
            default_resp_type=ContentType.ssz)) as prov:
        # The provider can be extended with any API model.
        await fun(prov.extended_api(lighthouse.Eth2API))
    print("done!")

trio.run(start)

Using the API model

from eth2.models.lighthouse import Eth2API

async def fun(api: Eth2API):
    print("fetching state!")
    data = await api.beacon.state(slot=spec.Slot(1))
    print(data.beacon_state.eth1_data)

    # Much faster than retrieving full state!
    head = await api.beacon.head()
    print(f"finalized: {head.finalized_block_root.hex()} {head.finalized_slot}")

    enr = await api.network.enr()
    print(enr)

    # Will error, the slashing is invalid, two equal headers (and no signatures ofc)
    await api.beacon.post_proposer_slashing(spec.ProposerSlashing())

Advanced API calls

When you need to tweak individual API call settings

from eth2.core import ContentType, APIEndpointFn
from eth2.models.lighthouse import Eth2API, APIState

async def advanced(api: Eth2API):
    # Make a copy of the callable function, to then change its settings
    fn = APIEndpointFn(api.beacon.state)
    fn.resp_type = ContentType.json  # Instead of default ssz, because why not

    # The most inefficient way to retrieve finalized checkpoint.
    # Full state, as json.
    # But hey, access any data, and process with the spec as you like.
    data: APIState = await fn(slot=spec.Slot(300))
    print(data.beacon_state.finalized_checkpoint)

Defining custom models

The HTTP provider can “learn” how to use a model of routes, on the fly! No need to hardcode any API calls. Just define the model as a Pytho 3.8 Protocol. The Eth2 API provider will shadow this model with an implementation.

  • Any Protocol class with annotations can be interpreted as route model. Fields are sub-routes.

  • api() decorator to make function calls usable endpoints. Customize endpoint options if you need.

  • var_path() decorator to make function calls construct dynamic paths

Currently the Lighthouse API model is well supported, and the new standard-API is being experimented with, but incomplete.

Contact

Author: @protolambda

License

MIT, see LICENSE file.

Metadata

Release files for eth2 0.0.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for eth2 0.0.5
File Size Uploaded
eth2-0.0.5.tar.gz 14.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for eth2 0.0.5
File Interpreter ABI Platform
eth2-0.0.5-py3-none-any.whl Python 3 none any Details

Total release size: 27.5 kB

Release files / eth2-0.0.5.tar.gz

Download URL eth2-0.0.5.tar.gz
Size 14.2 kB
Tags Source
SHA-256 checksum
How to use checksums
33f83dbfb6d3ee1138f7f0e20ee3b490cb31fd1649b6f3e782c402465bf5815b
BLAKE2b-256 checksum
How to use checksums
b6132edd47c505b582a97dc01b9cb3a7800d06fb2fbb70c51305156fb4fba336
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.8.3

Release files / eth2-0.0.5-py3-none-any.whl

Download URL eth2-0.0.5-py3-none-any.whl
Size 13.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
16c58180b20fdfa9178978d9cb90973c17af2da8b1b3c4b9425e4960d536c1d4
BLAKE2b-256 checksum
How to use checksums
1c089e8efbbe621c0cb459c1439a3c3c38df3498764269c4226c36f691c551f8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.8.3

Release history Release notifications | RSS feed

This release

0.0.5 This release

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page