Package: eth2
eth2Eth2.py: Python API interface to theEth2.0 API.If you are looking for the Eth2 spec, seeeth2speconPyPiandGitHub.FeaturesUses the excellenthttpxlibrary, withsupportforTrioandAsyncIOasync runtimes.Full type annotations, type hints for every API methodCompatible with theeth2specpackage types andremerkleablefor SSZ.Through theeth2specpackage, Beacon types can be configured, to use the API for testnets or other Eth2 variants.Extensive use of Python 3.8Protocoltyping (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.ExampleGetting startedimporttriofrometh2.modelsimportlighthousefrometh2.coreimportContentTypefrometh2.providers.httpimportEth2HttpClient,Eth2HttpOptionsasyncdefstart():# Customize this to use your Beacon endpointaddress='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))asyncwithEth2HttpClient(options=Eth2HttpOptions(api_base_url=address,default_req_type=ContentType.json,default_resp_type=ContentType.ssz))asprov:# The provider can be extended with any API model.awaitfun(prov.extended_api(lighthouse.Eth2API))print("done!")trio.run(start)Using the API modelfrometh2.models.lighthouseimportEth2APIasyncdeffun(api:Eth2API):print("fetching state!")data=awaitapi.beacon.state(slot=spec.Slot(1))print(data.beacon_state.eth1_data)# Much faster than retrieving full state!head=awaitapi.beacon.head()print(f"finalized:{head.finalized_block_root.hex()}{head.finalized_slot}")enr=awaitapi.network.enr()print(enr)# Will error, the slashing is invalid, two equal headers (and no signatures ofc)awaitapi.beacon.post_proposer_slashing(spec.ProposerSlashing())Advanced API callsWhen you need to tweak individual API call settingsfrometh2.coreimportContentType,APIEndpointFnfrometh2.models.lighthouseimportEth2API,APIStateasyncdefadvanced(api:Eth2API):# Make a copy of the callable function, to then change its settingsfn=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=awaitfn(slot=spec.Slot(300))print(data.beacon_state.finalized_checkpoint)Defining custom modelsThe 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.AnyProtocolclass 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 pathsCurrently the Lighthouse API model is well supported, and the new standard-API is being experimented with, but incomplete.Project LinksDocs:https://eth2py.readthedocs.io/Changelog:https://eth2py.readthedocs.io/en/latest/changelog.htmlPyPI:https://pypi.python.org/pypi/eth2Issues:https://github.com/protolambda/eth2.py/issuesContactAuthor:@protolambdaLicenseMIT, seeLICENSEfile.