web3-input-decoder


Nameweb3-input-decoder JSON
Version 0.1.11 PyPI version JSON
download
home_pagehttps://github.com/kigawas/web3-input-decoder
SummaryA simple offline web3 transaction input decoder for functions and constructors
upload_time2024-03-25 16:52:17
maintainerWeiliang Li
docs_urlNone
authorWeiliang Li
requires_python<4.0,>=3.8
licenseMIT
keywords ethereum web3
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage
            # web3-input-decoder

[![Codacy Badge](https://app.codacy.com/project/badge/Grade/6f10d5104ef4464797ee94b17c7b9371)](https://www.codacy.com/gh/kigawas/web3-input-decoder/dashboard)
[![CI](https://img.shields.io/github/actions/workflow/status/kigawas/web3-input-decoder/ci.yml)](https://github.com/kigawas/web3-input-decoder/actions)
[![Codecov](https://img.shields.io/codecov/c/github/kigawas/web3-input-decoder.svg)](https://codecov.io/gh/kigawas/web3-input-decoder)
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/web3-input-decoder.svg)](https://pypi.org/project/web3-input-decoder/)
[![PyPI](https://img.shields.io/pypi/v/web3-input-decoder.svg)](https://pypi.org/project/web3-input-decoder/)
[![License](https://img.shields.io/github/license/kigawas/web3-input-decoder.svg)](https://github.com/kigawas/web3-input-decoder)

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

## Install

```bash
pip install web3-input-decoder
```

## Quick start

Let's take a [USDT transfer transaction](https://etherscan.io/tx/0x0331fdfa070ee26b1fc7b01b246ef5e58593cbe9f4a02f7f09bf4a2aa640cf35) and the [USDT contract creator transaction](https://etherscan.io/address/0xdac17f958d2ee523a2206206994597c13d831ec7#code) as an example:

```python
>>> 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](https://replit.com/@kigawas/Web3-input-decoder-quick-start).

### Performance enhancement

If you have lots of inputs in the same contract to decode, consider using [`InputDecoder`](web3_input_decoder/decoder.py#L26).

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

## API

- [`decode_constructor`](web3_input_decoder/__init__.py#L12)

  ```python
  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`](web3_input_decoder/__init__.py#L37)

  ```python
  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](https://web3py.readthedocs.io/en/stable/contracts.html#web3.contract.Contract.decode_function_input) can only decode function calls and it's necessary to be online to set up a provider first.
2. [ethereum-input-decoder](https://github.com/tintinweb/ethereum-input-decoder) is not actively maintained and it contains several glitches.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/kigawas/web3-input-decoder",
    "name": "web3-input-decoder",
    "maintainer": "Weiliang Li",
    "docs_url": null,
    "requires_python": "<4.0,>=3.8",
    "maintainer_email": "to.be.impressive@gmail.com",
    "keywords": "ethereum, web3",
    "author": "Weiliang Li",
    "author_email": "to.be.impressive@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/95/f9/c4a04c1bd8ccbb702172735047f9471c2383f5e53f330f7d4023cd6b0ee4/web3_input_decoder-0.1.11.tar.gz",
    "platform": null,
    "description": "# web3-input-decoder\n\n[![Codacy Badge](https://app.codacy.com/project/badge/Grade/6f10d5104ef4464797ee94b17c7b9371)](https://www.codacy.com/gh/kigawas/web3-input-decoder/dashboard)\n[![CI](https://img.shields.io/github/actions/workflow/status/kigawas/web3-input-decoder/ci.yml)](https://github.com/kigawas/web3-input-decoder/actions)\n[![Codecov](https://img.shields.io/codecov/c/github/kigawas/web3-input-decoder.svg)](https://codecov.io/gh/kigawas/web3-input-decoder)\n[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/web3-input-decoder.svg)](https://pypi.org/project/web3-input-decoder/)\n[![PyPI](https://img.shields.io/pypi/v/web3-input-decoder.svg)](https://pypi.org/project/web3-input-decoder/)\n[![License](https://img.shields.io/github/license/kigawas/web3-input-decoder.svg)](https://github.com/kigawas/web3-input-decoder)\n\nA simple offline web3 transaction input decoder for functions and constructors.\n\n## Install\n\n```bash\npip install web3-input-decoder\n```\n\n## Quick start\n\nLet's take a [USDT transfer transaction](https://etherscan.io/tx/0x0331fdfa070ee26b1fc7b01b246ef5e58593cbe9f4a02f7f09bf4a2aa640cf35) and the [USDT contract creator transaction](https://etherscan.io/address/0xdac17f958d2ee523a2206206994597c13d831ec7#code) as an example:\n\n```python\n>>> import json\n>>> import urllib.request\n>>> from web3_input_decoder import decode_constructor, decode_function\n>>> f = urllib.request.urlopen(\"https://api.etherscan.io/api?module=contract&action=getabi&address=0xdac17f958d2ee523a2206206994597c13d831ec7\")\n>>> TETHER_ABI = json.loads(json.load(f)[\"result\"])\n>>> decode_function(\n        TETHER_ABI, \"0xa9059cbb000000000000000000000000f050227be1a7ce587aa83d5013f900dbc3be0611000000000000000000000000000000000000000000000000000000000ecdd350\",\n    )\n[('address', '_to', '0xf050227be1a7ce587aa83d5013f900dbc3be0611'),\n ('uint256', '_value', 248370000)]\n>>> decode_constructor(\n        TETHER_ABI, \"000000000000000000000000000000000000000000000000000000174876e800000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a546574686572205553440000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000045553445400000000000000000000000000000000000000000000000000000000\"\n    )\n[('uint256', '_initialSupply', 100000000000),\n ('string', '_name', 'Tether USD'),\n ('string', '_symbol', 'USDT'),\n ('uint256', '_decimals', 6)]\n```\n\nYou can also play with it [here](https://replit.com/@kigawas/Web3-input-decoder-quick-start).\n\n### Performance enhancement\n\nIf you have lots of inputs in the same contract to decode, consider using [`InputDecoder`](web3_input_decoder/decoder.py#L26).\n\n```python\n>>> from web3_input_decoder import InputDecoder\n>>> decoder = InputDecoder(TETHER_ABI)\n>>> for _ in range(10000):\n>>>    decoder.decode_function(\n          (\n            \"0xa9059cbb000000000000000000000000f050227be1a7ce587aa83d5013f900dbc3b\"\n            \"e0611000000000000000000000000000000000000000000000000000000000ecdd350\"\n          ),\n        )\n```\n\n## API\n\n- [`decode_constructor`](web3_input_decoder/__init__.py#L12)\n\n  ```python\n  def decode_constructor(\n      abi: List[dict],\n      tx_input: Union[str, bytes],\n      bytecode: Optional[Union[str, bytes]] = None,\n  ) -> List[Tuple[str, str, Any]]\n  ```\n\n  **Parameters**:\n\n  - `abi`: Contract ABI\n  - `tx_input`: Transaction input to decode, with or without deployed contract bytecode\n  - `bytecode`: Optional deployed contract bytecode. If this is set, `tx_input` should include bytecode\n\n  **Returns**:\n\n  - `List[Tuple[str, str, Any]]`: Decoded type-name-value tuples\n\n- [`decode_function`](web3_input_decoder/__init__.py#L37)\n\n  ```python\n  def decode_function(\n      abi: List[dict], tx_input: Union[str, bytes]\n  ) -> List[Tuple[str, str, Any]]\n  ```\n\n  **Parameters**:\n\n  - `abi`: Contract ABI\n  - `tx_input`: Transaction input to decode\n\n  **Returns**:\n\n  - `List[Tuple[str, str, Any]]`: Decoded type-name-value tuples\n\n## Rationale\n\nExisting solutions are not satisfying to me, e.g.:\n\n1. [web3py](https://web3py.readthedocs.io/en/stable/contracts.html#web3.contract.Contract.decode_function_input) can only decode function calls and it's necessary to be online to set up a provider first.\n2. [ethereum-input-decoder](https://github.com/tintinweb/ethereum-input-decoder) is not actively maintained and it contains several glitches.\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A simple offline web3 transaction input decoder for functions and constructors",
    "version": "0.1.11",
    "project_urls": {
        "Homepage": "https://github.com/kigawas/web3-input-decoder",
        "Repository": "https://github.com/kigawas/web3-input-decoder"
    },
    "split_keywords": [
        "ethereum",
        " web3"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "74fb3fbdfc89a17f69e9134c8c1e225268804370ac93b84226463e3faff98292",
                "md5": "d34bb6d05cc493ff67eab965ca33f580",
                "sha256": "d57266b61dc5a2cb9e6a7d973dab6c5a111a7e4259cb35afe7cd222cb7cbbd18"
            },
            "downloads": -1,
            "filename": "web3_input_decoder-0.1.11-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "d34bb6d05cc493ff67eab965ca33f580",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.8",
            "size": 7856,
            "upload_time": "2024-03-25T16:52:16",
            "upload_time_iso_8601": "2024-03-25T16:52:16.656401Z",
            "url": "https://files.pythonhosted.org/packages/74/fb/3fbdfc89a17f69e9134c8c1e225268804370ac93b84226463e3faff98292/web3_input_decoder-0.1.11-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "95f9c4a04c1bd8ccbb702172735047f9471c2383f5e53f330f7d4023cd6b0ee4",
                "md5": "0cfdb48c732e20a0aa7fd754d01acf79",
                "sha256": "b311f93ed366bfdfadde998d86c5ffa1f1c233bb27eb4774888f3c492f5c83dd"
            },
            "downloads": -1,
            "filename": "web3_input_decoder-0.1.11.tar.gz",
            "has_sig": false,
            "md5_digest": "0cfdb48c732e20a0aa7fd754d01acf79",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.8",
            "size": 5700,
            "upload_time": "2024-03-25T16:52:17",
            "upload_time_iso_8601": "2024-03-25T16:52:17.781739Z",
            "url": "https://files.pythonhosted.org/packages/95/f9/c4a04c1bd8ccbb702172735047f9471c2383f5e53f330f7d4023cd6b0ee4/web3_input_decoder-0.1.11.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-03-25 16:52:17",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "kigawas",
    "github_project": "web3-input-decoder",
    "travis_ci": false,
    "coveralls": true,
    "github_actions": true,
    "lcname": "web3-input-decoder"
}
        
Elapsed time: 0.22498s