forked from MattyIce/postpromoter
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathreversal.js
110 lines (100 loc) · 5.19 KB
/
reversal.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
var steem = require('@hiveio/hive-js')
var dsteem = require('@hiveio/dhive')
var utils = require('./utils.js');
var fs = require('fs');
var config = JSON.parse(fs.readFileSync("config.json"));
var client = new dsteem.Client('https://anyx.io')
const version = 'postpromoter Steemium Fork - 1.0.0';
const wallet = JSON.parse(fs.readFileSync("wallet.json"));
function checkAmount(bid_transfer, reversal_transfer, reversal_price, steem_price, sbd_price, pubkey) {
let bid_amount = parseFloat(bid_transfer.amount)
let bid_currency = utils.getCurrency(bid_transfer.amount)
let reversal_amount = parseFloat(reversal_transfer.amount)
let reversal_currency = utils.getCurrency(reversal_transfer.amount)
let bid_usd = bid_currency == 'STEEM' ? bid_amount * steem_price : bid_amount * sbd_price
let reversal_usd = reversal_currency == 'STEEM' ? reversal_amount * steem_price : reversal_amount * sbd_price
let _reversal_price = bid_usd * reversal_price
let leftovers_usd = reversal_usd - _reversal_price
return leftovers_usd
}
async function reverseVote(vote_to_reverse, leftovers_usd, pubkey, reversal_transfer, steem_price, sbd_price, retries) {
let postURL = vote_to_reverse.memo.startsWith('#') ? vote_to_reverse.memo.substring(1) : vote_to_reverse.memo
let permlink = postURL.substr(postURL.lastIndexOf('/') + 1)
var author = postURL.substring(postURL.lastIndexOf('@') + 1, postURL.lastIndexOf('/'))
const vote = {
'voter': wallet.account.name,
'author': author,
'permlink': permlink,
'weight': 0
}
// console.log(vote)
try {
await client.broadcast.vote(vote, dsteem.PrivateKey.fromString(wallet.account.posting))
} catch(e) {
console.log(e)
utils.log('Error reversing vote for: @' + vote_to_reverse.from + permlink);
let already_reversed_err = 'itr->vote_percent != o.weight: Your current vote on this comment is identical to this vote.'
if (err = already_reversed_err) {
let memo = config.transfer_memos['already_reversed']
memo = memo.replace(/{postURL}/g, postURL)
utils.log(memo)
if (pubkey.length > 0) memo = steem.memo.encode(wallet.account.memo, pubkey, ('#' + memo))
return client.broadcast.transfer({ amount: reversal_transfer.amount, from: wallet.account.name, to: reversal_transfer.from , memo: memo}, dsteem.PrivateKey.fromString(wallet.account.active))
}
// Try again on error
if(retries < 2) return setTimeout(() => { reverseVote(vote_to_reverse, retries + 1); }, 10000);
else throw new Error(err)
}
utils.log('Vote reversed for: @' + vote_to_reverse.from + permlink);
// if no leftovers we close here
// send leftovers back
let currency = utils.getCurrency(reversal_transfer.amount)
let leftovers = (currency == 'STEEM') ? leftovers_usd / steem_price : leftovers_usd / sbd_price
leftovers = parseFloat(leftovers).toFixed(3) + ' ' + currency
let memo = config.transfer_memos['reversal_leftovers']
memo = memo.replace(/{postURL}/g, postURL);
utils.log(memo)
if (pubkey.length > 0) memo = steem.memo.encode(wallet.account.memo, pubkey, ('#' + memo))
return client.broadcast.transfer({ amount: leftovers, from: wallet.account.name, to: reversal_transfer.from, memo: memo}, dsteem.PrivateKey.fromString(wallet.account.active))
}
function sendReversalComment(vote_to_reverse) {
var content = null;
if(config.comment_reversal_to_author_location && config.comment_reversal_to_author_location != '') {
content = fs.readFileSync(config.comment_reversal_to_author_location, "utf8");
} else {
return utils.log('missing reversal_comment_to_author file or wrong path/location')
}
// If promotion content is specified in the config then use it to comment on the upvoted post
if (content && content != '') {
var parent_permlink = vote_to_reverse.memo.substr(vote_to_reverse.memo.lastIndexOf('/') + 1)
var author = vote_to_reverse.memo.substring(vote_to_reverse.memo.lastIndexOf('@') + 1, vote_to_reverse.memo.lastIndexOf('/'))
// Generate the comment permlink via steemit standard convention
var permlink = 're-' + author.replace(/\./g, '') + '-' + parent_permlink + '-' + new Date().toISOString().replace(/-|:|\./g, '').toLowerCase();
// Replace variables in the promotion content
content = content.replace(/\{permlink\}/g, parent_permlink)
// content = content.replace(/\{amount\}/g, reversal.amount)
// content = content.replace(/\{currency\}/g, reversal.currency)
// Broadcast the comment
var comment = {
author: wallet.account.name,
permlink: permlink,
parent_author: author,
parent_permlink: parent_permlink,
title: permlink,
body: content,
json_metadata: '{"app":"postpromoter/' + version + '"}'
};
// Broadcast the comment
client.broadcast.comment(comment, dsteem.PrivateKey.fromString(wallet.account.posting))
.then((res) => utils.log('reversal comment to reversed vote author has been succesfully broadcasted'))
.catch((e) => {
utils.log('reversal comment to reversed vote author has NOT been succesfully broadcasted')
console.log(e)
})
}
}
module.exports = {
checkAmount: checkAmount,
reverseVote: reverseVote,
sendReversalComment
}