-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmint.ts
187 lines (173 loc) · 5.21 KB
/
mint.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
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
import {
Transaction,
SystemProgram,
Keypair,
Connection,
PublicKey,
} from '@solana/web3.js';
import {
MINT_SIZE,
TOKEN_PROGRAM_ID,
createInitializeMintInstruction,
getMinimumBalanceForRentExemptMint,
getAssociatedTokenAddress,
createAssociatedTokenAccountInstruction,
createMintToInstruction,
} from '@solana/spl-token';
import {
DataV2,
createCreateMetadataAccountV2Instruction,
} from '@metaplex-foundation/mpl-token-metadata';
import {
bundlrStorage,
findMetadataPda,
keypairIdentity,
Metaplex,
UploadMetadataInput,
} from '@metaplex-foundation/js';
import secret from './guideSecret.json';
import tokenData from './tokenData.json';
import config from './config.json';
const endpoint = config.network; //Replace with your RPC Endpoint
const solanaConnection = new Connection(endpoint);
const MINT_CONFIG = {
numDecimals: 9,
numberTokens: 10000,
};
const MY_TOKEN_METADATA: UploadMetadataInput = {
name: tokenData.name,
symbol: tokenData.symbol,
description: tokenData.description,
image: tokenData.image, //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;
/**
*
* @param wallet Solana Keypair
* @param tokenMetadata Metaplex Fungible Token Standard object
* @returns Arweave url for our metadata json file
*/
const uploadMetadata = async (
wallet: Keypair,
tokenMetadata: UploadMetadataInput
): Promise<string> => {
//create metaplex instance on devnet using this wallet
const metaplex = Metaplex.make(solanaConnection)
.use(keypairIdentity(wallet))
.use(
bundlrStorage({
address: 'https://devnet.bundlr.network',
providerUrl: endpoint,
timeout: 60000,
})
);
//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 findMetadataPda(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
),
createCreateMetadataAccountV2Instruction(
{
metadata: metadataPDA,
mint: mintKeypair.publicKey,
mintAuthority: mintAuthority,
payer: payer.publicKey,
updateAuthority: mintAuthority,
},
{
createMetadataAccountArgsV2: {
data: ON_CHAIN_METADATA,
isMutable: true,
},
}
)
);
return createNewTokenTransaction;
};
const main = async () => {
console.log(`---STEP 1: Uploading MetaData---`);
const userWallet = Keypair.fromSecretKey(new Uint8Array(secret));
let metadataUri = await uploadMetadata(userWallet, 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---`);
const transactionId = await solanaConnection.sendTransaction(
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();