-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathKPL_send_out_tokens_on_mainnet.js
161 lines (140 loc) · 4.59 KB
/
KPL_send_out_tokens_on_mainnet.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
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
import { Connection, PublicKey } from "@_koii/web3.js";
import { getOrCreateAssociatedTokenAccount, mintTo } from "@solana/spl-token";
import { MongoClient } from "mongodb";
import { Keypair } from "@_koii/web3.js";
import dotenv from "dotenv";
dotenv.config();
const privateKeyString = process.env.PRIVATE_KEY;
const privateKeyArray = privateKeyString.split(",").map(Number);
const senderKeypair = Keypair.fromSecretKey(Uint8Array.from(privateKeyArray));
/***********DO NOT EDIT ABOVE THIS LINE***********/
const oldMintAddress = "FJG2aEPtertCXoedgteCCMmgngSZo1Zd715oNBzR7xpR";
const newMintAddress = "7gWY3DyG9CWii9UhC8y27HFexH2yYWeYJKfJ6sofs46W";
const connection = new Connection("https://mainnet.koii.network", "confirmed");
/***********DO NOT EDIT BELOW THIS LINE***********/
const BATCH_SIZE = 10;
const client = new MongoClient(process.env.MONGODB_URL);
async function readFromMongoDB(collectionName) {
const db = client.db("Migration");
const collection = db.collection(`KPL_${collectionName}`);
const query = { status: { $nin: ["Success", "Unknown"] } };
const results = await collection.find(query).toArray();
return results;
}
async function updateTransactionResult(
collectionName,
query,
{ status, signature },
) {
const db = client.db("Migration");
const collection = db.collection(`KPL_${collectionName}`);
await collection.updateOne(query, { $set: { status, signature } });
}
async function createTokenAccount(mintAddress, toAddress) {
try {
const tokenAccount = await getOrCreateAssociatedTokenAccount(
connection,
senderKeypair,
new PublicKey(mintAddress),
new PublicKey(toAddress),
);
return tokenAccount;
} catch (error) {
console.log("ERROR INFO FOR CREATE TOKEN ACCOUNT", mintAddress, toAddress);
console.log(error);
return null;
}
}
async function sendToken(mintAddress, amount, toAddress) {
if (amount == 0) {
return { status: "Success", signature: null };
}
try {
const tokenAccount = await getOrCreateAssociatedTokenAccount(
connection,
senderKeypair,
new PublicKey(mintAddress),
new PublicKey(toAddress),
);
const signature = await mintTo(
connection,
senderKeypair,
new PublicKey(mintAddress),
tokenAccount.address,
senderKeypair,
amount,
);
return { status: "Success", signature: signature };
} catch (error) {
const signatureMatch = error.message.match(/signature (\w+)/);
const signature = signatureMatch
? signatureMatch[1]
: "Signature not found";
if (signature == "Signature not found") {
console.log(error);
return { status: "Failed", signature: null };
}
let waitTime = 1000;
for (let i = 0; i < 9; i++) {
const result = await connection.getSignatureStatus(signature);
if (result?.value?.err != null) {
return { status: "Failed", signature: signature };
}
if (
result?.value?.confirmationStatus == "finalized" ||
result?.value?.confirmationStatus == "confirmed"
) {
console.log("Transaction is Confirmed!");
return { status: "Success", signature: signature };
}
if (result?.value?.confirmationStatus == "processed") {
console.log("Transaction Processing.");
await new Promise((resolve) => setTimeout(resolve, waitTime));
waitTime *= 2;
continue;
}
await new Promise((resolve) => setTimeout(resolve, waitTime));
waitTime *= 2;
}
return { status: "Unknown", signature: signature };
}
}
async function processBatch(batch) {
const createTokenPromises = batch.map((response) =>
createTokenAccount(newMintAddress, response.owner),
);
await Promise.all(createTokenPromises);
const sendTokenPromises = batch.map(async (response, index) => {
if (
response.status &&
(response.status == "Success" || response.status == "Unknown")
) {
console.log("Skipping", response.owner);
return null;
}
const { status, signature } = await sendToken(
newMintAddress,
response.balance,
response.owner,
);
console.log(status, signature);
await updateTransactionResult(
oldMintAddress,
{ _id: response._id },
{ status, signature },
);
return { status, signature };
});
await Promise.all(sendTokenPromises);
}
async function main() {
const responses = await readFromMongoDB(oldMintAddress);
for (let i = 0; i < responses.length; i += BATCH_SIZE) {
const batch = responses.slice(i, i + BATCH_SIZE);
await processBatch(batch);
}
}
main();
process.on('exit', () => {
client.close();
});