-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathKOIIminer.js
111 lines (94 loc) · 3.38 KB
/
KOIIminer.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
const getTaskData = require("./helpers/getTaskData");
const transferKOII = require("./helpers/transferKOII");
const { MongoClient } = require("mongodb");
const { Connection } = require("@_koii/web3.js");
require("dotenv").config();
const uri = process.env.DB_KEY;
const DB_name = "mainnet_airdrop_middleman";
const collection_name = "koiiminer";
async function hasRoundTransferred(round) {
const client = new MongoClient(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
try {
await client.connect();
const database = client.db(DB_name);
const transfersCollection = database.collection(collection_name);
// Check if the round exists in the collection
const transfer = await transfersCollection.findOne({ round: round });
return transfer !== null; // If the round exists, it has been transferred
} catch (error) {
console.error("Error checking round transfer:", error);
return false; // Return false if error occurs
} finally {
await client.close(); // Ensure the connection is closed
}
}
async function recordTransfer(round, koiiToken, transfers, taskID) {
const client = new MongoClient(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
try {
await client.connect();
const database = client.db(DB_name);
const transfersCollection = database.collection(collection_name);
// Insert the transfer record
await transfersCollection.insertOne({
round: round,
token: koiiToken,
transfers: transfers, // An array of objects with { address, amount }
taskID: taskID,
timestamp: new Date(),
});
console.log(`Recorded transfer for round ${round}`);
} catch (error) {
console.error("Error recording transfer:", error);
} finally {
await client.close(); // Ensure the connection is closed
}
}
async function main() {
try {
let addresses = [];
const taskID = "4ESVAytVPEmTWeVGxKX3kVhFrfFYPkXSCTMqFmWc5M4v";
const connection = new Connection("https://mainnet.koii.network");
const taskData = await getTaskData(
connection,
taskID,
0
);
// console.log("taskData", taskData);
addresses = taskData.submitters;
let koiiPerSubmission = 0.001;
console.log("Miner round: ", taskData.maxRound)
console.log("KOII per submission: ", koiiPerSubmission);
console.log("Addresses: ", addresses.length);
let checkTransferred = await hasRoundTransferred(taskData.maxRound);
if (checkTransferred) {
console.log("Round already transferred", taskData.maxRound);
} else {
// Transfer KOII to all addresses
for (let i = 0; i < addresses.length; i++) {
const address = addresses[i];
const transferResult = await transferKOII(connection, address, koiiPerSubmission);
if (transferResult) {
console.log(`Transferred ${koiiPerSubmission} KOII to ${address}`);
}
}
// Record the transfer after processing all addresses
await recordTransfer(taskData.maxRound, "KOII", addresses, taskID);
}
// Wait for the next round
console.log("Waiting for the next round...");
await new Promise((resolve) => setTimeout(resolve, 600000));
main();
} catch (error) {
console.error("Error in main function:", error);
console.log("Retrying in 5 seconds");
await new Promise((resolve) => setTimeout(resolve, 5000));
main();
}
}
main();