-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_token_mint.ts
More file actions
130 lines (105 loc) · 3.5 KB
/
create_token_mint.ts
File metadata and controls
130 lines (105 loc) · 3.5 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
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
import { createMint,} from "@solana/spl-token";
import "dotenv/config";
import {
getKeypairFromEnvironment,
getExplorerLink,
} from "@solana-developers/helpers";
import { Connection, PublicKey, Transaction, clusterApiUrl, sendAndConfirmTransaction } from "@solana/web3.js";
import { createCreateMetadataAccountV3Instruction } from "@metaplex-foundation/mpl-token-metadata";
const connection = new Connection(clusterApiUrl("devnet"));
const user = getKeypairFromEnvironment("SECRET_KEY");
console.log(
`🔑 Loaded our keypair securely, using an env file! Our public key is: ${user.publicKey.toBase58()}`,
);
const TOKEN_METADATA_PROGRAM_ID = new PublicKey(
"metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s",
);
const tokenMintAccount = await createMint(connection, user, user.publicKey, user.publicKey, 8);
const metadataData = {
name: "SolVent",
symbol: "SLVNT",
// Arweave / IPFS / Pinata etc link using metaplex standard for offchain data
uri: "https://arweave.net/1234",
sellerFeeBasisPoints: 0,
creators: null,
collection: null,
uses: null,
};
const metadataPDAAndBump = PublicKey.findProgramAddressSync(
[
Buffer.from("metadata"),
TOKEN_METADATA_PROGRAM_ID.toBuffer(),
tokenMintAccount.toBuffer(),
],
TOKEN_METADATA_PROGRAM_ID,
);
const metadataPDA = metadataPDAAndBump[0];
const transaction = new Transaction();
const createMetadataAccountInstruction =
createCreateMetadataAccountV3Instruction(
{
metadata: metadataPDA,
mint: tokenMintAccount,
mintAuthority: user.publicKey,
payer: user.publicKey,
updateAuthority: user.publicKey,
},
{
createMetadataAccountArgsV3: {
collectionDetails: null,
data: metadataData,
isMutable: true,
},
},
);
transaction.add(createMetadataAccountInstruction);
const transactionSignature = await sendAndConfirmTransaction(
connection,
transaction,
[user],
);
const transactionLink = getExplorerLink(
"transaction",
transactionSignature,
"devnet",
);
console.log(`✅ Transaction confirmed, explorer link is: ${transactionLink}`);
const tokenMintLink = getExplorerLink(
"address",
tokenMintAccount.toString(),
"devnet",
);
console.log(`✅ Look at the token mint: ${tokenMintLink}`);
console.log(
`🎉 Minted a new token mint with address: ${tokenMintAccount.toBase58()}`,
);
import {
getOrCreateAssociatedTokenAccount,
mintTo,
} from "@solana/spl-token";
// Replace this with the recipient’s public key (could be your own or someone else’s)
const recipientPublicKey = user.publicKey; // or new PublicKey("RECIPIENT_PUBLIC_KEY_HERE")
// Get or create the recipient’s associated token account (ATA)
const recipientTokenAccount = await getOrCreateAssociatedTokenAccount(
connection,
user, // payer
tokenMintAccount, // mint
recipientPublicKey, // owner of the token account
);
console.log(`📦 Recipient ATA: ${recipientTokenAccount.address.toBase58()}`);
// Mint tokens to the recipient’s ATA
const amountToMint = 1000n * 10n ** 8n; // 1000 tokens with 8 decimals
const mintTxSignature = await mintTo(
connection,
user, // payer
tokenMintAccount, // mint
recipientTokenAccount.address, // destination ATA
user, // authority (mintAuthority)
amountToMint, // amount (as bigint)
);
const mintTxLink = getExplorerLink(
"transaction",
mintTxSignature,
"devnet",
);
console.log(`✅ Minted ${Number(amountToMint) / 10 ** 8} tokens! Explorer link: ${mintTxLink}`);