Translating decoded transactions into Buys/Sells

How can I determine if a transaction is a buy or a sale of a certain token?
What about complex transactions like the one below ( I would need to get a similar understanding of the transaction through code; I’m currently using Python)?


Thank you very much for the help.

Hi @Betas

You can decode the swap tokens data from a transaction using get-decoded-transaction endpoint. The decoded log data can give more details on if it is a buy or sale of a token.

I already do that using get_transactions_verbose, in theory.
My problem is using the decoded logs to determine exactly what happened in the transaction. Is there any scheme to follow to do so?

This is what I concluded after comparing the usdt swap data from API with the usdt trades data on etherscan

The below is a sell trade where amount0 is the value of token0 which is a positive number and indicates a sale in a swap. and amount1 is the value of token1 which is a negative that indicates the token received in a swap.

"decoded_event": {
        "signature": "Swap(address,address,int256,int256,uint160,uint128,int24)",
        "label": "Swap",
        "type": "event",
        "params": [
          {
            "name": "sender",
            "value": "0x3fC91A3afd70395Cd496C647d5a6CC9D4B2b7FAD",
            "type": "address"
          },
          {
            "name": "recipient",
            "value": "0x6aacbE949F0766d3fD0E27384904d4E29C45354A",
            "type": "address"
          },
          {
            "name": "amount0",
            "value": "5413310785033668975",
            "type": "int256"
          },
          {
            "name": "amount1",
            "value": "-10000000000",
            "type": "int256"
          },
          {
            "name": "sqrtPriceX96",
            "value": "3406058967400672189642946",
            "type": "uint160"
          },
          {
            "name": "liquidity",
            "value": "11121876794809196193",
            "type": "uint128"
          },
          {
            "name": "tick",
            "value": "-201101",
            "type": "int24"
          }
        ]
      }

The below is a buy trade with different params data. In this case, the conditions seem like if amount0In and amount1Out are greater than 0 then it is a sell transaction and if amount1In and amount0Out are greater than 0 then it is a buy transaction.

"decoded_event": {
        "signature": "Swap(address,uint256,uint256,uint256,uint256,address)",
        "label": "Swap",
        "type": "event",
        "params": [
          {
            "name": "sender",
            "value": "0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D",
            "type": "address"
          },
          {
            "name": "amount0In",
            "value": "0",
            "type": "uint256"
          },
          {
            "name": "amount1In",
            "value": "2420901171",
            "type": "uint256"
          },
          {
            "name": "amount0Out",
            "value": "1305319972714076888",
            "type": "uint256"
          },
          {
            "name": "amount1Out",
            "value": "0",
            "type": "uint256"
          },
          {
            "name": "to",
            "value": "0xbe8BC29765E11894f803906Ee1055a344fDf2511",
            "type": "address"
          }
        ]
      }

The swap params data in different swap contracts seem different so we need to identify them in the code. The above sell trade example is from uniswap v3 and buy trade example is from uniswap v2.

Let me know if there are any questions. :smiley:

Another case where both token0 and token1 values are 0 is a no value trade. So it is neither a buy or sell.

Thank you very much for the reply.
How do I identify token0 and token1? Because I don’t see any topic or field in the swap log that could identify them.

In the transaction data returned from get-decoded-transaction endpoint you will also find other logs of the data.

You can find the token pair address in the decoded transaction logs data.

For example when you get the decoded transaction data of 0x392ebae1f1d6a52124be952ea94f524b231ba4bf83c38d295cc7fd0820abe654 you will 0x11b815efB8f581194ae79006d24E0d814B7697F6 address in the transfer logs. This is the token pair address of weth - usdt.

When you have the token pair address you can read the token0 and token1 from the contract variables using run-contract-function

token0 and token1 are hardcoded data from smart contracts so incase if you are only looking for few token swaps you can also hardcode it in your code for easy processing.


https://etherscan.io/address/0x11b815efB8f581194ae79006d24E0d814B7697F6#readContract

Hello, I was trying to implement the system to understand the swaps that happened in a transaction and I came out with an iteration of all the logs, getting token0 and token1 from the transfer events and ‘closing’ the single swap in a multi-swap transaction when I find the swap function log. It seemed to work but I guess the logs are not always in chronological order because I got a token even if its swap was already passed.

The answer given above, to try and decode the input data seems not to be feasible since the transaction has multiple swaps within ( screen of the transaction I’m trying to analyze above )

Each transaction log will have a log index to identify which log was processed first. Would that help?