Eth2 API
Project description
eth2
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.
Project Links
Contact
Author: @protolambda
License
MIT, see LICENSE file.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file eth2-0.0.5.tar.gz
.
File metadata
- Download URL: eth2-0.0.5.tar.gz
- Upload date:
- Size: 14.2 kB
- Tags: Source
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 33f83dbfb6d3ee1138f7f0e20ee3b490cb31fd1649b6f3e782c402465bf5815b |
|
MD5 | 81926430cfe0e1633a15cea73ce8eb13 |
|
BLAKE2b-256 | b6132edd47c505b582a97dc01b9cb3a7800d06fb2fbb70c51305156fb4fba336 |
File details
Details for the file eth2-0.0.5-py3-none-any.whl
.
File metadata
- Download URL: eth2-0.0.5-py3-none-any.whl
- Upload date:
- Size: 13.2 kB
- Tags: Python 3
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 16c58180b20fdfa9178978d9cb90973c17af2da8b1b3c4b9425e4960d536c1d4 |
|
MD5 | 82e215a649fcfae908b439e79ac45f78 |
|
BLAKE2b-256 | 1c089e8efbbe621c0cb459c1439a3c3c38df3498764269c4226c36f691c551f8 |