-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathKPLsender.js
112 lines (100 loc) · 3.66 KB
/
KPLsender.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
const { Connection, PublicKey } = require("@_koii/web3.js");
const transferKPL = require("./helpers/transferKPL");
const { getTaskStateInfo } = require("@_koii/create-task-cli");
const getStakingAccountInfo = require("./helpers/getStakingAccountInfo");
const { MongoClient } = require("mongodb");
require("dotenv").config();
const uri = process.env.DB_KEY;
const DB_name = "mainnet_airdrop_middleman";
const collection_name = "kplsender";
const client = new MongoClient(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
async function hasRoundTransferred(address) {
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({ address: address });
// console.log(transfer)
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, kplToken, address) {
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: kplToken,
address: address, // An array of objects with { address, amount }
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 {
const KPLMintAddress = "CJLjkLohs3PScpBQ3r1WKHjQsWYPq291MgPjpTWTXNc4"; // ASLINK
const connection = new Connection("https://mainnet.koii.network");
const taskData = await getTaskStateInfo(
connection,
"2XUizbiDdG2PUWHX48vFzp7ekqJ2NpgCvtGtHHpDiHtS" // Task ID
);
const stakeList = taskData.stake_list;
const addresses = Object.keys(stakeList);
// Transfer KPL to all addresses
for (const address of addresses) {
let checkTransferred = await hasRoundTransferred(address);
if (!checkTransferred) {
let walletAddress = await getStakingAccountInfo(address);
if (!walletAddress) {
// console.log(`No transactions found for ${address}`);
try {
// walletAddress = await getUserMainWallet(address);
if (!walletAddress) {
// console.log(`Main wallet not found for ${address}`);
continue;
}
} catch (error) {
console.error("Error fetching main wallet:", error);
continue;
}
}
const amount = 1; // Convert amount to correct KPL
const transferResult = await transferKPL(
KPLMintAddress,
walletAddress,
amount
);
if (transferResult) {
console.log(`Transferred ${amount} KPL to ${address}`);
// Record the transfer after processing all addresses
await recordTransfer(0, KPLMintAddress, address);
// Record the transfer after processing all addresses
// await recordTransfer(taskData.maxRound, KPLMintAddress, addresses);
} else {
console.log(`Failed to transfer to ${address}`);
}
} else {
console.log("Already sent KPL to ", address);
}
}
} catch (error) {
console.error("Error in main function:", error);
}
}
main();