forked from dfinity/portal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvoting-rewards.js
73 lines (63 loc) · 2.22 KB
/
voting-rewards.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
const fetch = require("node-fetch-retry");
let cache;
/** @type {import('@docusaurus/types').PluginModule} */
const votingRewardsPlugin = async function () {
return {
name: "voting-rewards",
async loadContent() {
if (!cache) {
const response = await fetch(
"https://ic-api.internetcomputer.org/api/nns/metrics",
{ method: "GET", retry: 10, pause: 500 }
).then((res) => res.json());
const lastRewardEventE8s = response.metrics.find((metric) => {
return metric.name === "governance_last_rewards_event_e8s";
});
if (
lastRewardEventE8s === undefined ||
lastRewardEventE8s.subsets.length === 0
) {
throw new Error(
`governance_last_rewards_event_e8s cannot be found in metrics ${JSON.stringify(
response.metrics
)}`
);
}
const totalVotingPowerE8s = response.metrics.find((metric) => {
return metric.name === "governance_voting_power_total";
});
if (
totalVotingPowerE8s === undefined ||
totalVotingPowerE8s.subsets.length === 0
) {
throw new Error(
`governance_voting_power_total cannot be found in metrics ${JSON.stringify(
response.metrics
)}`
);
}
const lastRewardsEventIcp =
+lastRewardEventE8s.subsets[0].value[1] / 100000000;
const totalVotingPowerIcp =
+totalVotingPowerE8s.subsets[0].value[1] / 100000000;
cache = [0.5, 1, 2, 3, 4, 5, 6, 7, 8].map((dissolveDelay) => {
const dissolveDelayBonus = 1 + dissolveDelay / 8;
const dailyRewardsIcpPerVotingPowerUnit =
lastRewardsEventIcp / totalVotingPowerIcp;
const estimatedRewardsIcpPerVotingPowerUnit =
dailyRewardsIcpPerVotingPowerUnit * dissolveDelayBonus;
return {
dissolveDelay,
reward: estimatedRewardsIcpPerVotingPowerUnit * 365.25 * 100,
};
});
}
return cache;
},
async contentLoaded({ content, actions }) {
const { setGlobalData } = actions;
setGlobalData(content);
},
};
};
module.exports = votingRewardsPlugin;