if w3.is_connected(): print(f"Connected to chain: w3.eth.chain_id") A DEX like Uniswap V2 emits two critical events: Swap and Sync . We only need the ABI fragments for those events:
pip install web3 requests python-dotenv Now, connect to Ethereum mainnet (or BSC, Polygon, etc.):
for s in swaps[:5]: print(f"Swap by s['sender']: s['amount0_out'] tokens out") Hardcoding a single pair address is fine for testing. A real explorer would query a factory contract to discover all pairs dynamically. Step 4: Making It Useful – Real-Time Monitoring A script that runs once is a toy. A script that runs forever is a tool. dex explorer script
Enter the .
RPC_URL = os.getenv("RPC_URL") # Your Infura/Alchemy key w3 = Web3(Web3.HTTPProvider(RPC_URL)) Step 4: Making It Useful – Real-Time Monitoring
swaps = [] for event in swap_filter.get_all_entries(): swaps.append( "block": event["blockNumber"], "sender": event["args"]["sender"], "amount0_in": event["args"]["amount0In"] / 1e18, "amount1_in": event["args"]["amount1In"] / 1e18, "amount0_out": event["args"]["amount0Out"] / 1e18, "amount1_out": event["args"]["amount1Out"] / 1e18, "to": event["args"]["to"] )
If you’ve ever wanted to peek under the hood of a decentralized exchange (DEX) like Uniswap or PancakeSwap, you’ve probably realized that while the data is public , it’s not exactly easy to read. RPC_URL = os
while True: current_block = w3.eth.block_number