-
Notifications
You must be signed in to change notification settings - Fork 1
/
mintTo.ts
67 lines (56 loc) · 2.45 KB
/
mintTo.ts
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
import {
sendAndConfirmTransaction,
Transaction,
PublicKey,
} from '@solana/web3.js';
import {
TOKEN_2022_PROGRAM_ID,
getAssociatedTokenAddress,
createAssociatedTokenAccountInstruction,
createMintToCheckedInstruction
} from '@solana/spl-token';
import * as fs from "fs";
import { connection, payerKeypair } from './config';
async function mintToken2022To() {
const tokenInfo: { mint: string, decimals: number } = JSON.parse(fs.readFileSync("tokenInfo.json", { encoding: "utf-8" }));
const mint = new PublicKey(tokenInfo.mint);
const decimals = tokenInfo.decimals;
const receivers: { address: string, amount: number }[] = JSON.parse(fs.readFileSync("receivers.json", { encoding: "utf-8" }));
const nbReceiversPerTx = 5;
// calculate the total number of transactions to do
let nbTx: number;
if (receivers.length % nbReceiversPerTx == 0) {
nbTx = receivers.length / nbReceiversPerTx;
} else {
nbTx = Math.floor(receivers.length / nbReceiversPerTx) + 1;
}
console.log("Minting started...")
// for each transaction
for (let i = 0; i < nbTx; i++) {
let bornSup: number;
if (i == nbTx - 1) {
bornSup = receivers.length;
} else {
bornSup = nbReceiversPerTx * (i + 1);
}
const transaction = new Transaction();
// we add the instructions for each receiver
for (let j = nbReceiversPerTx * i; j < bornSup; j++) {
const receiverPK = new PublicKey(receivers[j].address);
const amount = receivers[j].amount;
const receiverTokenAccount = await getAssociatedTokenAddress(mint, receiverPK, undefined, TOKEN_2022_PROGRAM_ID);
const info = await connection.getAccountInfo(receiverTokenAccount);
if (info == null) {
transaction.add(
createAssociatedTokenAccountInstruction(payerKeypair.publicKey, receiverTokenAccount, receiverPK, mint, TOKEN_2022_PROGRAM_ID))
}
transaction.add(
createMintToCheckedInstruction(mint, receiverTokenAccount, payerKeypair.publicKey, amount * 10 ** decimals, decimals, [], TOKEN_2022_PROGRAM_ID)
);
}
const signature = await sendAndConfirmTransaction(connection, transaction, [payerKeypair], { skipPreflight: true });
console.log("Mint transaction signature: ", signature);
}
console.log("Minting finished!")
}
mintToken2022To()