· 4 years ago · Aug 20, 2021, 08:04 AM
1// Imports
2const axios = require('axios');
3const sqlite3 = require('sqlite3').verbose();
4const crypto = require('crypto')
5
6// Polygonscan
7const APIKEY = "<INSERT API KEY FROM POLYGONSCAN>"
8
9// Blockchain
10const TITAN_CONTRACT_ADDRESS = "0xaAa5B9e6c589642f98a1cDA99B9D024B8407285A".toLowerCase()
11
12var db
13let START_BLOCK = 14589807
14const END_BLOCK = 15801023
15
16// const END_BLOCK = 15801023
17
18async function main() {
19 initDB()
20 while (START_BLOCK < END_BLOCK) {
21 const range = getNextTransactionsRange()
22 let completed = false
23 const api = getContractTransactionsAPI(TITAN_CONTRACT_ADDRESS, range[0], range[1])
24 console.log("Processing blocks", range, '/', END_BLOCK)
25 try {
26 const { data } = await axios.get(api);
27 const shasum = crypto.createHash('sha256')
28 shasum.update(JSON.stringify(data))
29 while (!completed) {
30 if (data.status !== '0' || data.message === "No transactions found") {
31 for (const tx in data.result) {
32 addAccount(data.result[tx].from)
33 }
34 if (data.message !== "No transactions found")
35 storeDataHash(shasum.digest('hex'), range)
36 completed = true
37 } else {
38 console.log(data.message, api)
39 }
40 await sleep(500)
41 }
42 } catch (err) {
43 console.log(err)
44 console.log(api)
45 exit()
46 }
47 }
48 setTimeout(async () => {
49 console.log(await getAccountTotal(), "accounts added to database")
50 console.log("Total Data hash = " + await getDataHash())
51 closeDB();
52 }, 5000)
53}
54
55function sleep(ms) {
56 return new Promise(resolve => setTimeout(resolve, ms));
57}
58
59function addAccount(account) {
60 console.log("Checking", account)
61 if (account !== '') {
62 db.serialize(() => {
63 db.run('INSERT OR IGNORE INTO titan_accounts (account) VALUES(?)', [account], function (err) {
64 if (err) {
65 return console.log(err.message);
66 }
67 });
68 });
69 }
70}
71
72function storeDataHash(hash, startBlock, endBlock) {
73 console.log(hash)
74 db.serialize(() => {
75 db.run('INSERT INTO data_hashes (hash, start_block, end_block) VALUES(?,?,?)', [hash, startBlock, endBlock], function (err) {
76 if (err) {
77 return console.log(err.message);
78 }
79 });
80 });
81}
82
83function getDataHash() {
84 return new Promise((resolve, reject) => {
85 let dataString = ""
86 const shasum = crypto.createHash('sha256')
87 db.all("SELECT * FROM data_hashes", [], (err, rows) => {
88 if (err)
89 reject(err)
90 rows.forEach((row) => {
91 dataString += row.hash + row.start_block + row.end_block
92 });
93 });
94 shasum.update(dataString)
95 resolve(shasum.digest('hex'))
96 })
97}
98
99function getAccountTotal() {
100 return new Promise((resolve, reject) => {
101 db.get("SELECT count(*) FROM titan_accounts", [], (err, row) => {
102 if (err)
103 reject(err)
104 resolve(row['count(*)'])
105 });
106 })
107}
108
109function getNextTransactionsRange() {
110 if (START_BLOCK + 500 < END_BLOCK) {
111 return [START_BLOCK, (START_BLOCK += 500) - 1]
112 } else {
113 return [START_BLOCK, START_BLOCK += (END_BLOCK - START_BLOCK)]
114 }
115}
116
117function getContractTransactionsAPI(contract, startBlock, endBlock) {
118 return 'https://api.polygonscan.com/api?module=account&action=txlist&address=' + contract + '&startblock=' + startBlock + '&endblock=' + endBlock + '&page=1&offset=1000&sort=asc&apikey=' + APIKEY
119}
120
121function initDB() {
122 db = new sqlite3.Database('./titan_accounts.db', (err) => {
123 if (err) {
124 console.error(err.message);
125 }
126 console.log('Connected to the titan_account database.');
127 db.serialize(() => {
128 db.run('CREATE TABLE IF NOT EXISTS titan_accounts(account TEXT UNIQUE NOT NULL)');
129 db.run('CREATE TABLE IF NOT EXISTS data_hashes(hash TEXT UNIQUE NOT NULL, start_block INT, end_block INT)');
130 });
131 });
132}
133
134function closeDB() {
135 db.close((err) => {
136 if (err) {
137 console.error(err.message);
138 }
139 console.log('Closed the database connection.');
140 });
141}
142
143main()
144