How-To Properly construct an object for the getPastLogs and filter transactions with the topics parameter

78 Views Asked by At

Ok this is a bit of a long one so please forgive me ahead of time. My goal is to recreate the Dex Trade Table from a token page through the usage of the RJSONRPCAPI method enter image description here

Now I am using a Infura node and was a bit unsuccessful with their documentation and I could not get the server to send me results. I then went on ahead and used the web3 library and called web3.eth.getPastLogs .

const params = {
        address  : "0x481Aeac57Ed8b16c528B60F490366730AdF8A491",
        topics   : ["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"],
        fromBlock: '0x0', //startBlock,//'0x0',
        toBlock  : latestBlockNumberDecimal//'latest'
    };
    console.log("starting")
    await web3.eth.getPastLogs(params).then(response => console.log(response));
  

Now This would produce no results. I go through all kinds of different hex values to try in the topics array including web3.utils.keccak256("swap(address executor,tuple desc,bytes permit,bytes data)"), ethers.utils.id('swap(address executor,tuple desc,bytes permit,bytes data)') and I still received no results. I finally decided to send a request in with a blank topics

const params = {
        address  : "0x481Aeac57Ed8b16c528B60F490366730AdF8A491",
        //topics   : ["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"],
        fromBlock: '0x0', //startBlock,//'0x0',
        toBlock  : latestBlockNumberDecimal//'latest'
    };
    console.log("starting")
    await web3.eth.getPastLogs(params).then(response => console.log(response));
  

and this produced all kinds of results. It was beautiful. Now at this point i realized. My problem is my topics and before i get into my question, I wish to show one more finding; In the results of this successful call; I find a topics array inside the transactions. From this array I noticed 3 things.

Response:
{
    address: '0x481aeac57ed8b16c528b60f490366730adf8a491',
    blockHash: '0x59eba7b1cec5d1fb498b1d0ecee87e2533d7026c8c097717a5f7a55e1d3995a2',
    blockNumber: 18270504n,
    data: '0xffffffffffffffffffffffffffffffffffffffffffdf1c70c449867bffffffff',
    logIndex: 228n,
    removed: false,
    topics: [
      '0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925',
      '0x00000000000000000000000087a939d26c6125817643b8329f6d55bee1e2ff3b',
      '0x000000000000000000000000e592427a0aece92de3edee1f18e0157c05861564'
    ],
    transactionHash: '0xe3396e371f7ac2d9ac478f0512549895d28fe76a9d9c2eabd65204622f587da1',
    transactionIndex: 83n
  }

enter image description here

  1. That element 0 is the actual function used.
  2. Element 1 seemed to be the hex value of the "from" field
  3. Element 2 was the hex value of "Interacted With: To" field.

Going through each transaction I saw that even though it could be another transaction using a "Swap" function the first element would change. I realize that through research, that there is a ERC-20 standard that developers should follow and I kinda feel like its to avoid situations like these because If i use just that hex value then i will only be showed transactions that follow standard or the specific transactions that used the hex value I put into the array, and miss the other transactions. My question to the community is how does one construct this topics array so that I can also get all the dex trades associated with the desired token? Am I making sense and going in the correct direction for an answer?

0

There are 0 best solutions below