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.4.tar.gz (6.6 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.4-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ethereum_input_decorder-1.2.4.tar.gz
  • Upload date:
  • Size: 6.6 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.4.tar.gz
Algorithm Hash digest
SHA256 d48fa377fbe06563c77b6fa945d6335d450b28d88a3ae05c2d5bd60d2dc3ae82
MD5 9133c416d45871062b1767daa17b5eff
BLAKE2b-256 01d47e2b82610c84c004dec4a7e70a567862df1534601349ba76cc3a16d9f25b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ethereum_input_decorder-1.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 0c9106b52cdc5e8b050212060b2f0ded1a22f247d2bc170f9539c8059035c913
MD5 0cedbd81c2f7b0ac8c0654df3eb1e716
BLAKE2b-256 9c42ce51a2c51605b114faf29bdf3ff7213572b9ae101b9069899dfa9cd652ce

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