Dex Explorer Script ((install)) Info
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:
contract = w3.eth.contract(address=UNISWAP_V2_PAIR, abi=DEX_ABI) swap_events = contract.events.Swap.get_logs(from_block=18500000, to_block=18500100)
Add a simple loop with a sleep interval to monitor swaps every 12 seconds (Ethereum block time): dex explorer script
from web3 import Web3 w3 = Web3(Web3.HTTPProvider("YOUR_RPC_URL")) UNISWAP_V2_PAIR = "0xB4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc" # USDC/WETH
Enter the .
from web3 import Web3 import os from dotenv import load_dotenv load_dotenv()
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 ) to_block): contract = w3.eth.contract(address=pair_address
return swaps latest = w3.eth.block_number swaps = get_recent_swaps("0x...PAIR_ADDRESS...", latest-100, latest)