-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-things.js
132 lines (116 loc) · 3.27 KB
/
test-things.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
let logger = require("node-color-log");
const dhive = require("@hiveio/dhive");
var _ = require("lodash");
const dayjs = require("dayjs");
var utc = require("dayjs/plugin/utc");
var timezone = require("dayjs/plugin/timezone");
dayjs.extend(utc);
dayjs.extend(timezone);
const { dhiveClient } = require("./utils/dhive");
const { config } = require("./config");
const mongoDB = require("./database/mongoDB.js");
const adminRepository = require("./database/admin_repository");
function jsonToAddSuperAdmin(username) {
return {
id: "distriator_super_admin_add",
json: JSON.stringify({
username: username,
}),
};
}
function jsonToRemoveSuperAdmin(username) {
return {
id: "distriator_super_admin_remove",
json: JSON.stringify({
username: username,
}),
};
}
function jsonToAddAdmin(username) {
return {
id: "distriator_admin_add",
json: JSON.stringify({
username: username,
}),
};
}
function jsonToUpdateAdmin(username, banned) {
return {
id: "distriator_admin_update",
json: JSON.stringify({
username: username,
banned: banned,
}),
};
}
function jsonToRemoveAdmin(username) {
return {
id: "distriator_admin_remove",
json: JSON.stringify({
username: username,
}),
};
}
async function broadcastOps(json) {
const ops = [
"custom_json",
{
...json,
required_auths: [],
required_posting_auths: [config.distriator.account],
},
];
const key = dhive.PrivateKey.from(config.distriator.key);
const op = [ops];
try {
const result = await dhiveClient.broadcast.sendOperations(op, key);
logger.info(`Operation broadcasted. Result is - ${JSON.stringify(result)}`);
} catch (e) {
logger.error(e);
throw e;
}
}
async function adminRepoTest() {
await broadcastOps(jsonToAddSuperAdmin("starkerz"));
await broadcastOps(jsonToAddSuperAdmin("sagarkothari88"));
await broadcastOps(jsonToAddSuperAdmin("sagarkothari"));
await broadcastOps(jsonToAddSuperAdmin("scamforest"));
await broadcastOps(jsonToRemoveSuperAdmin("scamforest"));
}
// adminRepoTest();
// ================================
function isValidOperation(t) {
const checkOne =
t.operations[0][0] === "custom_json" &&
t.operations[0][1].required_posting_auths[0] === "thedistriator";
if (checkOne === false) return false;
return true;
}
async function syncFromBlock(blockNumber) {
let block = blockNumber;
try {
const blockData = await dhiveClient.database.getBlock(blockNumber);
let timestamp = blockData.timestamp;
if (timestamp.toLowerCase().includes("z") === false) {
timestamp = `${timestamp}Z`;
}
const blockDate = dayjs(timestamp);
const fastFetchNextBlock = blockDate.isBefore(dayjs().subtract(3, "second")) === true;
const operations = blockData.transactions;
const filteredOperations = operations.filter((t) => isValidOperation(t));
for (i = 0; i < filteredOperations.length; i++) {
await adminRepository.distriator_admin_add(
filteredOperations[i].operations[0][1]
);
}
block++;
} catch {
logger.error(`Error getting block number ${block}`);
}
}
async function readLastBlock() {
await mongoDB.connectDB();
// Overall start block is this - 87357738
await syncFromBlock(87357738);
}
readLastBlock();