-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
54 lines (48 loc) · 2.49 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const database = require('./DB/db');
const { ethers } = require('ethers');
const erc721ABI = require('./ERC721ABI.json').ABI;
const provider = new ethers.JsonRpcProvider(process.env.PROVIDER_URL);
const axios = require('axios');
let contractOwnerBasedData = database.contractOwnerBasedData
let contractBasedData = database.contractBasedData
let ownerBasedData = database.ownerBasedData
const handleTX = async (contract, contractAddress, to , tokenId, event) => {
let tokenURI, tokenDetails;
if(tokenId){
const tokenIdString = tokenId.toString()
tokenURI = await contract.tokenURI(tokenId);
try{
tokenDetails = await axios.get(tokenURI);
console.log(tokenDetails);
} catch{
tokenDetails = {data: {name:"", description: "" }};
} finally{
if (!contractOwnerBasedData[contractAddress + "-" + to]) {
contractOwnerBasedData[contractAddress + "-" + to] = []
}
contractOwnerBasedData[contractAddress + "-" + to].push({"owner": to, "tokenId": tokenIdString, "tokenURI" : tokenURI, "tokenName" : tokenDetails.data.name, "tokenDescription" : tokenDetails.data.description });
if(!contractBasedData[contractAddress]){
contractBasedData[contractAddress] = []
}
contractBasedData[contractAddress].push({"owner" : to, "tokenId" : tokenIdString , "tokenURI" : tokenURI, "tokenName" : tokenDetails.data.name, "tokenDescription" : tokenDetails.data.description});
if(!ownerBasedData[to]){
ownerBasedData[to] = []
}
ownerBasedData[to].push({"owner" : to, "tokenId" : tokenIdString , "tokenURI" : tokenURI, "tokenName" : tokenDetails.data.name, "tokenDescription" : tokenDetails.data.description});
}
}
}
exports.subScribe = async (contractAddress = "0x2ac3C692f8cd4e87Bd46Ddf471EAAe59291D8b74") =>{
const contract = new ethers.Contract(contractAddress, erc721ABI, provider);
const filter = contract.filters['Transfer']();
const events = await contract.queryFilter(filter, 0, 'latest');
const users = new Set();
console.log("contractAddress: "+ contractAddress);
events.forEach(async event => {
handleTX(contract, contractAddress, event.args.to, event.args.tokenId, event);
});
contract.on('Transfer', (from, to, tokenId, event) => {
console.log("new transfer:" +to);
handleTX(contract, contractAddress, to, tokenId, event);
});
}