Overview
In this guide, we explore how to transfer ERC-20 tokens on the Ethereum blockchain using Web3.py, a Python library for Ethereum interaction. ERC-20 tokens are a widely adopted standard for fungible tokens, powering decentralized applications (dApps), DeFi protocols, and more.
By the end of this tutorial, you’ll learn how to:
- Set up a Python environment for blockchain development.
- Transfer ERC-20 tokens to another Ethereum address.
- Approve a smart contract to spend tokens on your behalf.
Prerequisites
Before proceeding, ensure you have:
- Python 3.9+ installed.
- A basic understanding of Ethereum and smart contracts.
- An Ethereum wallet (e.g., MetaMask) with access to your private key.
- A QuickNode endpoint (free tier available) for blockchain connectivity.
| Dependency | Version |
|---|---|
| Python | 3.9.6+ |
| Web3.py | 6.18.0+ |
Understanding ERC-20 Tokens
ERC-20 tokens adhere to a standardized smart contract interface, enabling seamless interoperability across Ethereum-based platforms. Key functions include:
transfer()– Sends tokens to another address.approve()– Authorizes a third party (e.g., a smart contract) to spend tokens.
👉 Learn more about ERC-20 token standards
Step-by-Step Guide
Step 1: Set Up Your QuickNode Endpoint
- Sign up for a free QuickNode account here.
- Deploy an Ethereum Sepolia endpoint (testnet recommended for practice).
- Note your RPC URL—you’ll need it to connect to the blockchain.
Step 2: Install Web3.py
Install the library via pip:
pip install web3==6.18.0Step 3: Transfer ERC-20 Tokens
Create a Python script (main.py) with the following code:
from web3 import Web3
import json
# Configure Web3 connection
RPC_URL = 'YOUR_QUICKNODE_ENDPOINT'
w3 = Web3(Web3.HTTPProvider(RPC_URL))
# Contract details
CONTRACT_ADDRESS = Web3.to_checksum_address('0xYourTokenContract')
TO_ADDRESS = '0xRecipientAddress'
PRIVATE_KEY = 'YourPrivateKey' # Never expose in production!
# Load ABI (from Etherscan or the token's documentation)
with open('abi.json') as f:
ABI = json.load(f)
contract = w3.eth.contract(address=CONTRACT_ADDRESS, abi=ABI)
# Build transaction
nonce = w3.eth.get_transaction_count(w3.eth.account.from_key(PRIVATE_KEY).address)
tx = contract.functions.transfer(TO_ADDRESS, w3.to_wei(1, 'ether')).build_transaction({
'chainId': w3.eth.chain_id,
'gas': 200000,
'gasPrice': w3.eth.gas_price,
'nonce': nonce,
})
# Sign and send
signed_tx = w3.eth.account.sign_transaction(tx, PRIVATE_KEY)
tx_hash = w3.eth.send_raw_transaction(signed_tx.rawTransaction)
print(f"Transaction Hash: {tx_hash.hex()}")Step 4: Approve Token Spending
To authorize a smart contract (e.g., a DEX) to spend tokens:
SPENDER_CONTRACT = '0xSpenderAddress'
tx = contract.functions.approve(SPENDER_CONTRACT, w3.to_wei(100, 'ether')).build_transaction({
'chainId': w3.eth.chain_id,
'gas': 200000,
'nonce': nonce + 1, # Increment nonce for sequential transactions
})FAQs
1. What’s the difference between transfer() and approve()?
transfer()sends tokens directly to an address.approve()grants permission to another contract to withdraw tokens later.
2. How do I get testnet ETH for gas fees?
Use the QuickNode Multi-Chain Faucet to obtain Sepolia ETH.
3. Is it safe to share my private key?
Never share your private key. Use environment variables or secure vaults in production.
Best Practices
- Gas Optimization: Use
eth_gasPriceto avoid overpaying. - Error Handling: Check for revert reasons with
try-exceptblocks. - Security: Store keys securely using tools like
python-dotenv.
👉 Explore advanced Web3.py techniques
Final Thoughts
Mastering ERC-20 transfers with Web3.py opens doors to DeFi development, token swaps, and more. Experiment with different networks (e.g., Polygon, Arbitrum) and explore advanced features like batch transactions.
For further reading:
Got feedback? Let us know how we can improve this guide! 🚀