-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
241 lines (212 loc) · 7.44 KB
/
index.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import {
Connection,
Keypair,
PublicKey,
SystemProgram,
Transaction,
clusterApiUrl,
sendAndConfirmTransaction,
} from "@solana/web3.js";
import {
ExtensionType,
TOKEN_2022_PROGRAM_ID,
createInitializeMintInstruction,
getMintLen,
createInitializeMetadataPointerInstruction,
getMint,
getMetadataPointerState,
getTokenMetadata,
TYPE_SIZE,
LENGTH_SIZE,
getOrCreateAssociatedTokenAccount,
mintToChecked,
transferChecked,
} from "@solana/spl-token";
import {
createInitializeInstruction,
createUpdateFieldInstruction,
pack,
} from "@solana/spl-token-metadata";
import "dotenv/config";
import { getKeypairFromEnvironment } from "@solana-developers/helpers";
// Payer wallet
const payer = getKeypairFromEnvironment("ADMIN_ACCOUNT");
// Connection to devnet cluster
const connection = new Connection(clusterApiUrl("devnet"), "confirmed");
// Transaction to send
let transaction;
// Transaction signature returned from sent transaction
let transactionSignature;
// // Generate new keypair for Mint Account
// const mintKeypair = Keypair.generate();
// // const mintKeypair = getKeypairFromEnvironment("MINT_ACCOUNT");
// console.log("mintKeypairs", mintKeypair.publicKey, mintKeypair.secretKey);
// // Address for Mint Account
// const mint = mintKeypair.publicKey;
// // Decimals for Mint Account
// const decimals = 9;
// // Authority that can mint new tokens
// const mintAuthority = payer.publicKey;
// // Authority that can update token metadata
// const updateAuthority = payer.publicKey;
// // Metadata to store in Mint Account
// const metaData = {
// updateAuthority: updateAuthority,
// mint: mint,
// name: "Bottle Test Tokens",
// symbol: "BTT",
// // uri: "https://raw.githubusercontent.com/solana-developers/opos-asset/main/assets/DeveloperPortal/metadata.json",
// additionalMetadata: [["description", "Testnet random tokens"]],
// };
// // Size of MetadataExtension 2 bytes for type, 2 bytes for length
// const metadataExtension = TYPE_SIZE + LENGTH_SIZE;
// // Size of metadata
// const metadataLen = pack(metaData).length;
// // Size of Mint Account with extension
// const mintLen = getMintLen([ExtensionType.MetadataPointer]);
// // Minimum lamports required for Mint Account
// const lamports = await connection.getMinimumBalanceForRentExemption(
// mintLen + metadataExtension + metadataLen,
// );
// // Instruction to invoke System Program to create new account
// const createAccountInstruction = SystemProgram.createAccount({
// fromPubkey: payer.publicKey, // Account that will transfer lamports to created account
// newAccountPubkey: mint, // Address of the account to create
// space: mintLen, // Amount of bytes to allocate to the created account
// lamports, // Amount of lamports transferred to created account
// programId: TOKEN_2022_PROGRAM_ID, // Program assigned as owner of created account
// });
// // Instruction to initialize the MetadataPointer Extension
// const initializeMetadataPointerInstruction =
// createInitializeMetadataPointerInstruction(
// mint, // Mint Account address
// updateAuthority, // Authority that can set the metadata address
// mint, // Account address that holds the metadata
// TOKEN_2022_PROGRAM_ID,
// );
// // Instruction to initialize Mint Account data
// const initializeMintInstruction = createInitializeMintInstruction(
// mint, // Mint Account Address
// decimals, // Decimals of Mint
// mintAuthority, // Designated Mint Authority
// null, // Optional Freeze Authority
// TOKEN_2022_PROGRAM_ID, // Token Extension Program ID
// );
// // Instruction to initialize Metadata Account data
// const initializeMetadataInstruction = createInitializeInstruction({
// programId: TOKEN_2022_PROGRAM_ID, // Token Extension Program as Metadata Program
// metadata: mint, // Account address that holds the metadata
// updateAuthority: updateAuthority, // Authority that can update the metadata
// mint: mint, // Mint Account address
// mintAuthority: mintAuthority, // Designated Mint Authority
// name: metaData.name,
// symbol: metaData.symbol,
// uri: metaData.uri,
// });
// // Instruction to update metadata, adding custom field
// const updateFieldInstruction = createUpdateFieldInstruction({
// programId: TOKEN_2022_PROGRAM_ID, // Token Extension Program as Metadata Program
// metadata: mint, // Account address that holds the metadata
// updateAuthority: updateAuthority, // Authority that can update the metadata
// field: metaData.additionalMetadata[0][0], // key
// value: metaData.additionalMetadata[0][1], // value
// });
// // Add instructions to new transaction
// transaction = new Transaction().add(
// createAccountInstruction,
// initializeMetadataPointerInstruction,
// initializeMintInstruction,
// initializeMetadataInstruction,
// updateFieldInstruction,
// );
// // Send transaction
// transactionSignature = await sendAndConfirmTransaction(
// connection,
// transaction,
// [payer, mintKeypair], // Signers
// );
// console.log(
// "\nCreate Mint Account:",
// `https://solana.fm/tx/${transactionSignature}?cluster=devnet-solana`,
// );
// // Retrieve mint information
// const mintInfo = await getMint(
// connection,
// mint,
// "confirmed",
// TOKEN_2022_PROGRAM_ID,
// );
// // Retrieve and log the metadata pointer state
// const metadataPointer = getMetadataPointerState(mintInfo);
// console.log("\nMetadata Pointer:", JSON.stringify(metadataPointer, null, 2));
// // Retrieve and log the metadata state
// const metadata = await getTokenMetadata(
// connection,
// mint, // Mint Account address
// );
// console.log("\nMetadata:", JSON.stringify(metadata, null, 2));
// // Retrieve and log the metadata state
// const updatedMetadata = await getTokenMetadata(
// connection,
// mint, // Mint Account address
// );
// console.log("\nUpdated Metadata:", JSON.stringify(updatedMetadata, null, 2));
// console.log(
// "\nMint Account:",
// `https://solana.fm/address/${mint}?cluster=devnet-solana`,
// );
// // Create a ATA for tokenss to be minted
// const ata = await getOrCreateAssociatedTokenAccount(
// connection,
// payer,
// mintKeypair.publicKey,
// mintKeypair.publicKey,
// false,
// "confirmed",
// {},
// TOKEN_2022_PROGRAM_ID,
// );
// console.log(`ata : ${ata.address.toString()} ATA of ${ata.owner.toString()}`);
// // MINT TOKENS
// const mintTxnHash = await mintToChecked(
// connection,
// payer,
// mintKeypair.publicKey,
// ata.address,
// mintAuthority,
// 100e9,
// decimals,
// undefined,
// { commitment: "confirmed", skipPreflight: true },
// TOKEN_2022_PROGRAM_ID,
// );
// console.log(`Mint tokens txn hash ${mintTxnHash}`);
// Local ATA to mint tokens to
const localATA = await getOrCreateAssociatedTokenAccount(
connection,
payer,
mintKeypair.publicKey,
new PublicKey("2AAJJ71aca6ugqpMYbStFgbxfmhx1mH3iwmzmzkdDkFM"),
false,
"confirmed",
{},
TOKEN_2022_PROGRAM_ID,
);
console.log(
`localATA : ${localATA.address.toString()} ATA of 2AAJJ71aca6ugqpMYbStFgbxfmhx1mH3iwmzmzkdDkFM`,
);
// Transfer tokens
let transferHash = await transferChecked(
connection, // connection
payer, // payer
ata.address, // from (should be a token account)
mint, // mint
localATA.address, // to (should be a token account)
mintKeypair, // from's owner
10e9, // amount, if your deciamls is 8, send 10^8 for 1 token
9, // decimals
[],
{ commitment: "confirmed", skipPreflight: true },
TOKEN_2022_PROGRAM_ID,
);
console.log(`txhash: ${transferHash}`);