py-contract-codegen
py-contract-codegen is a command-line tool for generating Python code from Ethereum ABI.
Installation
pip install py-contract-codegen
Commands
gen: Generate Python code from an Ethereum ABI file.version: Show the version of the code generator.
gen Command
The gen command is used to generate Python code from an Ethereum ABI.
py-contract-codegen gen [OPTIONS]
Options
╭─ Options ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ --abi-path PATH Path to ABI file. If not provided, `--abi-stdin` or `--contract-address` must be set [default: None] │
│ --abi-stdin --no-abi-stdin ABI content from stdin [default: no-abi-stdin] │
│ --contract-address TEXT Auto fetch abi from etherscan and generate code. Please set environment variable `ETHERSCAN_API_KEY` [default: None] │
│ --out-file PATH Path to save file name the generated code. If not provided, prints to stdout [default: None] │
│ --class-name TEXT Contract Class Name to save the generated code. If not provided, use `GeneratedContract` [default: GeneratedContract] │
│ --target-lib [web3_v7|web3_v6] Target library and version [default: web3_v7] │
│ --network [mainnet|sepolia] Ethereum network for fetching ABI [default: mainnet] │
│ --help Show this message and exit. │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
Command Examples
Gen from ABI file
py-contract-codegen gen --abi-path {ABI_FILE_PATH} --out-file generated_contract.py
Gen from stdin
cat {ABI_FILE_PATH} | py-contract-codegen gen --abi-stdin --out-file generated_contract.py
Gen from Contract Address
It automatically generates code by fetching the ABI through Etherscan API.
The contract's source code needs to be verified on Etherscan.
Please set environment variable ETHERSCAN_API_KEY.
py-contract-codegen gen --contract-address {CONTRACT_ADDRESS} --out-file generated_contract.py
Examples
To use the generated contract, you need to install web3.py.
Here's an example of a generated contract:
ERC20
from web3 import Web3
from eth_account import Account
w3 = Web3(provider=Web3.HTTPProvider("{YOUR_PROVIDER_URL}"))
private_key = "{YOUR_PRIVATE_KEY}"
contract_address = w3.to_checksum_address("{YOUR_CONTRACT_ADDRESS}")
to_address = w3.to_checksum_address("{YOUR_TO_ADDRESS}")
contract = GeneratedContract(contract_address=contract_address, web3=w3)
account = Account.from_key(private_key)
# balanceOf
balance = contract.balanceOf(to_address)
# transfer
transfer = contract.transfer(to_address, 1)
nonce = w3.eth.get_transaction_count(account.address)
build_tx = transfer.build_transaction({"from": account.address, "nonce": nonce})
signed_tx = account.sign_transaction(build_tx)
tx_hash = w3.eth.send_raw_transaction(signed_tx.rawTransaction)
w3.eth.wait_for_transaction_receipt(tx_hash)
License
MIT
Metadata
Release files for py-contract-codegen 0.1.3
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 |
|---|---|---|---|---|
| py_contract_codegen-0.1.3.tar.gz | 97.1 kB | Without provenance | Details for py_contract_codegen-0.1.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| py_contract_codegen-0.1.3-py3-none-any.whl | Python 3 | none | any | Details for py_contract_codegen-0.1.3-py3-none-any.whl |
Total release size: 119.4 kB
Release files / py_contract_codegen-0.1.3.tar.gz
| Download URL | py_contract_codegen-0.1.3.tar.gz |
|---|---|
| Size | 97.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a154be033f1228db6f4624922a29a9922bd886322ea16b8086ee29c3293e7445
|
|
BLAKE2b-256 checksum How to use checksums |
7de7972c6b18bb2244aabccab88720a0bfa428fb89bdb7c5e2dd80809d2abbaa
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.5
|
Release files / py_contract_codegen-0.1.3-py3-none-any.whl
| Download URL | py_contract_codegen-0.1.3-py3-none-any.whl |
|---|---|
| Size | 22.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
42a75e43c3f1904b0e91bb2417612e71fa086897df1c76dfc757a3bae3033d70
|
|
BLAKE2b-256 checksum How to use checksums |
81d58bb368ffc3404ff87ceea0423e8a74cb781bbd9f3473445264dd46afd888
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.5
|
Security for py-contract-codegen 0.1.3
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.