2

I got logs using the following code.

web3.eth.subscribe('logs', {
      address: getLogsAddress,
      fromBlock: '0x' + fromBlock,
      toBlock: 'latest',
      topics: [topics]
  }, function(error, result){
    //   if (!error)
    //       console.log(result);
  })
  .on("connected", function(subscriptionId){
      console.log("subcriptionID === " + subscriptionId);
  })
  .on("data", function(log){
    //   console.log(log);
    // console.log(abiDecoder.decodeLogs(log));
  })
  .on("changed", function(log){
  });

I want to get a decode of this log. How to do this?

3
  • 1
    topics are not decodable, the decodable is only the Data field. Topics are the keccak on the Event signature. The ones that your generator of artifacts (whatever it is) have produced. So you only compare topics to existing signatures and decide if this event is the one you are looking for
    – Nulik
    Aug 18, 2021 at 0:15
  • actually you are posting the input paramter to subscription for logs, you shoulod provide the list of topics you want to listen for, this is not an output parameter which can be decoded somehow
    – Nulik
    Aug 18, 2021 at 0:17
  • I found it. please check it. Thank you for your reply.
    – Alex Weber
    Aug 18, 2021 at 2:39

2 Answers 2

2

I found how to decode topics of the log.

This is my sample code. Please check it.

let item = web3.eth.abi.decodeLog([{
                    type: 'string',
                    name: 'topicsAddress',
                    indexed: true
                },{
                    type: 'address',
                    name: 'from',
                    indexed: true
                },{
                    type: 'address',
                    name: 'to'
                }],
                log.data,
                log.topics
            );
            
            let temp = {};
            temp.from = item.from;
            temp.to = item.to;



I hope to help you.

1
  • yeah, here you decode it because you provided a sample log on the input, and the abi is provided as first parameter
    – Nulik
    Aug 18, 2021 at 17:38
0

You need to write a for/of loop, which will take each log and use web3.eth.abi.decodeParameters to read the value output of the topics of each log. Yes, it is helpful to know what each topic represents.

You would then create a 'typesArray' which you need to pass to your decode call, wherein you define what the topic type is and what the value will be, hence why it's helpful to know what each topic represents. This should be in JSON format.

It might look something like this in JS:

typesArray = 
[{
    type: 'address',
    name: 'addressFromTopic',
    indexed: true
}];

for (log of logs) {   
   try {
let logData = {
address: web3.eth.abi.decodeParameters(topicArray,String(log.topics[1]);
}  catch { error }};

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.