-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathredeemer.js
315 lines (271 loc) · 12.2 KB
/
redeemer.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
const randy = require("randy");
const accounts = require('./config.js');
const moment = require("moment");
const dhive = require('@hiveio/dhive');
const client = new dhive.Client('https://api.hive.blog', {rebrandedApi: true});
function power_down(account, wif, vesting)
{
return new Promise(async resolve => {
const privateKey = dhive.PrivateKey.fromString(wif);
const op = [
'withdraw_vesting',
{
account: account,
vesting_shares: vesting,
},
];
client.broadcast.sendOperations([op], privateKey).then(
function() {
console.log("Power down reset on "+account);
return resolve("=");
},
function(error) {
console.error(error);
return resolve("=");
}
);
});
}
/**
* @param {float} num - Number to be analyzedgit
* @return {int} number of decimals
*/
function decimalPlaces(num) {
var match = ('' + num).match(/(?:\.(\d+))?(?:[eE]([+-]?\d+))?$/);
if (!match) {
return 0;
}
return Math.max(
0,
// Number of digits right of decimal point.
(match[1] ? match[1].length : 0)
// Adjust for scientific notation.
- (match[2] ? +match[2] : 0));
}
async function sell_hbd(account, reward_hbd, name)
{
return new Promise(async resolve => {
const price_data = await client.database.call("get_order_book");
const seconds = Math.round(Date.now() / 1000) + 604800;
const date = (new Date(seconds * 1000)).toISOString().slice(0, 19);
const price = price_data.asks[0].real_price;
if (parseFloat(price) <= account['max_ratio']) {
console.log(reward_hbd + " on the account, price is "+ price +" hbd per hive, selling it ");
let sell = Math.round((parseFloat(reward_hbd) * ((1 - parseFloat(price)) + 1)) * 1000) / 1000;
const decimals = decimalPlaces(sell);
if (decimals === 0)
sell += ".000 HIVE";
else if (decimals === 1)
sell += "00 HIVE";
else if (decimals === 2)
sell += "0 HIVE";
else
sell += " HIVE";
const op = [
"limit_order_create",
{
amount_to_sell : reward_hbd,
expiration : date,
fill_or_kill : false,
min_to_receive : sell,
orderid : randy.getRandBits(32),
owner : name
}
];
const privateKey = dhive.PrivateKey.fromString(account['wif']);
client.broadcast.sendOperations([op], privateKey).then(
function() {
console.log("sent buy order for " + name + " : " + sell);
return resolve("=");
},
function(error) {
console.error(error);
return resolve("-");
}
);
} else
return resolve("=")
});
}
function wait(time)
{
return new Promise(resolve => {
setTimeout(() => resolve('☕'), time*1000); // miliseconds to seconds
});
}
async function execute(times) {
console.log("Execution minute : " + times);
for (let account in accounts) {
let response = await client.database.getAccounts([account]);
const reward_hbd = response[0]['reward_hbd_balance']; // will be claimed as hive Dollars (HBD)
const reward_hive = response[0]['reward_hive_balance']; // this parameter is always '0.000 HIVE'
const reward_vests = response[0]['reward_vesting_balance']; // this is the actual VESTS that will be claimed as SP
const name = response[0].name;
if (accounts[name].reset_power_down === true) {
const current_date = moment();
const power_down_date = moment(response[0].next_vesting_withdrawal);
const duration = moment.duration(power_down_date.diff(current_date));
if (duration._milliseconds > 0 && accounts[name].power_down_date === undefined) {
accounts[name].power_down_date = response[0].next_vesting_withdrawal;
} else if (accounts[name].power_down_date !== undefined && accounts[name].power_down_date !== response[0].next_vesting_withdrawal) {
// Reason for this is that you can't power down all your sp if you voting power isn't 100%
// 100 is one extra 0.1% just to be sure that the power down will work
let current_available_shares = Math.floor((response[0].voting_power - 100) / 10000 * parseFloat(response[0].vesting_shares)) + ".000000 VESTS";
console.log("reset power down on " + name + "Powering down " + current_available_shares);
await power_down(name, accounts[name]['wif'], current_available_shares);
let updated_account = await client.database.getAccounts([account]);
accounts[name].power_down_date = updated_account[0].next_vesting_withdrawal;
}
}
// Triggers every 5 minutes
if (times % 5 === 0 || times === 0) {
if (parseFloat(response[0].hbd_balance) > 0) {
if (accounts[name].convert_hbd === true) {
console.log("converting hbd " + name);
convert_hbd(accounts[name]['wif'], name, parseFloat(response[0].hbd_balance))
} else if (accounts[name].sell_hbd === true) {
console.log("Selling hbd and executing actions on it for account : " + name);
await sell_hbd(accounts[name], response[0].hbd_balance, name);
} else if (accounts[name].liquid_hbd_action === "transfer") {
if (accounts[name].liquid_hbd_to_account !== "") {
// TODO: don't duplicate this code
if (accounts[name].liquid_hbd_action_min !== undefined) {
if (accounts[name].liquid_hbd_action_min <= parseFloat(response[0].hbd_balance)) {
console.log(`Transferring ${response[0].hbd_balance} hbd from ${name} to ${accounts[name].liquid_hbd_to_account}`);
await transfer(accounts[name]['wif'], name, accounts[name].liquid_hbd_to_account, response[0].hbd_balance, accounts[name].liquid_hbd_memo);
}
} else {
console.log(`Transferring ${response[0].hbd_balance} hbd from ${name} to ${accounts[name].liquid_hbd_to_account}`);
await transfer(accounts[name]['wif'], name, accounts[name].liquid_hbd_to_account, response[0].hbd_balance, accounts[name].liquid_hbd_memo);
}
} else {
console.log(`cannot transfer hbd from ${name}: liquid_hbd_to_account is not defined`)
}
} else if (accounts[name].liquid_hbd_action === "put_in_savings") {
if (accounts[name].liquid_hbd_action_min !== undefined) {
if (accounts[name].liquid_hbd_action_min <= parseFloat(response[0].hbd_balance)) {
if (parseFloat(response[0].hbd_balance) > 0) {
transfer_to_savings(accounts[name]['wif'], name, accounts[name].liquid_to_account, response[0].hbd_balance);
console.log(response[0].hbd_balance + " on " + name + ", putting it in the savings to " + accounts[name].liquid_to_account)
}
}
}
}
if (accounts[name].liquid_action === "powerup") {
if (parseFloat(response[0].balance) > 0) {
power_up(accounts[name]['wif'], name, accounts[name].liquid_to_account, response[0].balance);
console.log(response[0].balance + " on " + name + ", powering it up to " + accounts[name].liquid_to_account)
}
} else if (accounts[name].liquid_action === "put_in_savings") {
if (parseFloat(response[0].balance) > 0) {
transfer_to_savings(accounts[name]['wif'], name, accounts[name].liquid_to_account, response[0].balance);
console.log(response[0].balance + " on " + name + ", putting it in the savings to " + accounts[name].liquid_to_account)
}
}
}
// if it's been an hour since the last execution.
if (times === 60) {
console.log("Claiming rewards for account : " + name);
if (parseFloat(reward_hbd) > 0 || parseFloat(reward_hive) > 0 || parseFloat(reward_vests) > 0) {
const privateKey = dhive.PrivateKey.fromString(accounts[name]['wif']);
const op = [
'claim_reward_balance',
{
account: name,
reward_hive: reward_hive,
reward_hbd: reward_hbd,
reward_vests: reward_vests,
},
];
await client.broadcast.sendOperations([op], privateKey).catch( function(error) {
console.error(error);
});
console.log(name + " reward : " + reward_hbd + " , " + reward_hive + " " + reward_vests);
if (parseFloat(reward_hbd) > 0) {
if (accounts[name].convert_hbd === true) {
console.log("converting hbd " + name);
convert_hbd(accounts[name]['wif'], name, parseFloat(reward_hbd))
}
else if (accounts[name].sell_hbd === true) {
console.log("Selling hbd for account : " + name);
await sell_hbd(accounts[name], parseFloat(reward_hbd), name);
}
}
}
}
}
}
}
async function run() {
let i = 0;
while (true) {
await execute(i);
await wait(60);
i++;
if (i > 60)
i = 0;
}
}
console.log("Running...");
run();
function convert_hbd(activekey, owner, amount, tries = 0) {
const privateKey = dhive.PrivateKey.fromString(activekey);
const op = [
'convert',
{
amount: new dhive.Asset(amount, "HBD"),
owner: owner,
requestid: Math.floor(Math.random() * 4294967294), // 4294967294 is the max request id possible
},
];
client.broadcast.sendOperations([op], privateKey).then(
function(result) {
},
function(error) {
if (error.message === "could not insert object, most likely a uniqueness constraint was violated: " && tries < 10)
return convert_hbd(activekey, owner, amount, tries++);
console.error(error.message);
}
);
}
function power_up(Activekey, from, to, amount) {
const privateKey = dhive.PrivateKey.fromString(Activekey);
const op = [
'transfer_to_vesting',
{
from: from,
to: to,
amount: amount,
},
];
client.broadcast.sendOperations([op], privateKey).then(
function(result) {},
function(error) {}
);
}
function transfer_to_savings(Activekey, from, to, amount) {
const privateKey = dhive.PrivateKey.fromString(Activekey);
const op = [
'transfer_to_savings',
{
from: from,
to: to,
amount: amount,
memo : "",
request_id : randy.getRandBits(32),
},
];
client.broadcast.sendOperations([op], privateKey).then(
function(result) {},
function(error) {}
);
}
function transfer(Activekey, from, to, amount, memo) {
const privateKey = dhive.PrivateKey.fromString(Activekey);
client.broadcast.transfer({from, to, amount, memo}, privateKey).then(
function(result) {
},
function(error) {
}
);
}