Skip to main content

A simple offline web3 transaction input decoder for functions and constructors

Project description

web3-input-decoder

Codacy Badge CI Codecov PyPI - Python Version PyPI License

A simple offline web3 transaction input decoder for functions and constructors.

Install

pip install web3-input-decoder

Quick start

Let's take a USDT transfer transaction and the USDT contract creator transaction as an example:

>>> import json
>>> import urllib.request
>>> from web3_input_decoder import decode_constructor, decode_function
>>> f = urllib.request.urlopen("https://api.etherscan.io/api?module=contract&action=getabi&address=0xdac17f958d2ee523a2206206994597c13d831ec7")
>>> TETHER_ABI = json.loads(json.load(f)["result"])
>>> decode_function(
        TETHER_ABI, "0xa9059cbb000000000000000000000000f050227be1a7ce587aa83d5013f900dbc3be0611000000000000000000000000000000000000000000000000000000000ecdd350",
    )
[('address', '_to', '0xf050227be1a7ce587aa83d5013f900dbc3be0611'),
 ('uint256', '_value', 248370000)]
>>> decode_constructor(
        TETHER_ABI, "000000000000000000000000000000000000000000000000000000174876e800000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a546574686572205553440000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000045553445400000000000000000000000000000000000000000000000000000000"
    )
[('uint256', '_initialSupply', 100000000000),
 ('string', '_name', 'Tether USD'),
 ('string', '_symbol', 'USDT'),
 ('uint256', '_decimals', 6)]

You can also play with it here.

Performance enhancement

If you have lots of inputs in the same contract to decode, consider using InputDecoder.

>>> from web3_input_decoder import InputDecoder
>>> decoder = InputDecoder(TETHER_ABI)
>>> for _ in range(10000):
>>>    decoder.decode_function(
          (
            "0xa9059cbb000000000000000000000000f050227be1a7ce587aa83d5013f900dbc3b"
            "e0611000000000000000000000000000000000000000000000000000000000ecdd350"
          ),
        )

API

  • decode_constructor

    def decode_constructor(
        abi: List[dict],
        tx_input: Union[str, bytes],
        bytecode: Optional[Union[str, bytes]] = None,
    ) -> List[Tuple[str, str, Any]]
    

    Parameters:

    • abi: Contract ABI
    • tx_input: Transaction input to decode, with or without deployed contract bytecode
    • bytecode: Optional deployed contract bytecode. If this is set, tx_input should include bytecode

    Returns:

    • List[Tuple[str, str, Any]]: Decoded type-name-value tuples
  • decode_function

    def decode_function(
        abi: List[dict], tx_input: Union[str, bytes]
    ) -> List[Tuple[str, str, Any]]
    

    Parameters:

    • abi: Contract ABI
    • tx_input: Transaction input to decode

    Returns:

    • List[Tuple[str, str, Any]]: Decoded type-name-value tuples

Rationale

Existing solutions are not satisfying to me, e.g.:

  1. web3py can only decode function calls and it's necessary to be online to set up a provider first.
  2. ethereum-input-decoder is not maintained and it contains several glitches.

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

web3_input_decoder-0.1.13.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

web3_input_decoder-0.1.13-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file web3_input_decoder-0.1.13.tar.gz.

File metadata

  • Download URL: web3_input_decoder-0.1.13.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.10.12 Linux/6.5.0-1025-azure

File hashes

Hashes for web3_input_decoder-0.1.13.tar.gz
Algorithm Hash digest
SHA256 5859f18c582601b966c1aacf6381d5f2edf259b1f6e2c72495be068c494f7165
MD5 e2cdad990b6ee2128d97b8b87751f609
BLAKE2b-256 55e3385a315be2f9db2843014359610865f1e5814a2b62a6c1424d9bc8dfb09f

See more details on using hashes here.

File details

Details for the file web3_input_decoder-0.1.13-py3-none-any.whl.

File metadata

File hashes

Hashes for web3_input_decoder-0.1.13-py3-none-any.whl
Algorithm Hash digest
SHA256 40753678b14d29f3eb58d902318cff5db9299aaa481e5f9f9d177bed1ed04e85
MD5 9c351428b715cf9257f13ec96fbb0eb2
BLAKE2b-256 22b6e5fc30267a4001b915665604bf854170a2e68027c51ca1376157500eec6f

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