-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublish.js
86 lines (75 loc) · 2.69 KB
/
publish.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
const BigNumber = require("bignumber.js");
const {TwitterApi} = require("twitter-api-v2");
const twitterConfig = require("./twitter_config");
const {sumReducer} = require("./common");
const {getBurnedCount, database, getLatestTweet, getPaymentsCursor, getSwappedCount, getSwaps, insertTweet} = require("./db");
const twitterApi = new TwitterApi(twitterConfig);
const twitterClient = twitterApi.v2;
// log every 5 minutes if there was no update
const notifyAfter = 300;
let notifyAt;
let processing = false;
const shouldNotifyLogs = () => {
const shouldI = new Date().getTime() >= (notifyAt??0);
if (shouldI) {
notifyAt = new Date().getTime() + notifyAfter * 1000;
}
return shouldI;
}
const main = () => {
const latestTweet = getLatestTweet();
const cursor = getPaymentsCursor();
if (cursor === latestTweet?.latest_payment) {
return Promise.reject("Already published tweet for latest TX " + cursor);
}
processing = true;
const swaps = getSwaps();
const swapped = new BigNumber(getSwappedCount());
const burned = new BigNumber(getBurnedCount());
const total = swapped.plus(burned);
const amount = swaps.map(swap => swap.amount).reduce(sumReducer);
const status =
`🚀 ${total.toFormat()} claimable balances of #spam assets have been cleaned with #stellarclaim:🗑💱💰!\n\n` +
`🔥 ${burned.toFormat()} of them got burned\n` +
`💱 ${swapped.toFormat()} have been converted into ~${amount.toFormat(3)} $XLM\n` +
"\n" +
"🧹 Clean your #stellar account off spam on https://balances.lumens.space/claim\n" +
"\n" +
"#StellarFamily #trashtocash #XLM";
/*
return new Promise((resolve) => {
setTimeout(() => {
console.log(status);
resolve({data:{id:cursor}});
}, 2000);
})
*/
return twitterClient
.tweet(status)
.then(result => result.data.id)
.then(tweetId => confirmTweet(tweetId, cursor))
.then(tweetId => console.log("sent tweet:", tweetId))
.catch(console.warn)
.finally(() => { processing = false; });
};
const confirmTweet = (tweetId, operationsCursor) => {
insertTweet(tweetId, operationsCursor);
return tweetId;
}
const intervalEntry = () => {
if(!processing) {
main()
.then(() => { notifyAt = new Date().getTime() + 15000; })
.catch(e => {
if (shouldNotifyLogs()) {
console.log(e);
}
})
.finally(() => database().close());
}
return intervalEntry;
}
const intervalID = setInterval(intervalEntry(), 1000);
process.on("SIGINT", () => {
clearInterval(intervalID);
});