-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.js
161 lines (134 loc) · 5.39 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
155
156
157
158
159
160
161
const steem = require('steem');
const fs = require('fs');
const config = JSON.parse(fs.readFileSync('config.json', 'utf8'));
const frontrun = config.frontrun;
var VPlimit = frontrun.VPlimit * 100;
var STEEMIT_100_PERCENT = 10000;
var STEEMIT_VOTE_REGENERATION_SECONDS = (5 * 60 * 60 * 24);
var HOURS = 60 * 60;
var steemPrice;
var rewardBalance;
var recentClaims;
var currentUserAccount;
var votePowerReserveRate;
var totalVestingFund;
var totalVestingShares;
var steem_per_mvests;
function updateSteemVariables() {
steem.api.getRewardFund("post", function (e, t) {
if(t && !e) {
rewardBalance = parseFloat(t.reward_balance.replace(" STEEM", ""));
recentClaims = t.recent_claims;
} else {
log('Error loading reward fund: ' + e);
}
});
steem.api.getCurrentMedianHistoryPrice(function (e, t) {
if(t && !e) {
steemPrice = parseFloat(t.base.replace(" SBD", "")) / parseFloat(t.quote.replace(" STEEM", ""));
} else {
log('Error loading steem price: ' + e);
}
});
steem.api.getDynamicGlobalProperties(function (e, t) {
if(t && !e) {
votePowerReserveRate = t.vote_power_reserve_rate;
totalVestingFund = parseFloat(t.total_vesting_fund_steem.replace(" STEEM", ""));
totalVestingShares = parseFloat(t.total_vesting_shares.replace(" VESTS", ""));
steem_per_mvests = ((totalVestingFund / totalVestingShares) * 1000000);
} else {
log('Error loading global properties: ' + e);
}
});
setTimeout(updateSteemVariables, 180 * 1000)
}
function vestsToSP(vests) { return vests / 1000000 * steem_per_mvests; }
function getVotingPower(account) {
var voting_power = account.voting_power;
var last_vote_time = new Date((account.last_vote_time) + 'Z');
var elapsed_seconds = (new Date() - last_vote_time) / 1000;
var regenerated_power = Math.round((STEEMIT_100_PERCENT * elapsed_seconds) / STEEMIT_VOTE_REGENERATION_SECONDS);
var current_power = Math.min(voting_power + regenerated_power, STEEMIT_100_PERCENT);
return current_power;
}
function getVoteRShares(voteWeight, account, power) {
if (!account) {
return;
}
if (rewardBalance && recentClaims && steemPrice && votePowerReserveRate) {
var effective_vesting_shares = Math.round(getVestingShares(account) * 1000000);
var voting_power = account.voting_power;
var weight = voteWeight * 100;
var last_vote_time = new Date((account.last_vote_time) + 'Z');
var elapsed_seconds = (new Date() - last_vote_time) / 1000;
var regenerated_power = Math.round((STEEMIT_100_PERCENT * elapsed_seconds) / STEEMIT_VOTE_REGENERATION_SECONDS);
var current_power = power || Math.min(voting_power + regenerated_power, STEEMIT_100_PERCENT);
var max_vote_denom = votePowerReserveRate * STEEMIT_VOTE_REGENERATION_SECONDS / (60 * 60 * 24);
var used_power = Math.round((current_power * weight) / STEEMIT_100_PERCENT);
used_power = Math.round((used_power + max_vote_denom - 1) / max_vote_denom);
var rshares = Math.round((effective_vesting_shares * used_power) / (STEEMIT_100_PERCENT))
return rshares;
}
}
function getVoteValue(voteWeight, account, power) {
if (!account) {
return;
}
if (rewardBalance && recentClaims && steemPrice && votePowerReserveRate) {
var voteValue = getVoteRShares(voteWeight, account, power)
* rewardBalance / recentClaims
* steemPrice;
return voteValue;
}
}
function mTimer(cur_power) {
return (VPlimit - cur_power) * STEEMIT_VOTE_REGENERATION_SECONDS / VPlimit;
}
function timeTilFullPower(cur_power){
return (STEEMIT_100_PERCENT - cur_power) * STEEMIT_VOTE_REGENERATION_SECONDS / STEEMIT_100_PERCENT;
}
function getVestingShares(account) {
var effective_vesting_shares = parseFloat(account.vesting_shares.replace(" VESTS", ""))
+ parseFloat(account.received_vesting_shares.replace(" VESTS", ""))
- parseFloat(account.delegated_vesting_shares.replace(" VESTS", ""));
return effective_vesting_shares;
}
function getCurrency(amount) {
return amount.substr(amount.indexOf(' ') + 1);
}
function format(n, c, d, t) {
var c = isNaN(c = Math.abs(c)) ? 2 : c,
d = d == undefined ? "." : d,
t = t == undefined ? "," : t,
s = n < 0 ? "-" : "",
i = String(parseInt(n = Math.abs(Number(n) || 0).toFixed(c))),
j = (j = i.length) > 3 ? j % 3 : 0;
return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
}
function toTimer(ts) {
var h = Math.floor(ts / HOURS);
var m = Math.floor((ts % HOURS) / 60);
var s = Math.floor((ts % 60));
return padLeft(h, 2) + ':' + padLeft(m, 2) + ':' + padLeft(s, 2);
}
function padLeft(v, d) {
var l = (v + '').length;
if (l >= d) return v + '';
for(var i = l; i < d; i++)
v = '0' + v;
return v;
}
function log(msg) { console.log(new Date().toString() + ' - ' + msg); }
module.exports = {
updateSteemVariables: updateSteemVariables,
getVotingPower: getVotingPower,
getVoteValue: getVoteValue,
timeTilFullPower: timeTilFullPower,
getVestingShares: getVestingShares,
vestsToSP: vestsToSP,
getCurrency: getCurrency,
format: format,
toTimer: toTimer,
log: log,
mTimer: mTimer
}