Dex Explorer Script May 2026
def get_recent_swaps(pair_address, from_block, to_block): contract = w3.eth.contract(address=pair_address, abi=DEX_ABI) # Create event filter swap_filter = contract.events.Swap.create_filter( from_block=from_block, to_block=to_block )
RPC_URL = os.getenv("RPC_URL") # Your Infura/Alchemy key w3 = Web3(Web3.HTTPProvider(RPC_URL))
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_ABI = '''[ "anonymous": false, "inputs": [ "indexed": true, "name": "sender", "type": "address", "indexed": false, "name": "amount0In", "type": "uint256", "indexed": false, "name": "amount1In", "type": "uint256", "indexed": false, "name": "amount0Out", "type": "uint256", "indexed": false, "name": "amount1Out", "type": "uint256", "indexed": true, "name": "to", "type": "address" ], "name": "Swap", "type": "event" ]''' For a production script, you’d include the full factory and pair ABIs. But this is enough to get started. Here’s a function that fetches the last 100 blocks and extracts every swap event from a specific DEX router:
Start simple. Monitor one pair. Then expand to one DEX. Then to every DEX on three chains.
def get_recent_swaps(pair_address, from_block, to_block): contract = w3.eth.contract(address=pair_address, abi=DEX_ABI) # Create event filter swap_filter = contract.events.Swap.create_filter( from_block=from_block, to_block=to_block )
RPC_URL = os.getenv("RPC_URL") # Your Infura/Alchemy key w3 = Web3(Web3.HTTPProvider(RPC_URL))
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_ABI = '''[ "anonymous": false, "inputs": [ "indexed": true, "name": "sender", "type": "address", "indexed": false, "name": "amount0In", "type": "uint256", "indexed": false, "name": "amount1In", "type": "uint256", "indexed": false, "name": "amount0Out", "type": "uint256", "indexed": false, "name": "amount1Out", "type": "uint256", "indexed": true, "name": "to", "type": "address" ], "name": "Swap", "type": "event" ]''' For a production script, you’d include the full factory and pair ABIs. But this is enough to get started. Here’s a function that fetches the last 100 blocks and extracts every swap event from a specific DEX router:
Start simple. Monitor one pair. Then expand to one DEX. Then to every DEX on three chains.