Build Your Own On-Chain Graph Tracker
You can also easily call Collections based on Category and Version reading events:
1
//The Eth Item Factory has 4 different events, raised when a new Collection is created
2
var ethItemFactoryEventsByVersionAndABIValues = {
3
"NewNativeCreated(uint256,uint256,uint256,address)" : "NativeABI",
4
"NewWrappedERC1155Created(uint256,uint256,uint256,address)" : "W1155ABI",
5
"NewWrappedERC721Created(uint256,uint256,uint256,address)" : "W721ABI",
6
"NewWrappedERC20Created(uint256,uint256,uint256,address)" : "W20ABI"
7
};
Copied!
For every event, the fields are the same:
---uint256 indexed standardVersion
---uint256 indexed interoperableInterfaceModelVersion
---uint256 indexed modelVersion
---address ethItemAddress : the address of the Collection created by the Factory
You can choose of course to receive the Collections of all Categories or just the ones you're interested in by removing the irrelevant events in the ethItemFactoryEventsByVersionAndABIValues object above
1
//Let's convert plain events in keccak256 topics so they can be used for the web3.eth.getPastLogs call
2
var ethItemFactoryTopicsAndCollectionABIValues = {};
3
Object.entries(ethItemFactoryEventsByVersionAndABIValues).forEach(it => ethItemFactoryTopicsAndCollectionABIValues[web3.utils.sha3(it[0])] = it[1]);
4
5
//First argument of the topics array is an array of three hash elements. This means that first log topic can be one of the passed arguments (like the "OR" operator in Web2 DB queries)
6
var topics = [Object.keys(ethItemFactoryTopicsAndCollectionABIValues)];
7
8
//Now you can customize the query as you wish, e.g.:
9
10
//All the Collections we want must follow the first version of the EthItem Token Standard
11
topics.push(web3.eth.abi.encodeParameter('uint256', 1));
12
13
14
15
//No matter what is the version of the Interoperable Interface Model
16
17
topics.push([]);
18
19
//All the Collections must be generated by their respective models, but only for version 2 or 5
20
topics.push([web3.eth.abi.encodeParameter('uint256', 2), web3.eth.abi.encodeParameter('uint256', 5)]);
21
22
//Call the blockchain.
23
//Of course this is a generic-purpose code, it can be more efficient by using just the topics you need (e.g. give me only the Wrapped ERC721 Collections) or use from/to block tranches.
24
var logs = await web3.eth.getPastLogs({
25
address : factoryAddresses,
26
topics
27
});
28
29
//Navigate the logs to obtain info about collections
30
for (var log of logs) {
31
//Retrieve the correct ABI using the first log topic through the key/value map previously made
32
var collectionABIValue = ethItemFactoryTopicsAndCollectionABIValues[log.topics[0]];
33
34
//As already said, last topic param is che Collection address
35
var collectionAddress = web3.eth.abi.decodeParameter("address", log.topics[log.topics.length - 1]);
36
37
//Make a checksum address for eventual off-chain research purposes
38
collectionAddress = web3.utils.toChecksumAddress(collectionAddress);
39
40
//Grab the correct Collection ABI from the configuration file
41
var collectionABI = configuration[collectionABIValue];
42
43
//Extract Collection Category label from the ABI value. Just remove the latest 3 "ABI" characters from the string
44
var collectionCategory = collectionABIValue.split("ABI")[0];
45
46
//Put everything in the allCollections array
47
allCollections.push({
48
address : collectionAddress,
49
category : collectionCategory,
50
contract : new web3.eth.Contract(collectionABI, collectionAddress)
51
});
52
}
53
54
//Use all the collections as you wish
55
console.table(allCollections.map(it => {return {address : it.address, category : it.category}}));
56
Copied!
Copy link