Skip to main content

eth-pydantic-types: Pydantic Types for Ethereum

Project description

eth-pydantic-types

The types in this package are pydantic types for Ethereum inspired from eth-typing.

Hash32

Hash32 is a good type to use for Ethereum transaction hashes. When using the Hash32 type, your inputs will be validated by length and your schema will declare the input a string with a binary format. Use Hash32 like this:

from pydantic import BaseModel

from eth_pydantic_types import Hash32

class Transaction(BaseModel):
    tx_hash: Hash32


# NOTE: I am able to pass an int-hash as the value and it will
#  get validated and type-coerced.
tx = Transaction(
    tx_hash=0x1031f0c9ac54dcb64b4f121a27957c14263c5cb49ed316d568e41e19c34d7b28
)

HexBytes

A thin-wrapper around an already thin-wrapper hexbytes.HexBytes. The difference here is that this HexBytes properly serializes. Use HexBytes any place where you would actually use hexbytes.HexBytes.

from pydantic import BaseModel
from eth_pydantic_types import HexBytes

class MyStorage(BaseModel):
    cid: HexBytes

# NOTE: We are able to pass a hex-str for a HexBytes value.
storage = MyStorage(cid="0x123")

Address

Use the Address class for working with checksummed-addresses. Addresses get validated and checksummed in model construction.

from pydantic import BaseModel
from eth_pydantic_types import Address

class Account(BaseModel):
    address: Address

# NOTE: The address ends up checksummed
#   ("0x0837207e343277CBd6c114a45EC0e9Ec56a1AD84")
account = Account(address="0x837207e343277cbd6c114a45ec0e9ec56a1ad84")

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

eth-pydantic-types-0.1.0a1.tar.gz (61.2 kB view hashes)

Uploaded Source

Built Distribution

eth_pydantic_types-0.1.0a1-py3-none-any.whl (10.8 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page