Repository navigation
Expand file tree
/
Copy pathpowerup.js
More file actions
134 lines (104 loc) · 3.62 KB
/
Copy pathpowerup.js
File metadata and controls
134 lines (104 loc) · 3.62 KB
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
require('dotenv').config();
const fs = require('fs');
const path = require('path');
const { Client, PrivateKey } = require('@hiveio/dhive');
const username = process.env.HIVE_USERNAME;
const activeKey = process.env.HIVE_ACTIVE_KEY;
const amount = process.env.HIVE_AMOUNT || '1.000';
// Conexão com a Hive
const client = new Client([
'https://api.hive.blog',
'https://api.deathwing.me',
'https://hive-api.arcange.eu',
]);
const now = new Date();
const utcDateStr = now.toISOString().split('T')[0]; // YYYY-MM-DD
// Logger
function log(msg) {
const now = new Date();
const timestamp = now.toISOString();
const dia = String(now.getUTCDate()).padStart(2, '0');
const mes = String(now.getUTCMonth() + 1).padStart(2, '0');
const ano = String(now.getUTCFullYear());
const logDir = path.join(__dirname, 'log');
if (!fs.existsSync(logDir)) fs.mkdirSync(logDir);
const logFile = path.join(logDir, `powerup_log_${dia}${mes}${ano}.txt`);
const fullMsg = `[${timestamp}] ${msg}\n`;
fs.appendFileSync(logFile, fullMsg);
console.log(fullMsg.trim());
}
// Validação
log(`Validating configuration parameters...`);
if (!username || username.trim() === '' || username === 'your_hive_user') {
log('HIVE_USERNAME field is empty or not set in .env file! Cannot continue!');
process.exit(1);
}
if (!activeKey || activeKey.trim() === '' || activeKey === 'your_private_key') {
log('HIVE_ACTIVE_KEY field is empty or not set in .env file! Cannot continue!');
process.exit(1);
}
if (!amount || isNaN(parseFloat(amount)) || parseFloat(amount) <= 0) {
log('Invalid HIVE_AMOUNT field in .env file! Cannot continue!');
process.exit(1);
}
log(`Checking Account Power Up Operations for ${username}...`);
// Buscar até 10.000 entradas de histórico, em blocos de 1000
async function buscarPowerUpsDoDia(usuario, dataUTC) {
let start = -1;
const limiteMax = 10000;
let totalOps = [];
let done = false;
while (!done && totalOps.length < limiteMax) {
const batchSize = 1000;
const history = await client.database.call('get_account_history', [usuario, start, batchSize]);
if (!history || history.length === 0) break;
let foundPowerUpToday = false;
let stopBecauseOlder = false;
for (const [index, entry] of [...history].reverse()) {
const [opType, opData] = entry.op;
const timestamp = entry.timestamp.split('T')[0]; // YYYY-MM-DD
start = index - 1;
if (opType === 'transfer_to_vesting' && timestamp === dataUTC && opData.from === usuario) {
totalOps.push(entry);
foundPowerUpToday = true;
break;
}
if (timestamp < dataUTC) {
stopBecauseOlder = true;
break;
}
}
if (foundPowerUpToday) {
done = true;
} else if (stopBecauseOlder) {
done = true;
}
if (history.length < batchSize) break;
}
return totalOps;
}
// Main Function
(async () => {
try {
const powerUpsHoje = await buscarPowerUpsDoDia(username, utcDateStr);
if (powerUpsHoje.length > 0) {
log(`There is already Power Up today (${utcDateStr} UTC). No action will be taken.`);
process.exit(0);
}
log(`No Power Up found today. Performing ${amount} HIVE Power Up.`);
const key = PrivateKey.fromString(activeKey);
const op = [
'transfer_to_vesting',
{
from: username,
to: username,
amount: `${parseFloat(amount).toFixed(3)} HIVE`,
},
];
await client.broadcast.sendOperations([op], key);
log('Power Up successfully completed!');
} catch (err) {
log('Error fetching history or executing operation: ' + (err.message || String(err)));
process.exit(1);
}
})();