-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreate-spl-mint.ts
More file actions
78 lines (68 loc) · 2.13 KB
/
create-spl-mint.ts
File metadata and controls
78 lines (68 loc) · 2.13 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
import "dotenv/config";
import {
Keypair,
SystemProgram,
Transaction,
sendAndConfirmTransaction,
} from "@solana/web3.js";
import { createRpc } from "@lightprotocol/stateless.js";
import { LightTokenProgram } from "@lightprotocol/compressed-token";
import {
MINT_SIZE,
TOKEN_PROGRAM_ID,
createInitializeMint2Instruction,
} from "@solana/spl-token";
import { homedir } from "os";
import { readFileSync } from "fs";
// devnet:
// const RPC_URL = `https://devnet.helius-rpc.com?api-key=${process.env.API_KEY!}`;
// const rpc = createRpc(RPC_URL);
// localnet:
const rpc = createRpc();
const payer = Keypair.fromSecretKey(
new Uint8Array(
JSON.parse(readFileSync(`${homedir()}/.config/solana/id.json`, "utf8"))
)
);
(async function () {
const mintKeypair = Keypair.generate();
const decimals = 9;
// Get rent for mint account
const rentExemptBalance = await rpc.getMinimumBalanceForRentExemption(
MINT_SIZE
);
// Instruction 1: Create mint account
const createMintAccountIx = SystemProgram.createAccount({
fromPubkey: payer.publicKey,
lamports: rentExemptBalance,
newAccountPubkey: mintKeypair.publicKey,
programId: TOKEN_PROGRAM_ID,
space: MINT_SIZE,
});
// Instruction 2: Initialize mint
const initializeMintIx = createInitializeMint2Instruction(
mintKeypair.publicKey,
decimals,
payer.publicKey, // mint authority
null, // freeze authority
TOKEN_PROGRAM_ID
);
// Instruction 3: Create SPL interface PDA
// Holds SPL tokens when wrapped to light-token
const createSplInterfaceIx = await LightTokenProgram.createSplInterface({
feePayer: payer.publicKey,
mint: mintKeypair.publicKey,
tokenProgramId: TOKEN_PROGRAM_ID,
});
const tx = new Transaction().add(
createMintAccountIx,
initializeMintIx,
createSplInterfaceIx
);
const signature = await sendAndConfirmTransaction(rpc, tx, [
payer,
mintKeypair,
]);
console.log("Mint:", mintKeypair.publicKey.toBase58());
console.log("Tx:", signature);
})();