-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.js
154 lines (136 loc) · 3.64 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
const rp = require("request-promise");
const data = {
priceUSD: 0,
priceBTC: 0,
commits: 0,
};
function getPrice() {
return parseFloat(data.priceUSD).toFixed(2);
}
function getPriceInSats() {
return parseInt(data.priceBTC * 1e8, 10);
}
function getCommits() {
return parseInt(data.commits, 10);
}
function getAllStats() {
return data;
}
async function updateGithubStats() {
let requestOptions = {
headers: {
"User-Agent": "Request-Promise",
},
uri: "https://api.github.com/users/semuxproject/repos",
};
let repos;
try {
repos = JSON.parse(await rp(requestOptions));
} catch (e) {
console.error(e);
return console.error("Failed to update Github stats");
}
let totalCommits = 0;
for (let repo of repos) {
requestOptions.uri = `https://api.github.com/repos/${repo.full_name}/stats/participation`;
let commits;
try {
commits = JSON.parse(await rp(requestOptions));
} catch (e) {
console.error(e);
return console.error("Failed to update Github stats");
}
/* sum of last 4 weeks out of 52 */
if (commits.all.slice(48, 52).length) {
totalCommits += commits.all
.slice(48, 52)
.reduce((sum, currentVal) => sum + currentVal);
}
}
data.commits = totalCommits;
}
async function updateNetworkStats() {
let stats;
try {
stats = JSON.parse(await rp("https://semux.info/api/v1/network-stats"));
} catch (e) {
return console.error("Failed to update network stats");
}
data.totalTransactions = stats.total_transactions;
data.totalAddresses = stats.total_addresses;
data.validatorRoi = stats.validator_roi;
data.blockchainSize = stats.blockchain_size;
data.marketCap = stats.market_cap;
data.circulatingSupply = stats.current_supply;
data.maxSupply = stats.max_supply;
}
async function updateStexPrice() {
let stexPrice, btcUSD;
try {
stexPrice = JSON.parse(await rp("https://api3.stex.com/public/ticker/575"));
} catch (e) {
return console.error("Failed to update stex price");
}
try {
btcUSD = await getBtcPrice();
} catch (e) {
return console.error("Failed to get btcusd price");
}
data.priceBTC = parseFloat(stexPrice.data.last);
data.priceUSD = data.priceBTC * btcUSD;
}
async function getBtcPrice() {
let btcPrice;
try {
btcPrice = JSON.parse(
await rp("https://www.bitstamp.net/api/v2/ticker/btcusd/")
);
} catch (e) {
return console.error("Failed to update btc price");
}
return parseFloat(btcPrice.last);
}
function numberFormat(balance) {
const balanceInt = new Intl.NumberFormat("us-US").format(balance);
return balanceInt;
}
function parseBal(balance) {
return parseFloat((parseFloat(balance) / Math.pow(10, 9)).toFixed(10));
}
function numberToString(number) {
if (!number) {
return "";
}
return number.toString().replace(/(\d)(?=(\d\d\d)+([^\d]|$))/g, "$1,");
}
function toHexString(byteArray) {
return Array.from(byteArray, function (byte) {
return ("0" + (byte & 0xff).toString(16)).slice(-2);
}).join("");
}
function hexBytes(s) {
return Buffer.from(s.replace("0x", ""), "hex");
}
// update Semux USD and BTC price
updateStexPrice();
// update Semux price every 5 min
setInterval(updateStexPrice, 5 * 60 * 1000);
// update Network Stats on startup
updateNetworkStats();
// update Network Stats every 15 min
setInterval(updateNetworkStats, 15 * 60 * 1000);
// update Github stats on startup
updateGithubStats();
// update Github stats every 3 hours
setInterval(updateGithubStats, 3 * 60 * 60 * 1000);
module.exports = {
getPrice,
getPriceInSats,
getCommits,
getAllStats,
numberFormat,
parseBal,
numberToString,
toHexString,
hexBytes,
};