forked from chimera-defi/SharedDeposit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmva.js
62 lines (51 loc) · 1.56 KB
/
mva.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
// Howto: `npm i got; node mva.js`
const got = require("got");
const getFromCG = async function (suffix) {
let url = `https://api.coingecko.com/api/v3/${suffix}`;
let res = await got(url);
const text = await res.body;
return JSON.parse(text);
};
const getSGTPriceAtTime = async (dateStr = "9-9-2021") => {
let token = "sharedstake-governance-token",
prefix = "/history/?date=",
loc = "&localization=false",
url = `coins/${token}${prefix}${dateStr}${loc}`;
let res = await getFromCG(url);
return res.market_data.current_price.usd;
};
const getAvg = arr => arr.reduce((a, b) => a + b) / arr.length;
const getTAvg = async t => {
let days = getDatesArr(t - 1);
pricemap = await Promise.all(
days.map(async d => {
return await getSGTPriceAtTime(d);
}),
);
return getAvg(pricemap);
};
const getDatesArr = num => {
let d = new Date();
d.setDate(d.getDate() - num);
return dateLoop(d);
};
const dateLoop = start => {
var now = new Date();
var daysOfYear = [];
for (var d = new Date(start); d <= now; d.setDate(d.getDate() + 1)) {
day = String(d.getDate()).padStart(2, "0");
daysOfYear.push(`${day}-${d.getMonth() + 1}-${d.getFullYear()}`);
}
return daysOfYear;
};
async function main() {
let weeklyAvg = await getTAvg(7);
let monthlyAvg = await getTAvg(30);
if (monthlyAvg > weeklyAvg) {
console.log(`Current Monthly MA > Weekly MA - reduce rewards`);
} else {
console.log(`Current Monthly MA < Weekly MA - increase rewards`);
}
console.log(`Weekly: ${weeklyAvg} | Monthly: ${monthlyAvg}`);
}
main();