There certainly is.
First, go to Alchemy, create a free account, register a new tool and drop your API key into your code as displayed there. Then, go to the composer.
There, choose your language, find eth_getLogs
from the drop down and specify the contract your hoping to listen to, along with a block range (this is limited to around 10K logs, so you’ll need to loop (or other) your function in order to crawl or use WebSockets to monitor.
You’ll then need to create a topicArray
, as you’re asking, defining the ‘type’ and ‘name’ for each topic you want to listen to. In JS it looks something like this:
const getLogsAndReadTopicValues = async () => {
let logs = await alchemy.core.getLogs({
<DROP THE FILE CREATED BY THE COMPOSER HERE>
});
//then, create your loop
for (log of logs) {
const topicArray = [
{type: ‘address’, name: ‘theNameIWantToGiveThisValueInMyOutput’}
];
const topic0Array = web3.eth.abi.decodeParameters(topicArray, String(log.topics[0]));
try {
let topic0Value = topic0Array.theNameIWantToGiveThisValueInMyOutput;
console.log(topic0Value);
} catch (error) {
console.log(error);
}
getLogsAndReadTopicValues();
You'll then have direct access to the value held within the topic hexes.
EDIT
Now, if you want to listen to more than one topic, just define another topicArray for your other topics, and define your output in your loop accordingly. Something like this works:
const anotherTopicArray = [
{type: ‘address’, name: ‘theOtherInterestingTopicOutput’}
]
const anotherTopicArrayValue = web3.eth.abi.decodeParameters(anotherTopicArray, String(log.topics[2]));
const topic2Value = anotherTopicArrayValue.theOtherInterestingTopicOutput;
console.log(topic2Value);
It's worth noting some things.
Firstly, that you can supposedly do this with one topic array, but you might run into buffer problems, so this individual deconstruction will yield you what you're looking for, just in more lines of code/with less faf.
Secondly, you need to know what types of value the topics are outputting (so here, we have defined both of our type
s as address - but these can be uint as well.
Hope that helps