-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmint.ts
277 lines (257 loc) · 7.73 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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import * as dotenv from "dotenv";
import {
ComputeBudgetProgram,
AccountMeta,
Keypair,
PublicKey,
sendAndConfirmRawTransaction,
SYSVAR_CLOCK_PUBKEY,
SYSVAR_INSTRUCTIONS_PUBKEY,
SYSVAR_RECENT_BLOCKHASHES_PUBKEY,
Transaction,
Cluster,
} from "@solana/web3.js";
import {
createMintNftInstruction,
PROGRAM_ID,
CandyMachine,
findLockupSettingsId,
findPermissionedSettingsId,
remainingAccountsForPermissioned,
createSetCollectionDuringMintInstruction,
findCcsSettingsId,
remainingAccountsForCcs,
CCSSettings,
} from "@cardinal/mpl-candy-machine-utils";
import {
ASSOCIATED_TOKEN_PROGRAM_ID,
Token,
TOKEN_PROGRAM_ID,
} from "@solana/spl-token";
import {
Edition,
MasterEdition,
Metadata,
MetadataProgram,
} from "@metaplex-foundation/mpl-token-metadata";
import { remainingAccountsForLockup } from "@cardinal/mpl-candy-machine-utils";
import { findAta } from "@cardinal/token-manager";
import { connectionFor } from "./connection";
import { keypairFrom } from "./utils";
import { Wallet } from "@project-serum/anchor";
dotenv.config();
const walletKeypair = keypairFrom(process.env.WALLET_KEYPAIR, "Wallet");
const payerKeypair = process.env.PAYER_KEYPAIR
? keypairFrom(process.env.PAYER_KEYPAIR, "Payer")
: walletKeypair;
const candyMachineId = new PublicKey(process.env.CANDY_MACHINE_ID || "");
let collectionMintKeypair: Keypair | null = null;
const cluster = "mainnet";
export const mint = async (
wallet: Wallet,
candyMachineId: PublicKey,
cluster: Cluster | "mainnet" | "localnet",
payerWallet?: Wallet
) => {
const connection = connectionFor(cluster);
const payerId = payerWallet?.publicKey ?? wallet.publicKey;
const nftToMintKeypair = Keypair.generate();
const tokenAccountToReceive = await Token.getAssociatedTokenAddress(
ASSOCIATED_TOKEN_PROGRAM_ID,
TOKEN_PROGRAM_ID,
nftToMintKeypair.publicKey,
wallet.publicKey,
false
);
const metadataId = await Metadata.getPDA(nftToMintKeypair.publicKey);
const masterEditionId = await Edition.getPDA(nftToMintKeypair.publicKey);
const [candyMachineCreatorId, candyMachineCreatorIdBump] =
await PublicKey.findProgramAddress(
[Buffer.from("candy_machine"), candyMachineId.toBuffer()],
PROGRAM_ID
);
const candyMachine = await CandyMachine.fromAccountAddress(
connection,
candyMachineId
);
console.log(`> Creating mint instruction`);
const mintIx = createMintNftInstruction(
{
candyMachine: candyMachineId,
candyMachineCreator: candyMachineCreatorId,
payer: payerId,
wallet: candyMachine.wallet,
metadata: metadataId,
mint: nftToMintKeypair.publicKey,
mintAuthority: wallet.publicKey,
updateAuthority: wallet.publicKey,
masterEdition: masterEditionId,
tokenMetadataProgram: MetadataProgram.PUBKEY,
clock: SYSVAR_CLOCK_PUBKEY,
recentBlockhashes: SYSVAR_RECENT_BLOCKHASHES_PUBKEY,
instructionSysvarAccount: SYSVAR_INSTRUCTIONS_PUBKEY,
},
{
creatorBump: candyMachineCreatorIdBump,
}
);
const remainingAccounts: AccountMeta[] = [];
// Payment
if (candyMachine.tokenMint) {
console.log(`> Add payment accounts`);
const payerTokenAccount = await findAta(
candyMachine.tokenMint,
payerId,
true
);
remainingAccounts.push(
{
pubkey: payerTokenAccount,
isWritable: true,
isSigner: false,
},
{
pubkey: payerId,
isWritable: true,
isSigner: false,
}
);
}
// Inline minting
console.log(`> Adding mint accounts`);
remainingAccounts.push({
pubkey: tokenAccountToReceive,
isSigner: false,
isWritable: true,
});
// Lockup settings
const [lockupSettingsId] = await findLockupSettingsId(candyMachineId);
const lockupSettings = await connection.getAccountInfo(lockupSettingsId);
if (lockupSettings) {
console.log(`> Adding lockup settings accounts`);
remainingAccounts.push(
...(await remainingAccountsForLockup(
candyMachineId,
nftToMintKeypair.publicKey,
tokenAccountToReceive
))
);
}
// Permissioned settings
const [permissionedSettingsId] = await findPermissionedSettingsId(
candyMachineId
);
const permissionedSettings = await connection.getAccountInfo(
permissionedSettingsId
);
if (permissionedSettings) {
console.log(`> Adding permissioned settings accounts`);
remainingAccounts.push(
...(await remainingAccountsForPermissioned(
candyMachineId,
nftToMintKeypair.publicKey,
tokenAccountToReceive
))
);
}
// CSS settings
const [cssSettingsId] = await findCcsSettingsId(candyMachineId);
const cssSettings = await connection.getAccountInfo(cssSettingsId);
if (cssSettings) {
const ccsSettingsData = await CCSSettings.fromAccountAddress(
connection,
cssSettingsId
);
console.log(`> Adding css settings accounts`);
remainingAccounts.push(
...(await remainingAccountsForCcs(
connection,
wallet,
candyMachineId,
ccsSettingsData.creator,
nftToMintKeypair.publicKey,
tokenAccountToReceive,
""
))
);
}
// Minting
const instructions = [
ComputeBudgetProgram.requestUnits({
units: 400000,
additionalFee: 0,
}),
{
...mintIx,
keys: [
...mintIx.keys.map((k) =>
k.pubkey.equals(nftToMintKeypair.publicKey)
? { ...k, isSigner: true }
: k
),
// remaining accounts for locking
...remainingAccounts,
],
},
];
// Collections
if (collectionMintKeypair) {
const [collectionPdaId, _collectionPdaBump] =
await PublicKey.findProgramAddress(
[Buffer.from("collection"), candyMachineId.toBuffer()],
PROGRAM_ID
);
const collectionMintMetadataId = await Metadata.getPDA(
collectionMintKeypair.publicKey
);
const collectionMasterEditionId = await MasterEdition.getPDA(
collectionMintKeypair.publicKey
);
const [collectionAuthorityRecordId] = await PublicKey.findProgramAddress(
[
Buffer.from("metadata"),
MetadataProgram.PUBKEY.toBuffer(),
collectionMintKeypair.publicKey.toBuffer(),
Buffer.from("collection_authority"),
collectionPdaId.toBuffer(),
],
MetadataProgram.PUBKEY
);
instructions.push(
createSetCollectionDuringMintInstruction({
candyMachine: candyMachineId,
metadata: metadataId,
payer: walletKeypair.publicKey,
collectionPda: collectionPdaId,
tokenMetadataProgram: MetadataProgram.PUBKEY,
instructions: SYSVAR_INSTRUCTIONS_PUBKEY,
collectionMint: collectionMintKeypair.publicKey,
collectionMasterEdition: collectionMasterEditionId,
collectionMetadata: collectionMintMetadataId,
authority: walletKeypair.publicKey,
collectionAuthorityRecord: collectionAuthorityRecordId,
})
);
}
const tx = new Transaction();
tx.instructions = instructions;
tx.feePayer = payerId;
tx.recentBlockhash = (await connection.getRecentBlockhash()).blockhash;
await wallet.signTransaction(tx);
payerWallet && (await payerWallet.signTransaction(tx));
await tx.partialSign(nftToMintKeypair);
const txid = await sendAndConfirmRawTransaction(connection, tx.serialize());
console.log(
`Succesfully minted token ${nftToMintKeypair.publicKey.toString()} from candy machine with address ${candyMachineId.toString()} https://explorer.solana.com/tx/${txid}?cluster=${cluster}`
);
return txid;
};
const main = async () => {
await mint(
new Wallet(walletKeypair),
candyMachineId,
cluster,
new Wallet(payerKeypair)
).then((d) => console.log(`Ouput: `, d));
};
main().catch((e) => console.log(`[error]`, e));