Skip to main content

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.

Metadata

Release files for web3-input-decoder 0.1.15

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for web3-input-decoder 0.1.15
File Size Uploaded
web3_input_decoder-0.1.15.tar.gz 6.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for web3-input-decoder 0.1.15
File Interpreter ABI Platform
web3_input_decoder-0.1.15-py3-none-any.whl Python 3 none any Details

Total release size: 14.2 kB

Release files / web3_input_decoder-0.1.15.tar.gz

Download URL web3_input_decoder-0.1.15.tar.gz
Size 6.0 kB
Tags Source
SHA-256 checksum
How to use checksums
d4928c2cc0812256fc75739ddad9395516b816e38e6a42a357ec381c2b118ecf
BLAKE2b-256 checksum
How to use checksums
7ed0dc536d6877d40e0721be0269f6dacb8a95b4f681bc62c52a9330aff62bfb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.2.1 CPython/3.12.3 Linux/6.11.0-1018-azure

Release files / web3_input_decoder-0.1.15-py3-none-any.whl

Download URL web3_input_decoder-0.1.15-py3-none-any.whl
Size 8.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
23ad7a304a6d49c02155a389bc3f1ef8329cd462b1be6a61119673eaba21cd6f
BLAKE2b-256 checksum
How to use checksums
d395467f807ae860d3b2a67944fb763a6949b606ede4cb7318cba6e828b1cb34
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.2.1 CPython/3.12.3 Linux/6.11.0-1018-azure

Release history Release notifications | RSS feed

This release

0.1.15 This release

2 release files

0.1.13

2 release files

0.1.12

2 release files

0.1.11

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page