Skip to main content

Ethereum ABI Utils

Project description

Ethereum Contract
=================

|Build Status| |Documentation Status| |PyPi version|

Python utilities for working with the Ethereum ABI

Installation
------------

.. code:: sh

pip install eth-abi

Documentation
-------------

Decoding
~~~~~~~~

These functions are intended for decoding return values from the EVM.

- ``eth_abi.decode_single(type, data)``

This function tries to decode ``data`` into the python type that
corresponds to the provided ``type``. This function accepts data of type
bytes.

.. code-block:: python

::

>>> decode_single('uint256', b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0009')
12345

The **value** parameter is expected to be one of the recognized EVM
types.

.. note:: This function cannot be used to decode dynamic or array types
such as ``bytes32[]``.

- ``eth_abi.decode_abi(types, data)``

This function decodes ``data`` into the python type corresponding to the
provided ``types``. This function accepts arrays of type byte.

.. code-block:: python

::

>>> decode_abi(['uint256'], b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0009')
[12345]
>>> decode_abi(['bytes32', 'bytes32'], b'a\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00b\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
['a', 'b']

The **values** parameter is expected to be an iterable whose values are
all one of the recognized EVM types.

Encoding
~~~~~~~~

These functions are intended for encoding python values into
representations that are suitable for interacting with the EVM.

- ``eth_abi.encode_single(type, value)``

This function encodes ``value`` in the ABI encoding for the provided
``type``.

.. code-block:: python

::

>>> encode_single('uint256', 12345)
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0009'

The **value** parameter is expected to be one of the recognized EVM
types.

.. note:: This function cannot be used to encode array types such as
``bytes32[]``.

- ``eth_abi.encode_abi(types, values)``

This function encodes ``values`` in the ABI encoding for the
corresponding type provided by the ``types`` argument.

.. code-block:: python

::

>>> encode_abi(['uint256'], [12345])
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0009'
>>> encode_abi(['bytes32', 'bytes32'], ['a', 'b'])
b'a\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00b\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

The **values** parameter is expected to be an iterable whose values are
all one of the recognized EVM types.

Development
-----------

Clone the repository and then run:

.. code:: sh

pip install -e . -r requirements-dev.txt

Running the tests
~~~~~~~~~~~~~~~~~

You can run the tests with:

.. code:: sh

py.test tests

Or you can install ``tox`` to run the full test suite.

Releasing
~~~~~~~~~

Pandoc is required for transforming the markdown README to the proper
format to render correctly on pypi.

For Debian-like systems:

::

apt install pandoc

Or on OSX:

.. code:: sh

brew install pandoc

To release a new version:

.. code:: sh

make release bump=$$VERSION_PART_TO_BUMP$$

How to bumpversion
^^^^^^^^^^^^^^^^^^

The version format for this repo is ``{major}.{minor}.{patch}`` for
stable, and ``{major}.{minor}.{patch}-{stage}.{devnum}`` for unstable
(``stage`` can be alpha or beta).

To issue the next version in line, specify which part to bump, like
``make release bump=minor`` or ``make release bump=devnum``.

If you are in a beta version, ``make release bump=stage`` will switch to
a stable.

To issue an unstable version when the current version is stable, specify
the new version explicitly, like
``make release bump="--new-version 4.0.0-alpha.1 devnum"``

.. |Build Status| image:: https://circleci.com/gh/ethereum/eth-abi.svg?style=shield
:target: https://circleci.com/gh/ethereum/eth-abi
.. |Documentation Status| image:: https://readthedocs.org/projects/eth-abi/badge/?version=latest
:target: https://readthedocs.org/projects/eth-abi/?badge=latest
.. |PyPi version| image:: https://img.shields.io/pypi/v/eth-abi.svg
:target: https://pypi-hypernode.com/pypi/eth-abi

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-abi-1.1.0.tar.gz (18.9 kB view details)

Uploaded Source

Built Distribution

eth_abi-1.1.0-py3-none-any.whl (22.6 kB view details)

Uploaded Python 3

File details

Details for the file eth-abi-1.1.0.tar.gz.

File metadata

  • Download URL: eth-abi-1.1.0.tar.gz
  • Upload date:
  • Size: 18.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for eth-abi-1.1.0.tar.gz
Algorithm Hash digest
SHA256 911065bcb2f760d66060eb2d113c5eecee38498fbdad1bcca174879c458a2cbf
MD5 0126491f6d4ab4ff4c783cfccf7558c9
BLAKE2b-256 02b1b050bf36c45225925b0dcea134258abfb4c221003387767cc77cd6723740

See more details on using hashes here.

File details

Details for the file eth_abi-1.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for eth_abi-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bae86efecd9729038d36aecdad951f5d7957fda5a208b770dab8cd2820329ac4
MD5 2d0893ba88c0738d1897e22dd44a407d
BLAKE2b-256 00523029d0e4af3669a9a379a64cab3571ba2833ffc5c8f331744e8a1ee7e9ee

See more details on using hashes here.

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