Skip to main content

A module that prints Hello World on import

This project has been quarantined.

PyPI Admins need to review this project before it can be restored. While in quarantine, the project is not installable by clients, and cannot be being modified by its maintainers.

Read more in the project in quarantine help article.

Project description

eth-defi-demo

A Python project demonstrating how to build decentralized finance (DeFi) applications using the eth_defi library. This project provides examples for connecting to EVM-compatible networks, interacting with ERC-20 tokens, reading on-chain data, executing swaps, and automating common DeFi workflows.


Features

  • Connect to Ethereum-compatible RPC providers
  • Load wallet from a private key
  • Read ERC-20 token information
  • Query balances
  • Build and sign transactions
  • Execute token transfers
  • Perform Uniswap swaps
  • Batch RPC requests using Multicall
  • Estimate gas fees
  • Listen for blockchain events
  • Retry failed RPC requests
  • Support multiple EVM chains

Requirements

  • Python 3.10+
  • pip
  • Access to an Ethereum RPC endpoint

Examples:

  • Infura
  • Alchemy
  • QuickNode
  • Ankr
  • Local Geth
  • Local Anvil

Installation

git clone https://github.com/example/eth-defi-demo.git

cd eth-defi-demo

python -m venv .venv

source .venv/bin/activate      # Linux/macOS

# or

.venv\Scripts\activate         # Windows

pip install -r requirements.txt

requirements.txt

web3
web3-ethereum-defi
python-dotenv
requests

Project Structure

eth-defi-demo/
│
├── examples/
│   ├── connect.py
│   ├── wallet.py
│   ├── erc20.py
│   ├── transfer.py
│   ├── swap.py
│   ├── multicall.py
│   └── events.py
│
├── utils/
│   ├── config.py
│   ├── rpc.py
│   └── wallet.py
│
├── .env.example
├── requirements.txt
├── README.md
└── main.py

Configuration

Create a .env file.

RPC_URL=https://eth-mainnet.g.alchemy.com/v2/YOUR_API_KEY

PRIVATE_KEY=YOUR_PRIVATE_KEY

CHAIN_ID=1

Never commit your private key to Git.


Example

from web3 import Web3

rpc = "https://rpc.ankr.com/eth"

w3 = Web3(Web3.HTTPProvider(rpc))

print("Connected:", w3.is_connected())

print("Latest block:", w3.eth.block_number)

Reading ERC-20 Token Information

token_name = token.functions.name().call()

symbol = token.functions.symbol().call()

decimals = token.functions.decimals().call()

total_supply = token.functions.totalSupply().call()

Reading Wallet Balance

balance = w3.eth.get_balance(wallet)

print(w3.from_wei(balance, "ether"))

Token Transfer

tx = token.functions.transfer(
    recipient,
    amount
).build_transaction(...)

Swap Example

router.swap_exact_tokens_for_tokens(...)

Typical workflow:

  1. Approve token
  2. Build swap transaction
  3. Estimate gas
  4. Sign transaction
  5. Broadcast transaction
  6. Wait for receipt

Multicall

Instead of sending dozens of RPC requests:

balanceOf()
symbol()
decimals()
allowance()

Combine them into a single Multicall request for improved performance.


Event Listening

Example events:

  • Transfer
  • Approval
  • Swap
  • Mint
  • Burn

Useful for:

  • Wallet monitoring
  • Trading bots
  • Analytics
  • Portfolio tracking

Supported Networks

  • Ethereum
  • Arbitrum
  • Optimism
  • Base
  • Polygon
  • BNB Chain
  • Avalanche C-Chain
  • Fantom
  • Gnosis
  • Scroll
  • zkSync Era
  • Linea

Security Recommendations

  • Never hardcode private keys.
  • Store secrets in environment variables.
  • Validate contract addresses.
  • Verify token decimals before transfers.
  • Simulate transactions before broadcasting.
  • Use HTTPS RPC providers.
  • Limit wallet permissions.
  • Protect API keys.

Common Use Cases

  • Trading bots
  • Arbitrage bots
  • Portfolio trackers
  • Yield farming automation
  • Liquidity management
  • Token analytics
  • Wallet monitoring
  • Blockchain indexing
  • Treasury management
  • DeFi dashboards

Troubleshooting

Connection failed

  • Verify RPC endpoint.
  • Check API key.
  • Confirm internet connectivity.

Insufficient funds

Ensure the wallet has enough native tokens to pay gas fees.

Transaction reverted

Common causes include:

  • Insufficient allowance
  • Slippage exceeded
  • Incorrect router address
  • Expired deadline

Contributing

Contributions are welcome.

  1. Fork the repository.
  2. Create a feature branch.
  3. Commit your changes.
  4. Submit a pull request.

License

MIT License


Disclaimer

This project is provided for educational purposes only. Always test on a testnet before interacting with mainnet assets. You are responsible for securing your private keys and verifying all transactions before signing.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ethereum_input_decorder-1.2.2.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ethereum_input_decorder-1.2.2-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file ethereum_input_decorder-1.2.2.tar.gz.

File metadata

  • Download URL: ethereum_input_decorder-1.2.2.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for ethereum_input_decorder-1.2.2.tar.gz
Algorithm Hash digest
SHA256 28aa6e407999da8c2089f6bea9587b3413c730c5f105379058b9c6a90fb12ded
MD5 63106644e869bb3c0913c1d566472adb
BLAKE2b-256 9ac6d0003f1f73a1dd6dc38902b2fc4aa72f07759b766d28740f6eb78325ba4a

See more details on using hashes here.

File details

Details for the file ethereum_input_decorder-1.2.2-py3-none-any.whl.

File metadata

File hashes

Hashes for ethereum_input_decorder-1.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a409f8dfc84769d556f425ac9d4fc7ca41513cfe25f590994db8281bf62c8750
MD5 4fcc54655f42ee1fea9876b9c4ed920a
BLAKE2b-256 4c77035c87b60330888b38f2f21fb480c3ab857589c3e32290a834a8831b4d0d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page