-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtool.js
More file actions
105 lines (90 loc) · 3.14 KB
/
tool.js
File metadata and controls
105 lines (90 loc) · 3.14 KB
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
const STEEMIT_BANDWIDTH_AVERAGE_WINDOW_SECONDS = 60 * 60 * 24 * 7
, STEEM_VOTING_MANA_REGENERATION_SECONDS = 432000 // 432000 sec = 5 days
, STEEM_RC_MANA_REGENERATION_SECONDS = 432000 // 432000 sec = 5 days
, CHAIN_ENERGY_REGENERATION_SECONDS = 432000 // 432000 sec = 5 days
;
let urlParse = require(`url-parse`)
;
function calculateVotingPower(account) {
if (`voting_manabar` in account) {
let totalShares = parseFloat(account.vesting_shares) + parseFloat(account.received_vesting_shares) - parseFloat(account.delegated_vesting_shares) - parseFloat(account.vesting_withdraw_rate)
, elapsed = Math.floor(Date.now() / 1000) - account.voting_manabar.last_update_time
, maxMana = totalShares * 1000000
, currentMana = parseFloat(account.voting_manabar.current_mana) + elapsed * maxMana / STEEM_VOTING_MANA_REGENERATION_SECONDS;
if (currentMana > maxMana) {
currentMana = maxMana;
}
return (currentMana * 100 / maxMana).toFixed(2);
} else if (`energy` in account) {
return (calculateCurrentValue(account, `energy`) / 100)
} else {
return (calculateCurrentValue(account, `voting_power`) / 100)
}
}
function calculateCurrentValue(account, key) {
let lastVoteTime = Date.parse(account.last_vote_time)
, deltaTime = parseInt((new Date().getTime() - lastVoteTime + (new Date().getTimezoneOffset() * 60000)) / 1000)
, currentValue = parseInt(account[key] + (deltaTime * 10000 / CHAIN_ENERGY_REGENERATION_SECONDS))
;
if (currentValue > 10000) {
currentValue = 10000;
}
return currentValue;
}
function parsePostUrl(url) {
let parsed = urlParse(url.toLowerCase())
, parts = parsed.pathname.split(`/`)
, queryParams = parseQueryParams(parsed.query)
, authorIndex = 0
;
if (`author` in queryParams && `permlink` in queryParams) {
return {
author: queryParams[`author`]
, permlink: queryParams[`permlink`]
};
}
for (let i in parts) {
if (parts[i].length === 0) {
continue;
}
if (parts[i][0] === `@`) {
authorIndex = i * 1;
break;
}
}
if (authorIndex === 0) {
return {};
}
return {
author: parts[authorIndex].slice(1),
permlink: parts[authorIndex + 1]
};
}
function parseQueryParams(queryString) {
if (queryString[0] === `?`) {
queryString = queryString.slice(1);
}
let queryParts = queryString.split(`&`)
, queryParams = {}
;
for (let i in queryParts) {
let [key, val] = queryParts[i].split(`=`);
queryParams[key] = decodeURIComponent(val);
}
return queryParams;
}
function isArrayContainsProperty(objects, propertyName, propertyValue) {
let result = false;
for (let i in objects) {
if (objects[i][propertyName] === propertyValue) {
result = true;
break;
}
}
return result;
}
module.exports = {
calculateVotingPower: calculateVotingPower
, parsePostUrl: parsePostUrl
, isArrayContainsProperty: isArrayContainsProperty
};