-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtransferKPL.js
135 lines (116 loc) · 4.58 KB
/
transferKPL.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
133
134
135
const transferKPL = require("./helpers/transferKPL");
const getStakingKey = require("./helpers/getStakingKey");
const getStakingAccountInfo = require("./helpers/getStakingAccountInfo");
const getUserMainWallet = require("./helpers/getUserMainWallet");
const { MongoClient } = require("mongodb");
const cron = require("node-cron"); // Add node-cron
const moment = require("moment");
require("dotenv").config();
const uri = process.env.DB_KEY;
const client = new MongoClient(uri, { useUnifiedTopology: true });
const dbName = "tweets_middleman";
const collectionName = "userKPLtransfer";
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
// Function to check if the target wallet and mint token exist in the database
async function checkExistingTransfer(mintToken, targetWallet, client) {
const collection = client.db(dbName).collection(collectionName);
const existingTransfer = await collection.findOne({
targetWallet: targetWallet,
[`mintToken.${mintToken}`]: { $exists: true },
});
return existingTransfer != null;
}
// Function to insert transfer details into MongoDB
async function insertTransferRecord(mintToken, targetWallet, amount, client) {
const collection = client.db(dbName).collection(collectionName);
const timestamp = new Date();
// Update the document if targetWallet exists, else insert a new document
await collection.updateOne(
{ targetWallet: targetWallet }, // Filter by targetWallet
{
$set: { timestamp: timestamp }, // Set/Update timestamp
$inc: { [`mintToken.${mintToken}`]: amount }, // Increment amount for the mintToken
},
{ upsert: true } // Insert a new document if it doesn't exist
);
}
async function main() {
const mintToken = "Fe7878UvoGHUM7B8C95rX3MigUqtBZmtcPcX5sz3Qhxd"; // BIRD
const targetWalletList = [];
await client.connect();
// Task ID here(FTT)
const stakingList = await getStakingKey(
"Aqr6jKpv7spkZ8TpLpEsLF5jvjNeHpaHaHPtkt4UTkn6"
);
// Populate targetWalletList with the addresses from stakingList
for (let walletAddress of Object.keys(stakingList)) {
targetWalletList.push(walletAddress);
}
console.log("Target Wallet Length:", targetWalletList.length);
try {
for (let i = 0; i < targetWalletList.length; i++) {
let walletAddress = await getStakingAccountInfo(targetWalletList[i]);
if (!walletAddress) {
// console.log(`No transactions found for ${targetWalletList[i]}`);
try {
walletAddress = await getUserMainWallet(targetWalletList[i]);
if (!walletAddress) {
// console.log(`Main wallet not found for ${targetWalletList[i]}`);
continue;
}
} catch (error) {
console.error("Error fetching main wallet:", error);
continue;
}
}
// const walletAddress = targetWalletList[i];
const amount = 10; // Adjust the amount as needed
// Check if this wallet and mint token already exists in the database
const exists = await checkExistingTransfer(
mintToken,
walletAddress,
client
);
if (exists) {
console.log(
`Transfer to ${walletAddress} with ${mintToken} already exists, skipping...`
);
continue;
}
// Perform the transfer
const signature = await transferKPL(mintToken, walletAddress, amount);
// Add the transfer details to MongoDB
if (signature) {
console.log(`Transferred ${amount} KPL to ${walletAddress}`);
await insertTransferRecord(mintToken, walletAddress, amount, client);
console.log(
`Inserted transfer record for ${walletAddress} into MongoDB`
);
}
// Delay for 0.5 seconds
await delay(200);
}
} finally {
await client.close(); // Always ensure that the client is closed
}
}
// Function to calculate time until next job
function timeUntilNextJob(hour, minute) {
const now = moment();
const nextJob = moment().hour(hour).minute(minute).second(0);
// If the next job time is earlier in the day than now, schedule it for tomorrow
if (now.isAfter(nextJob)) {
nextJob.add(1, 'days');
}
const duration = moment.duration(nextJob.diff(now));
const hours = Math.floor(duration.asHours());
const minutes = Math.floor(duration.minutes());
console.log(`Next job will start in ${hours} hours and ${minutes} minutes.`);
}
// Check how long until the job starts at 20:00 (8 PM)
// timeUntilNextJob(20, 0);
// Schedule the job to run every day at midnight
// cron.schedule("0 20 * * *", async () => {
// console.log("Running the transfer script at noon");
main();
// });