-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsponsor.js
80 lines (67 loc) · 2.09 KB
/
sponsor.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
require('dotenv').config();
const fetch = require("node-fetch");
const smoke = require("@smokenetwork/smoke-js");
// constants
const SPONSOR_API_LIST_URL = "https://api.smoke.io/sponsorapi/list";
const VOTER_NAME = '';
const VOTER_WIF = process.env.VOTER_WIF;
const ONEDAY = 24 * 60 * 60 * 1000;
// global vars
var sponsors = [];
const sleep = (ms) => {
return new Promise(resolve => setTimeout(resolve, ms));
};
// if author posted in the last day
const checkRecentPost = async (name) => {
let dateData = await smoke.api.getDiscussionsByAuthorBeforeDateAsync(name,null, new Date().toISOString().split('.')[0],1 , );
let timeCreated = Date.parse(dateData[0].created);
let timeNow = Date.now();
let difference = timeNow - timeCreated;
if (difference < ONEDAY) {
console.log("no posts in the last day " + difference);
return true;
}
return false;
};
const process_comment = async (name, link) => {
////////// Validating
// Needed to check if they a sponsor
let isSponsor = false;
for (let i = 0; i < sponsors.length; i++) {
if (sponsors[i].accountname === name) {
isSponsor = true;
}
}
if (isSponsor === false) {
console.log("Not sponsor");
return;
}
// Needed to see if they posted in the last day
const isRecentPost = await checkRecentPost(name);
if (isRecentPost === false) {
console.log("posted too old");
return;
}
////////// Processing here
// Needed for voting weight
let voteWeight = 10000;
if (sponsors.length > 10) {
voteWeight = 100000 / sponsors.length;
}
await smoke.broadcast.voteAsync(VOTER_WIF, VOTER_NAME, name, link, voteWeight);
};
const main = async () => {
// fetch sponsors to global data to be able to re-use later
const res = await fetch(SPONSOR_API_LIST_URL);
sponsors = await res.json();
await sleep(5000);
const txs = await smoke.api.streamOperations("head",(error, result) => {
// async in callback ?!
let txType = result[0];
let txData = result[1];
if (txType === "comment" && txData.parent_author === '') {
process_comment(txData.author, txData.permlink);
}
});
};
main();