How to Send ERC-20 Tokens using Web3.py and Python

·

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:


Prerequisites

Before proceeding, ensure you have:

DependencyVersion
Python3.9.6+
Web3.py6.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:

👉 Learn more about ERC-20 token standards


Step-by-Step Guide

Step 1: Set Up Your QuickNode Endpoint

  1. Sign up for a free QuickNode account here.
  2. Deploy an Ethereum Sepolia endpoint (testnet recommended for practice).
  3. 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.0

Step 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()?

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

👉 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! 🚀