5

I'm learning how to listen events in blockchain. So I listened an event Swap on Uniswap V3 and got this result:

{
  blockNumber: 15560305,
  blockHash: '0x93fc57218eeb84d071e2f2f81d3e410a7e7981c6b53efb73101b4767c8ffe905',
  transactionIndex: 178,
  removed: false,
  address: '0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640',
  data: '0x000000000000000000000000000000000000000000000000000000005f5e1000fffffffffffffffffffffffffffffffffffffffffffffffff0858f94155405810000000000000000000000000000000000006728d2d3f2cada59e3cbc98513f5000000000000000000000000000000000000000000000000a8ef31a7a4b0a2530000000000000000000000000000000000000000000000000000000000031b77',
  topics: [
    '0xc42079f94a6350d7e6235f29174924f928cc2ac818eb64fed8004e115fbcca67',
    '0x00000000000000000000000068b3465833fb72a70ecdf485e0e4c7bd8665fc45',
    '0x00000000000000000000000068b3465833fb72a70ecdf485e0e4c7bd8665fc45'
  ],
  transactionHash: '0x1980388f4459f1932f2df2f5fe9fd4854eda822deba0f8a7f59fc787f11fab7e',
  logIndex: 321
}

As I understand the data like addresses and amounts is encoded in data. So I tried to decode it:

const Web3 = require('web3');
const web3 = new Web3();

const typesArray = [
    {type: 'address', name: 'sender'}, 
    {type: 'address', name: 'recipient'},
    {type: 'int256', name: 'amount0'},
    {type: 'int256', name: 'amount1'},
    {type: 'uint160', name: 'sqrtPriceX96'},
    {type: 'uint128', name: 'liquidity'},
    {type: 'int24', name: 'tick'}
];

const data = '0x000000000000000000000000000000000000000000000000000000005f5e1000fffffffffffffffffffffffffffffffffffffffffffffffff0858f94155405810000000000000000000000000000000000006728d2d3f2cada59e3cbc98513f5000000000000000000000000000000000000000000000000a8ef31a7a4b0a2530000000000000000000000000000000000000000000000000000000000031b77';

const decodedParameters = web3.eth.abi.decodeParameters(typesArray, data);

And I get en error:

reason: 'data out-of-bounds',
  code: 'BUFFER_OVERRUN',  length: 160,
  offset: 192

Is it the right way to decode it? I only used abiDecoder.decodeMethod() for decoding input data from transactions. So I got confused with different ways of encoding/decoding.

This code gives the same error:

const data = '0x000000000000000000000000000000000000000000000000000000005f5e1000fffffffffffffffffffffffffffffffffffffffffffffffff0858f94155405810000000000000000000000000000000000006728d2d3f2cada59e3cbc98513f5000000000000000000000000000000000000000000000000a8ef31a7a4b0a2530000000000000000000000000000000000000000000000000000000000031b77';
topics = ["0xc42079f94a6350d7e6235f29174924f928cc2ac818eb64fed8004e115fbcca67","0x00000000000000000000000068b3465833fb72a70ecdf485e0e4c7bd8665fc45","0x00000000000000000000000068b3465833fb72a70ecdf485e0e4c7bd8665fc45"]
const decodedParameters = web3.eth.abi.decodeLog(typesArray, data, topics);

1 Answer 1

2

So I looked at logs on Etherscan and found my event there.

Topics
0 0xc42079f94a6350d7e6235f29174924f928cc2ac818eb64fed8004e115fbcca67
1 0x00000000000000000000000068b3465833fb72a70ecdf485e0e4c7bd8665fc45
2 0x00000000000000000000000068b3465833fb72a70ecdf485e0e4c7bd8665fc45
Data
000000000000000000000000000000000000000000000000000000005f5e1000
fffffffffffffffffffffffffffffffffffffffffffffffff0858f9415540581
0000000000000000000000000000000000006728d2d3f2cada59e3cbc98513f5
000000000000000000000000000000000000000000000000a8ef31a7a4b0a253
0000000000000000000000000000000000000000000000000000000000031b77

I decoded the first string from data and found amount0 there. So I understood that this five strings from data are amount0,amount1,sqrtPriceX96,liquidity,tick . That's why I simply commented first 2 lines of my array:

const typesArray = [
    /* {type: 'address', name: 'sender'}, 
    {type: 'address', name: 'recipient'}, */
    {type: 'int256', name: 'amount0'},
    {type: 'int256', name: 'amount1'},
    {type: 'uint160', name: 'sqrtPriceX96'},
    {type: 'uint128', name: 'liquidity'},
    {type: 'int24', name: 'tick'}
];

And it worked:

{
    "0": "1600000000",
    "1": "-1115327466528569983",
    "2": "2092322563758657713336077772985333",
    "3": "12173002913898078803",
    "4": "203639",
    "__length__": 5,
    "amount0": "1600000000",
    "amount1": "-1115327466528569983",
    "sqrtPriceX96": "2092322563758657713336077772985333",
    "liquidity": "12173002913898078803",
    "tick": "203639"
}

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.