1

I am trying to use Python to parse Uniswap swap event data from Etherscan. Here is the code for obtaining the swap event log:

import requests

etherscan_api = 'https://api.etherscan.io/api'
etherscan_api_key = 'ETHERSCAN_API_KEY'
pool_address = '0x5777d92f208679DB4b9778590Fa3CAB3aC9e2168' #DAU/USDC pool

swap_topic = '0xc42079f94a6350d7e6235f29174924f928cc2ac818eb64fed8004e115fbcca67'

params = {
    'module': 'logs',
    'action': 'getLogs',
    'address': pool_address,
    'fromBlock': 15591456,
    'toBlock': 15591456,
    'topic0': swap_topic,
    'page': 1,
    'offset': 1000,
    'apikey': etherscan_api_key
}

resp = requests.get(url=etherscan_api, params=params)
resp.json()['result']

which returns

[{'address': '0x5777d92f208679db4b9778590fa3cab3ac9e2168',
  'topics': ['0xc42079f94a6350d7e6235f29174924f928cc2ac818eb64fed8004e115fbcca67',
   '0x000000000000000000000000e592427a0aece92de3edee1f18e0157c05861564',
   '0x000000000000000000000000b0ccf43ada6cbaa26dcf4907117b496d49f74242'],
  'data': '0xfffffffffffffffffffffffffffffffffffffffffffffff7cf3dd2f5c2526afa000000000000000000000000000000000000000000000000000000000901ae820000000000000000000000000000000000000000000010c70158041addb5d04a00000000000000000000000000000000000000000003717214bf888b6de019b6fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffbc89c',
  'blockNumber': '0xede820',
  'blockHash': '0x173abb50387b29add553b4e1fc3154d02f0fc55c277738bd8d16d3d8022ae76d',
  'timeStamp': '0x632cce2b',
  'gasPrice': '0x2ba5552a7',
  'gasUsed': '0x20d7a',
  'logIndex': '0xb6',
  'transactionHash': '0x63e6d27fdbea0968fb10130e59dfa1b79d18074a971ed5cbd7c25cfe32c1f1e3',
  'transactionIndex': '0x34'}]

I expect that the data field contains the swap information (sender, receiver, amounts, etc.) How do I actually parse it?

1 Answer 1

0

You can decode it this way:

const Web3 = require("web3");

const web3 = new Web3("http://127.0.0.1:7545");

const data =
  "0xfffffffffffffffffffffffffffffffffffffffffffffff7cf3dd2f5c2526afa000000000000000000000000000000000000000000000000000000000901ae820000000000000000000000000000000000000000000010c70158041addb5d04a00000000000000000000000000000000000000000003717214bf888b6de019b6fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffbc89c";

// Swap(address,address,int256,int256,uint160,uint128,int24) // keccak256 hash of this signature is:
// c42079f94a6350d7e6235f29174924f928cc2ac818eb64fed8004e115fbcca67 // the topic

// Swap(
//     address indexed sender,
//     address indexed recipient,
//     int256 amount0,
//     int256 amount1,
//     uint160 sqrtPriceX96,
//     uint128 liquidity,
//     int24 tick
// )

const decoded = web3.eth.abi.decodeParameters(
  // Decoding event data. Exclude the `indexed` parameters.
  ["int256", "int256", "uint160", "uint128", "uint24"],
  data
);

console.log("decoded: ", decoded);

In the data field, we have the values of the non-indexed event params. The indexed param data are topics themselves and are not included in the data. Thus, when decoding the data, we need to indicate the non-indexed params.

I calculated the keccak256 hash of the Swap(address,address,int256,int256,uint160,uint128,int24) event signature to make sure it is the same topic as the one you have, and it is. Check yourself here: https://emn178.github.io/online-tools/keccak_256.html

So, just use web3.eth.abi.decodeParameters(arrayOfNonIndexedTypes,data) to decode your data.

In this case, the result is:

decoded:  Result {
  '0': '-151087372770986530054',
  '1': '151105154',
  '2': '79228862628487947997258',
  '3': '4162509295296294057679286',
  '4': '16500892',
  __length__: 5
}

I'm not sure if that data makes sense to you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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