web3-input-decoder
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
-
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 ABItx_input: Transaction input to decode, with or without deployed contract bytecodebytecode: Optional deployed contract bytecode. If this is set,tx_inputshould include bytecode
Returns:
list[tuple[str, str, Any]]: Decoded type-name-value tuples
-
def decode_function( abi: list[dict], tx_input: Union[str, bytes] ) -> list[tuple[str, str, Any]]
Parameters:
abi: Contract ABItx_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.:
- web3py can only decode function calls and it's necessary to be online to set up a provider first.
- 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)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| web3_input_decoder-0.1.15.tar.gz | 6.0 kB | Without provenance | Details for web3_input_decoder-0.1.15.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| web3_input_decoder-0.1.15-py3-none-any.whl | Python 3 | none | any | Details for web3_input_decoder-0.1.15-py3-none-any.whl |
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
|
Security for web3-input-decoder 0.1.15
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.