ok, so I have found a viable way of doing this. there might be a better solution and yes, my code could more efficient. but it works for me. any feedback is very much appreciated of course.
This collects all Swap transactions of the QuickSwap USDC/WETH pool occurring over the last 1000 blocks.
from web3 import Web3
import pandas as pd
import numpy as np
from datetime import datetime
import requests
from hexbytes import HexBytes
# Web3 connection
w3 = Web3('your connection')
endblock = w3.eth.blockNumber
startblock = endblock - 1000
# contract address (Quickswap USDC/WETH pool)
address = '0x853ee4b2a13f8a742d64c8f088be7ba2131f670d'
# APIKey
APIKey = 'your API Key'
# Topic for Log filter
topic0 = '0xd78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d822' # ---> This is the topic identifying the swap function of the pool contract
topic1 = '0x000000000000000000000000a5e0829caced8ffdd4de3c43696c57f7d7a678ff' # ---> This is the topic identifying the swap router
# Get all Logs which meet the topic criteria and the pool contract address
Logs = 'https://api.polygonscan.com/api?module=logs&action=getLogs&fromBlock='+str(startblock)+'&toBlock='+str(endblock)+'&address='+address+'&topic0='+topic0+'&topic0_1_opr=and&topic1='+topic1+'&apikey='+APIKey
lgs = requests.get(Logs)
lgs = pd.read_json(lgs.text)
# Collect data
trades = []
cols = ['Block','Time', 'SellUSDC', 'SellWETH', 'BuyUSDC', 'BuyWETH']
for i in np.arange(lgs.shape[0]):
tx_hash = lgs.iloc[i,2]['transactionHash']
try:
receipt = w3.eth.getTransactionReceipt(tx_hash)
# We need to filter out the log of the exact event we want to decode
# pool.events.Swap().processReceipt(event) can only handel one log
relevantlogs = [i for index, i in enumerate(receipt['logs'])
if receipt['logs'][index]['address'].upper() == '0x853ee4b2a13f8a742d64c8f088be7ba2131f670d'.upper()
if receipt['logs'][index]['topics'][0] == HexBytes('0xd78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d822')]
event = {}
event['logs'] = relevantlogs
except:
print('Error for transaction: '+ tx_hash)
# Decode the event to get information
logs = pool.events.Swap().processReceipt(event)
# Collect information in a pd dataframe
Data = pd.DataFrame([[None]*len(cols)], columns = cols)
SellUSDC = np.round(logs[0]['args']['amount0In'] / 1e6,5)
SellWETH = np.round(logs[0]['args']['amount1In'] / 1e18,6)
BuyUSDC = np.round(logs[0]['args']['amount0Out'] / 1e6,5)
BuyWETH = np.round(logs[0]['args']['amount1Out'] / 1e18,6)
Data.iloc[0,0] = logs[0]['blockNumber']
Data.iloc[0,1] = datetime.fromtimestamp(int(lgs.iloc[i,2]['timeStamp'],16))
Data.iloc[0,2] = SellUSDC
Data.iloc[0,3] = SellWETH
Data.iloc[0,4] = BuyUSDC
Data.iloc[0,5] = BuyWETH
# Collect dataframea
trades.append(Data)
# Combine dataframes in one
Trades = pd.concat(trades)
Trades.set_index('Time', inplace = True)