-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtron.js
52 lines (46 loc) · 1.57 KB
/
tron.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
/**
* @author JeffVader
*/
const axios = require('axios');
const path = require('path');
const fs = require('fs');
const headers = {
'TRON-PRO-API-KEY': '64fcfd69-f3b3-45b0-8c56-718a4bf33e54',
'Content-Type': 'application/json',
};
const decimals = 8;
const holders = [];
function writeToFileSync(filepath, args) {
const flag = 'w';
const fd = fs.openSync(filepath, flag);
fs.writeSync(fd, args);
fs.closeSync(fd);
}
async function followNext(link) {
const response = await axios.get(link, headers);
holders.push(...response.data.data);
if (response.data.meta && response.data.meta.links) {
const nextLink = response.data.meta.links.next;
await followNext(nextLink);
}
}
async function start(contract) {
axios.get(`https://api.trongrid.io/v1/contracts/${contract}/tokens?limit=200`, headers)
.then(async (response) => {
holders.push(...response.data.data);
if (response.data.meta && response.data.meta.links) {
const nextLink = response.data.meta.links.next;
await followNext(nextLink);
}
let total = 0;
holders.forEach((holder) => {
const amount = Number(Object.values(holder)[0]);
total += Number(amount / (10 ** decimals));
});
const homeDirPath = path.join(__dirname, './export/');
const filepath = `${homeDirPath}tronlist.json`;
console.log(`Done Tron - ${holders.length} records found. Exported at ${filepath}. Total Flux-TRON: ${total.toLocaleString()} (${total}).`);
writeToFileSync(filepath, JSON.stringify(holders, null, 2));
});
}
module.exports = { start };