UNPKG

@leda-mint-io/candymachine-client-sdk

Version:

Metaplex Candy Machine Client SDK

220 lines (219 loc) 11 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.mintOneNft = exports.createAccountsForMint = exports.getCollectionPDA = void 0; const anchor_1 = require("@j0nnyboi/anchor"); const web3_js_1 = require("@safecoin/web3.js"); const constants_1 = require("../constants"); const helpers_1 = require("./helpers"); const safe_token_1 = require("@safecoin/safe-token"); const transactions_1 = require("../upload/transactions"); /** * Get the collection PDA from a Candy Machine Account. * @param candyMachineAddress The address of the candy machine. * @returns The collection PDA from a candy Machine */ const getCollectionPDA = (candyMachineAddress) => __awaiter(void 0, void 0, void 0, function* () { return yield anchor_1.web3.PublicKey.findProgramAddress([Buffer.from("collection"), candyMachineAddress.toBuffer()], constants_1.CANDY_MACHINE_PROGRAM_V2_ID); }); exports.getCollectionPDA = getCollectionPDA; /** * Get the collection autrhority record PDA. * @param mint The mint to get the creator of. * @param newAuthority The new authority to set. * @returns The collection autrhority record PDA. */ const getCollectionAuthorityRecordPDA = (mint, newAuthority) => __awaiter(void 0, void 0, void 0, function* () { return (yield anchor_1.web3.PublicKey.findProgramAddress([ Buffer.from("metadata"), constants_1.TOKEN_METADATA_PROGRAM_ID.toBuffer(), mint.toBuffer(), Buffer.from("collection_authority"), newAuthority.toBuffer(), ], constants_1.TOKEN_METADATA_PROGRAM_ID))[0]; }); /** * Create the associated token account for a mint. * @param candyMachine The candy machine to get the mint of. * @param payer The payer to pay for the transaction. * @returns The associated token account for a mint. */ const createAccountsForMint = (candyMachine, payer) => __awaiter(void 0, void 0, void 0, function* () { const mint = anchor_1.web3.Keypair.generate(); const userTokenAccountAddress = (yield (0, helpers_1.getAtaForMint)(mint.publicKey, payer))[0]; const signers = [mint]; if (!candyMachine) return; const instructions = [ anchor_1.web3.SystemProgram.createAccount({ fromPubkey: payer, newAccountPubkey: mint.publicKey, space: safe_token_1.MintLayout.span, lamports: yield candyMachine.program.provider.connection.getMinimumBalanceForRentExemption(safe_token_1.MintLayout.span), programId: constants_1.TOKEN_PROGRAM_ID, }), (0, safe_token_1.createInitializeMintInstruction)(mint.publicKey, 0, payer, payer, constants_1.TOKEN_PROGRAM_ID), (0, safe_token_1.createAssociatedTokenAccountInstruction)(payer, userTokenAccountAddress, payer, mint.publicKey), (0, safe_token_1.createMintToInstruction)(mint.publicKey, userTokenAccountAddress, payer, 1, [], constants_1.TOKEN_PROGRAM_ID), ]; const sendTx = yield (0, transactions_1.sendTransactions)(candyMachine.program.provider.connection, // @ts-ignore candyMachine.program.provider.wallet, [instructions], [signers], transactions_1.SequenceType.StopOnFailure, "singleGossip", () => { }, () => false, undefined, [], []); console.log(sendTx); const txid = sendTx.txs[0].txid; return { mint: mint, userTokenAccount: userTokenAccountAddress, transaction: txid, }; }); exports.createAccountsForMint = createAccountsForMint; /** * Attempt to mint one nft. * @param candyMachine The candy machine to mint to. * @param payer The payer to pay for the transaction. * @param beforeTransactions The transactions to run before minting. * @param afterTransactions The transactions to run after minting. * @param setupState The setup state to use. * @returns The mint result. */ const mintOneNft = (candyMachine, payer, beforeTransactions = [], afterTransactions = [], setupState) => __awaiter(void 0, void 0, void 0, function* () { var _a; const mint = (_a = setupState === null || setupState === void 0 ? void 0 : setupState.mint) !== null && _a !== void 0 ? _a : anchor_1.web3.Keypair.generate(); const userTokenAccountAddress = (yield (0, helpers_1.getAtaForMint)(mint.publicKey, payer))[0]; if (!candyMachine) return null; const userPayingAccountAddress = candyMachine.state.tokenMint ? (yield (0, helpers_1.getAtaForMint)(candyMachine.state.tokenMint, payer))[0] : payer; const candyMachineAddress = candyMachine.id; const remainingAccounts = []; const instructions = []; const signers = []; if (!setupState) { signers.push(mint); instructions.push(...[ anchor_1.web3.SystemProgram.createAccount({ fromPubkey: payer, newAccountPubkey: mint.publicKey, space: safe_token_1.MintLayout.span, lamports: yield candyMachine.program.provider.connection.getMinimumBalanceForRentExemption(safe_token_1.MintLayout.span), programId: constants_1.TOKEN_PROGRAM_ID, }), (0, safe_token_1.createInitializeMintInstruction)(mint.publicKey, 0, payer, payer, constants_1.TOKEN_PROGRAM_ID), (0, safe_token_1.createAssociatedTokenAccountInstruction)(payer, userTokenAccountAddress, payer, mint.publicKey), (0, safe_token_1.createMintToInstruction)(mint.publicKey, userTokenAccountAddress, payer, 1, [], constants_1.TOKEN_PROGRAM_ID), ]); } if (candyMachine.state.whitelistMintSettings) { const mint = new anchor_1.web3.PublicKey(candyMachine.state.whitelistMintSettings.mint); const whitelistToken = (yield (0, helpers_1.getAtaForMint)(mint, payer))[0]; remainingAccounts.push({ pubkey: whitelistToken, isWritable: true, isSigner: false, }); if (candyMachine.state.whitelistMintSettings.mode.burnEveryTime) { remainingAccounts.push({ pubkey: mint, isWritable: true, isSigner: false, }); remainingAccounts.push({ pubkey: payer, isWritable: false, isSigner: true, }); } } if (candyMachine.state.tokenMint) { remainingAccounts.push({ pubkey: userPayingAccountAddress, isWritable: true, isSigner: false, }); remainingAccounts.push({ pubkey: payer, isWritable: false, isSigner: true, }); } const metadataAddress = yield (0, helpers_1.getMetadata)(mint.publicKey); const masterEdition = yield (0, helpers_1.getMasterEdition)(mint.publicKey); const [candyMachineCreator, creatorBump] = yield (0, helpers_1.getCandyMachineCreator)(candyMachineAddress); instructions.push(yield candyMachine.program.instruction.mintNft(creatorBump, { accounts: { candyMachine: candyMachineAddress, candyMachineCreator, payer: payer, wallet: candyMachine.state.treasury, mint: mint.publicKey, metadata: metadataAddress, masterEdition, mintAuthority: payer, updateAuthority: payer, tokenMetadataProgram: constants_1.TOKEN_METADATA_PROGRAM_ID, tokenProgram: constants_1.TOKEN_PROGRAM_ID, systemProgram: web3_js_1.SystemProgram.programId, rent: anchor_1.web3.SYSVAR_RENT_PUBKEY, clock: anchor_1.web3.SYSVAR_CLOCK_PUBKEY, recentBlockhashes: web3_js_1.SYSVAR_SLOT_HASHES_PUBKEY, instructionSysvarAccount: anchor_1.web3.SYSVAR_INSTRUCTIONS_PUBKEY, }, remainingAccounts: remainingAccounts.length > 0 ? remainingAccounts : undefined, })); const [collectionPDA] = yield (0, exports.getCollectionPDA)(candyMachineAddress); const collectionPDAAccount = yield candyMachine.program.provider.connection.getAccountInfo(collectionPDA); if (collectionPDAAccount && candyMachine.state.retainAuthority) { try { const collectionData = (yield candyMachine.program.account.collectionPda.fetch(collectionPDA)); const collectionMint = collectionData.mint; const collectionAuthorityRecord = yield getCollectionAuthorityRecordPDA(collectionMint, collectionPDA); if (collectionMint) { const collectionMetadata = yield (0, helpers_1.getMetadata)(collectionMint); const collectionMasterEdition = yield (0, helpers_1.getMasterEdition)(collectionMint); instructions.push(yield candyMachine.program.instruction.setCollectionDuringMint({ accounts: { candyMachine: candyMachineAddress, metadata: metadataAddress, payer: payer, collectionPda: collectionPDA, tokenMetadataProgram: constants_1.TOKEN_METADATA_PROGRAM_ID, instructions: anchor_1.web3.SYSVAR_INSTRUCTIONS_PUBKEY, collectionMint, collectionMetadata, collectionMasterEdition, authority: candyMachine.state.authority, collectionAuthorityRecord, }, })); } } catch (error) { console.error(error); } } try { const txns = (yield (0, transactions_1.sendTransactions)(candyMachine.program.provider.connection, // @ts-ignore candyMachine.program.provider.wallet, [instructions], [signers], transactions_1.SequenceType.StopOnFailure, "singleGossip", () => { }, () => false, undefined, beforeTransactions, afterTransactions)).txs.map((t) => t.txid); const mintTxn = txns[0]; return { mintTxId: mintTxn, metadataKey: metadataAddress, }; } catch (e) { console.log(e); } return null; }); exports.mintOneNft = mintOneNft;