A cli tool to generate Python code from EVM ABI.
Project description
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 = web3.eth.send_raw_transaction(signed_tx.rawTransaction)
web3.eth.wait_for_transaction_receipt(tx_hash)
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
py_contract_codegen-0.1.1.tar.gz
(17.5 kB
view details)
Built Distribution
File details
Details for the file py_contract_codegen-0.1.1.tar.gz
.
File metadata
- Download URL: py_contract_codegen-0.1.1.tar.gz
- Upload date:
- Size: 17.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c33d118f64a4cb50a6eff1db5864226003d7d323c51c38bd1e49b4a3e3b25cd0 |
|
MD5 | 3b2f088e8333197ef3f38a92ceb44c78 |
|
BLAKE2b-256 | 1769a97472bd8e2ac1aed200d9b599d13a03a573c66fb143f919268b782aa0a7 |
File details
Details for the file py_contract_codegen-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: py_contract_codegen-0.1.1-py3-none-any.whl
- Upload date:
- Size: 22.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cd3e0cba032a7445ba7a4f60d575c2e4d86e3880575a53067bcd81f2cf80b7d7 |
|
MD5 | f5e8390d990586f5d53de54d2f148cb1 |
|
BLAKE2b-256 | ab3cab123a09ccd47c10435acf17ce76a3e50dd65f5cba471b42a61cd8001640 |