2

I am trying to monitor my investment in a QuickSwap liquidity pool. So far, everything works fine tracking my LP-Token Value and calculating the impermanent loss etc. However, I would like to track the fees collected from all swap transactions happening in the pool. I am using python Web3 API and Infura. I was able to get all external transactions of the pool contract but this is not what I need. For example, when looking up a swap transaction shown on the QuickSwap UI, it seems that some transactions engage through different contracts. Are all swap transactions routed via the QuickSwap Router contract? Do I need to get all transactions from this contract and filter for the respective pool contract I am interested in?

Can anybody guide me in the right direction?

Thanks for any feedback in advance and enjoy your sunday

1
  • ok, I am trying to get the information with Polygon's Developer API collecting all logs polygonscan.com/apis#logs. I am having troubles decoding the data field, but it seems to me that this is a viable way getting the information
    – friend1
    Commented Dec 20, 2021 at 14:48

1 Answer 1

1

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)
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.