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/v2/api?chainid=1&apikey=[YOUR-ETHERSCAN-V2-API-KEY]&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 on Replit.

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.

Changelog

See Releases.

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.15.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

web3_input_decoder-0.1.15-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: web3_input_decoder-0.1.15.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.12.3 Linux/6.11.0-1018-azure

File hashes

Hashes for web3_input_decoder-0.1.15.tar.gz
Algorithm Hash digest
SHA256 d4928c2cc0812256fc75739ddad9395516b816e38e6a42a357ec381c2b118ecf
MD5 57611a150ac0e33ca899e83f03e7bb4a
BLAKE2b-256 7ed0dc536d6877d40e0721be0269f6dacb8a95b4f681bc62c52a9330aff62bfb

See more details on using hashes here.

File details

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

File metadata

  • Download URL: web3_input_decoder-0.1.15-py3-none-any.whl
  • Upload date:
  • Size: 8.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.12.3 Linux/6.11.0-1018-azure

File hashes

Hashes for web3_input_decoder-0.1.15-py3-none-any.whl
Algorithm Hash digest
SHA256 23ad7a304a6d49c02155a389bc3f1ef8329cd462b1be6a61119673eaba21cd6f
MD5 f1433852bcb665687921954f227d2baa
BLAKE2b-256 d395467f807ae860d3b2a67944fb763a6949b606ede4cb7318cba6e828b1cb34

See more details on using hashes here.

Supported by

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