-
Notifications
You must be signed in to change notification settings - Fork 18
/
voter.js
48 lines (47 loc) · 1.97 KB
/
voter.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
const { getPathObj, deleteObjs } = require("./getPathObj");
const { store } = require("./index");
const config = require('./config');
//determine consensus... needs some work with memory management
exports.voter = () => {
return new Promise((resolve, reject) => {
var Ppending = getPathObj(['pendingvote'])
Promise.all([Ppending]).then(function(v) {
deleteObjs([
['pendingvote']
])
.then(empty => {
let posts = v[0],
totalWeight = 0,
ops = []
for (post in posts) {
totalWeight += posts[post].v
}
for (post in posts) {
let b = {
author: post.split('/')[0],
permlink: post.split('/')[1]
}
ops.push({
type: 'put',
path: ['escrow', config.leader, `vote:${b.author}/${b.permlink}`],
data: ["vote",
{
"voter": config.leader,
"author": b.author,
"permlink": b.permlink,
"weight": parseInt((posts[post].v / totalWeight) * 10000)
}
]
})
}
if (ops.length) {
store.batch(ops, [resolve, reject, 1])
} else {
resolve(1)
}
})
.catch(e => { console.log(e) })
})
.catch(e => console.log(e))
})
}