# 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/latest/web3.contract.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 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/55/e3/385a315be2f9db2843014359610865f1e5814a2b62a6c1424d9bc8dfb09f/web3_input_decoder-0.1.13.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/latest/web3.contract.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 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.13",
"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": "22b6e5fc30267a4001b915665604bf854170a2e68027c51ca1376157500eec6f",
"md5": "9c351428b715cf9257f13ec96fbb0eb2",
"sha256": "40753678b14d29f3eb58d902318cff5db9299aaa481e5f9f9d177bed1ed04e85"
},
"downloads": -1,
"filename": "web3_input_decoder-0.1.13-py3-none-any.whl",
"has_sig": false,
"md5_digest": "9c351428b715cf9257f13ec96fbb0eb2",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.8",
"size": 7859,
"upload_time": "2024-08-31T01:35:45",
"upload_time_iso_8601": "2024-08-31T01:35:45.535176Z",
"url": "https://files.pythonhosted.org/packages/22/b6/e5fc30267a4001b915665604bf854170a2e68027c51ca1376157500eec6f/web3_input_decoder-0.1.13-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "55e3385a315be2f9db2843014359610865f1e5814a2b62a6c1424d9bc8dfb09f",
"md5": "e2cdad990b6ee2128d97b8b87751f609",
"sha256": "5859f18c582601b966c1aacf6381d5f2edf259b1f6e2c72495be068c494f7165"
},
"downloads": -1,
"filename": "web3_input_decoder-0.1.13.tar.gz",
"has_sig": false,
"md5_digest": "e2cdad990b6ee2128d97b8b87751f609",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.8",
"size": 5687,
"upload_time": "2024-08-31T01:35:46",
"upload_time_iso_8601": "2024-08-31T01:35:46.844194Z",
"url": "https://files.pythonhosted.org/packages/55/e3/385a315be2f9db2843014359610865f1e5814a2b62a6c1424d9bc8dfb09f/web3_input_decoder-0.1.13.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-08-31 01:35:46",
"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"
}