-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathKOIIsender.js
More file actions
188 lines (166 loc) · 5.72 KB
/
KOIIsender.js
File metadata and controls
188 lines (166 loc) · 5.72 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
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
const { Connection, PublicKey } = require("@_koii/web3.js");
const transferKOII = require("./helpers/transferKOII");
const { getTaskStateInfo } = require("@_koii/create-task-cli");
const getStakingAccountInfo = require("./helpers/getStakingAccountInfo");
const { MongoClient } = require("mongodb");
const fs = require('fs');
require("dotenv").config();
const uri = process.env.DB_KEY;
const DB_name = "mainnet_airdrop_middleman";
const collection_name = "KOIIsender";
// Create a single MongoDB client instance
let mongoClient = null;
async function connectToMongo() {
if (!mongoClient) {
mongoClient = new MongoClient(process.env.DB_KEY, {
// Remove deprecated options
// useNewUrlParser and useUnifiedTopology are no longer needed in MongoDB 4.0+
});
await mongoClient.connect();
}
return mongoClient;
}
async function hasRoundTransferred(taskId, address) {
try {
const client = await connectToMongo();
const db = client.db(DB_name);
const collection = db.collection(collection_name);
const result = await collection.findOne({
task: taskId,
stakingAddress: address
});
return !!result;
} catch (error) {
console.error('Error checking round transfer:', error);
return false;
}
}
async function recordTransfer(taskId, address, walletAddress, amount, signature) {
try {
const client = await connectToMongo();
const db = client.db(DB_name);
const collection = db.collection(collection_name);
// Insert the transfer record with all the necessary fields
await collection.insertOne({
task: taskId,
stakingAddress: address,
walletAddress: walletAddress,
amount: amount,
signature: signature,
timestamp: new Date(),
});
} catch (error) {
console.error("Error recording transfer:", error);
}
}
function getAddressesFromFile(fileName) {
try {
const fileData = fs.readFileSync(fileName, 'utf8');
const jsonData = JSON.parse(fileData);
return jsonData.addresses;
} catch (error) {
console.error('Error reading addresses from file:', error);
return null;
}
}
async function main() {
try {
const connection = new Connection("https://mainnet.koii.network");
// const taskData = await getTaskStateInfo(
// connection,
// "GKvoaVSNAfV96nGui8EmhWcVpg38n6s545isjHDUQoVF" // Task ID
// );
// const stakeList = taskData.stake_list;
// const addresses = Object.keys(stakeList);
// Get addresses from file
const addresses = getAddressesFromFile('stakeList_2025-02-07T21-36-23-065Z.json');
if (!addresses) {
console.error('Failed to load addresses from file');
return;
}
console.log(`Loaded ${addresses.length} addresses from file`);
// console.log(addresses);
// Store addresses in a JSON file (If needed)
// const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
// const fileName = `stakeList_${timestamp}.json`;
// const dataToStore = {
// taskId: "GKvoaVSNAfV96nGui8EmhWcVpg38n6s545isjHDUQoVF",
// timestamp: new Date().toISOString(),
// addresses: addresses,
// stakeList: stakeList // Including full stake list data for reference
// };
// fs.writeFileSync(
// fileName,
// JSON.stringify(dataToStore, null, 2)
// );
// console.log(`Stored ${addresses.length} addresses in ${fileName}`);
// Process transfers in batches of 10
const batchSize = 10;
for (let i = 0; i < addresses.length; i += batchSize) {
const batch = addresses.slice(i, i + batchSize);
const transferPromises = batch.map(async (address) => {
try {
const checkTransferred = await hasRoundTransferred(
"GKvoaVSNAfV96nGui8EmhWcVpg38n6s545isjHDUQoVF",
address
);
if (checkTransferred) {
console.log("Already sent KOII to ", address);
return null;
}
const walletAddress = await getStakingAccountInfo(address);
if (!walletAddress) {
console.log(`No transactions found for ${address}`);
return null;
}
console.log("Processing:", address, "walletAddress:", walletAddress);
const amount = 100;
const transferResult = await transferKOII(
connection,
walletAddress,
amount
);
if (transferResult && transferResult.success) {
console.log(`Transferred ${amount} KOII to ${walletAddress}`);
await recordTransfer(
"GKvoaVSNAfV96nGui8EmhWcVpg38n6s545isjHDUQoVF",
address,
walletAddress,
amount,
transferResult.signature
);
return transferResult;
}
console.log(`Failed to transfer to ${walletAddress}`);
return null;
} catch (error) {
console.error(`Error processing address ${address}:`, error);
return null;
}
});
// Wait for the current batch to complete before moving to the next
await Promise.all(transferPromises);
console.log(`Completed batch of ${batch.length} transfers`);
// Optional: Add a small delay between batches to prevent rate limiting
await new Promise(resolve => setTimeout(resolve, 1000));
}
} catch (error) {
console.error("Error in main function:", error);
} finally {
// Close MongoDB connection when done
if (mongoClient) {
await mongoClient.close();
mongoClient = null;
}
}
}
// Handle process termination gracefully
process.on('SIGINT', async () => {
console.log('\nGracefully shutting down...');
if (mongoClient) {
await mongoClient.close();
mongoClient = null;
}
process.exit(0);
});
main();