-
Notifications
You must be signed in to change notification settings - Fork 77
/
token.ts
130 lines (117 loc) · 5.93 KB
/
token.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
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 { Transaction, SystemProgram, Keypair, Connection, PublicKey, sendAndConfirmTransaction } from "@solana/web3.js";
import { MINT_SIZE, TOKEN_PROGRAM_ID, createInitializeMintInstruction, getMinimumBalanceForRentExemptMint, getAssociatedTokenAddress, createAssociatedTokenAccountInstruction, createMintToInstruction } from '@solana/spl-token';
import { DataV2, createCreateMetadataAccountV3Instruction } from '@metaplex-foundation/mpl-token-metadata';
import { bundlrStorage, keypairIdentity, Metaplex, UploadMetadataInput } from '@metaplex-foundation/js';
import secret from './wallet.json';
const endpoint = 'https://api.devnet.solana.com/'
const solanaConnection = new Connection(endpoint);
const userWallet = Keypair.fromSecretKey(new Uint8Array(secret));
const metaplex = Metaplex.make(solanaConnection)
.use(keypairIdentity(userWallet))
.use(bundlrStorage({
address: 'https://devnet.bundlr.network',
providerUrl: endpoint,
timeout: 60000,
}));
const MINT_CONFIG = {
numDecimals: 2,
numberTokens: 100
}
const MY_TOKEN_METADATA: UploadMetadataInput = {
name: "Campus Connect", //name of your token
symbol: "SCC", //symbol of your token
description: "Solana Campus Connect", //description of your token
image: "https://i.ibb.co/PtRMT08/avatar-orange.jpg" //add public URL to image you'd like to use
}
const ON_CHAIN_METADATA = {
name: MY_TOKEN_METADATA.name,
symbol: MY_TOKEN_METADATA.symbol,
uri: 'TO_UPDATE_LATER',
sellerFeeBasisPoints: 0,
creators: null,
collection: null,
uses: null
} as DataV2;
const uploadMetadata = async (tokenMetadata: UploadMetadataInput): Promise<string> => {
//Upload to Arweave
const { uri } = await metaplex.nfts().uploadMetadata(tokenMetadata);
console.log(`Arweave URL: `, uri);
return uri;
}
const createNewMintTransaction = async (connection: Connection, payer: Keypair, mintKeypair: Keypair, destinationWallet: PublicKey, mintAuthority: PublicKey, freezeAuthority: PublicKey) => {
//Get the minimum lamport balance to create a new account and avoid rent payments
const requiredBalance = await getMinimumBalanceForRentExemptMint(connection);
//metadata account associated with mint
const metadataPDA = await metaplex.nfts().pdas().metadata({ mint: mintKeypair.publicKey });
//get associated token account of your wallet
const tokenATA = await getAssociatedTokenAddress(mintKeypair.publicKey, destinationWallet);
const createNewTokenTransaction = new Transaction().add(
SystemProgram.createAccount({
fromPubkey: payer.publicKey,
newAccountPubkey: mintKeypair.publicKey,
space: MINT_SIZE,
lamports: requiredBalance,
programId: TOKEN_PROGRAM_ID,
}),
createInitializeMintInstruction(
mintKeypair.publicKey, //Mint Address
MINT_CONFIG.numDecimals, //Number of Decimals of New mint
mintAuthority, //Mint Authority
freezeAuthority, //Freeze Authority
TOKEN_PROGRAM_ID),
createAssociatedTokenAccountInstruction(
payer.publicKey, //Payer
tokenATA, //Associated token account
payer.publicKey, //token owner
mintKeypair.publicKey, //Mint
),
createMintToInstruction(
mintKeypair.publicKey, //Mint
tokenATA, //Destination Token Account
mintAuthority, //Authority
MINT_CONFIG.numberTokens * Math.pow(10, MINT_CONFIG.numDecimals),//number of tokens
),
createCreateMetadataAccountV3Instruction({
metadata: metadataPDA,
mint: mintKeypair.publicKey,
mintAuthority: mintAuthority,
payer: payer.publicKey,
updateAuthority: mintAuthority,
}, {
createMetadataAccountArgsV3: {
data: ON_CHAIN_METADATA,
isMutable: true,
collectionDetails: null
}
})
);
return createNewTokenTransaction;
}
const main = async () => {
console.log(`---STEP 1: Uploading MetaData---`);
const userWallet = Keypair.fromSecretKey(new Uint8Array(secret));
let metadataUri = await uploadMetadata(MY_TOKEN_METADATA);
ON_CHAIN_METADATA.uri = metadataUri;
console.log(`---STEP 2: Creating Mint Transaction---`);
let mintKeypair = Keypair.generate();
console.log(`New Mint Address: `, mintKeypair.publicKey.toString());
const newMintTransaction: Transaction = await createNewMintTransaction(
solanaConnection,
userWallet,
mintKeypair,
userWallet.publicKey,
userWallet.publicKey,
userWallet.publicKey
);
console.log(`---STEP 3: Executing Mint Transaction---`);
let { lastValidBlockHeight, blockhash } = await solanaConnection.getLatestBlockhash('finalized');
newMintTransaction.recentBlockhash = blockhash;
newMintTransaction.lastValidBlockHeight = lastValidBlockHeight;
newMintTransaction.feePayer = userWallet.publicKey;
const transactionId = await sendAndConfirmTransaction(solanaConnection, newMintTransaction, [userWallet, mintKeypair]);
console.log(`Transaction ID: `, transactionId);
console.log(`Succesfully minted ${MINT_CONFIG.numberTokens} ${ON_CHAIN_METADATA.symbol} to ${userWallet.publicKey.toString()}.`);
console.log(`View Transaction: https://explorer.solana.com/tx/${transactionId}?cluster=devnet`);
console.log(`View Token Mint: https://explorer.solana.com/address/${mintKeypair.publicKey.toString()}?cluster=devnet`)
}
main();