From b216a71a65e8407be4682a3d06187981ba136d47 Mon Sep 17 00:00:00 2001 From: Arnau Espin <43625217+aspnxdd@users.noreply.github.com> Date: Wed, 17 Aug 2022 13:17:14 +0200 Subject: [PATCH] docs: add documentation to sdk functions (#2) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: add jsdocs to the exported functions * Update src/lib/cache/actions.ts Co-authored-by: Begoña Álvarez de la Cruz * Update src/lib/mint/mint.ts Co-authored-by: Begoña Álvarez de la Cruz * Update src/lib/mint/mint.ts Co-authored-by: Begoña Álvarez de la Cruz * Update src/lib/update.ts Co-authored-by: Begoña Álvarez de la Cruz * Update src/lib/upload/arweave.ts Co-authored-by: Begoña Álvarez de la Cruz * Update src/lib/upload/transactions.ts Co-authored-by: Begoña Álvarez de la Cruz * Update src/lib/upload/transactions.ts Co-authored-by: Begoña Álvarez de la Cruz * Update src/lib/upload/transactions.ts Co-authored-by: Begoña Álvarez de la Cruz * Update src/lib/upload/transactions.ts Co-authored-by: Begoña Álvarez de la Cruz * Update src/lib/upload/upload.ts Co-authored-by: Begoña Álvarez de la Cruz * Update src/lib/upload/config.ts Co-authored-by: Begoña Álvarez de la Cruz * Update src/lib/upload/config.ts Co-authored-by: Begoña Álvarez de la Cruz * Update src/lib/upload/helpers.ts Co-authored-by: Begoña Álvarez de la Cruz * Update src/lib/upload/helpers.ts Co-authored-by: Begoña Álvarez de la Cruz * Update src/lib/upload/helpers.ts Co-authored-by: Begoña Álvarez de la Cruz * Update src/lib/upload/transactions.ts Co-authored-by: Begoña Álvarez de la Cruz Co-authored-by: Begoña Álvarez de la Cruz --- src/lib/cache/actions.ts | 7 + src/lib/mint/helpers.ts | 108 +++++-- src/lib/mint/mint.ts | 554 +++++++++++++++++++-------------- src/lib/update.ts | 13 + src/lib/upload/arweave.ts | 12 +- src/lib/upload/config.ts | 15 +- src/lib/upload/helpers.ts | 23 +- src/lib/upload/transactions.ts | 41 ++- src/lib/upload/upload.ts | 5 + 9 files changed, 506 insertions(+), 272 deletions(-) diff --git a/src/lib/cache/actions.ts b/src/lib/cache/actions.ts index 126dbf4..26bf4fa 100644 --- a/src/lib/cache/actions.ts +++ b/src/lib/cache/actions.ts @@ -29,6 +29,13 @@ import { ICache } from './interfaces' // return JSON.parse(fs.readFileSync(path).toString()); // } +/** + * Save the cache to the cache path. + * @param cacheName The name of the cache to save. + * @param env The environment used 'mainnet-beta' | 'devnet' | 'testnet' + * @param cacheContent The content of the cache to save. + */ + export function saveCache(cacheName: string, env: string, cacheContent: ICache) { cacheContent.env = env cacheContent.cacheName = cacheName diff --git a/src/lib/mint/helpers.ts b/src/lib/mint/helpers.ts index 68eca9a..fff4def 100644 --- a/src/lib/mint/helpers.ts +++ b/src/lib/mint/helpers.ts @@ -1,39 +1,81 @@ -import { web3 } from '@project-serum/anchor' +import { web3 } from "@project-serum/anchor"; import { - CANDY_MACHINE_PROGRAM_V2_ID, - SPL_ASSOCIATED_TOKEN_ACCOUNT_PROGRAM_ID, - TOKEN_METADATA_PROGRAM_ID, - TOKEN_PROGRAM_ID, -} from '../constants' + CANDY_MACHINE_PROGRAM_V2_ID, + SPL_ASSOCIATED_TOKEN_ACCOUNT_PROGRAM_ID, + TOKEN_METADATA_PROGRAM_ID, + TOKEN_PROGRAM_ID, +} from "../constants"; -export const getAtaForMint = async (mint: web3.PublicKey, buyer: web3.PublicKey): Promise<[web3.PublicKey, number]> => { - return await web3.PublicKey.findProgramAddress( - [buyer.toBuffer(), TOKEN_PROGRAM_ID.toBuffer(), mint.toBuffer()], - SPL_ASSOCIATED_TOKEN_ACCOUNT_PROGRAM_ID - ) -} +/** + * Get the associated token account from a mint and the owner. + * + * @param mint The mint to get the associated token account of. + * @param buyer The owner of the associated token account. + * @returns The associated token account + */ +export const getAtaForMint = async ( + mint: web3.PublicKey, + buyer: web3.PublicKey +): Promise<[web3.PublicKey, number]> => { + return await web3.PublicKey.findProgramAddress( + [buyer.toBuffer(), TOKEN_PROGRAM_ID.toBuffer(), mint.toBuffer()], + SPL_ASSOCIATED_TOKEN_ACCOUNT_PROGRAM_ID + ); +}; -export const getMetadata = async (mint: web3.PublicKey): Promise => { - return ( - await web3.PublicKey.findProgramAddress( - [Buffer.from('metadata'), TOKEN_METADATA_PROGRAM_ID.toBuffer(), mint.toBuffer()], - TOKEN_METADATA_PROGRAM_ID - ) - )[0] -} +/** + * Get the mint metadata. + * + * @param mint The mint to get the metadata of. + * @returns The mint metadata. + * */ -export const getMasterEdition = async (mint: web3.PublicKey): Promise => { - return ( - await web3.PublicKey.findProgramAddress( - [Buffer.from('metadata'), TOKEN_METADATA_PROGRAM_ID.toBuffer(), mint.toBuffer(), Buffer.from('edition')], - TOKEN_METADATA_PROGRAM_ID - ) - )[0] -} +export const getMetadata = async ( + mint: web3.PublicKey +): Promise => { + return ( + await web3.PublicKey.findProgramAddress( + [ + Buffer.from("metadata"), + TOKEN_METADATA_PROGRAM_ID.toBuffer(), + mint.toBuffer(), + ], + TOKEN_METADATA_PROGRAM_ID + ) + )[0]; +}; -export const getCandyMachineCreator = async (candyMachine: web3.PublicKey): Promise<[web3.PublicKey, number]> => { - return await web3.PublicKey.findProgramAddress( - [Buffer.from('candy_machine'), candyMachine.toBuffer()], - CANDY_MACHINE_PROGRAM_V2_ID +/** + * Get the mint edition. + * @param mint The mint to get the edition of. + * @returns The mint edition. + * + **/ +export const getMasterEdition = async ( + mint: web3.PublicKey +): Promise => { + return ( + await web3.PublicKey.findProgramAddress( + [ + Buffer.from("metadata"), + TOKEN_METADATA_PROGRAM_ID.toBuffer(), + mint.toBuffer(), + Buffer.from("edition"), + ], + TOKEN_METADATA_PROGRAM_ID ) -} + )[0]; +}; +/** + * Get the creator of a candy machine. + * @param candyMachine The candy machine to get the mint of. + * @returns The creator of the Candy Machine. + */ +export const getCandyMachineCreator = async ( + candyMachine: web3.PublicKey +): Promise<[web3.PublicKey, number]> => { + return await web3.PublicKey.findProgramAddress( + [Buffer.from("candy_machine"), candyMachine.toBuffer()], + CANDY_MACHINE_PROGRAM_V2_ID + ); +}; diff --git a/src/lib/mint/mint.ts b/src/lib/mint/mint.ts index 0215432..d6b0567 100644 --- a/src/lib/mint/mint.ts +++ b/src/lib/mint/mint.ts @@ -1,260 +1,354 @@ -import { web3 } from '@project-serum/anchor' -import { Transaction, SYSVAR_SLOT_HASHES_PUBKEY, SystemProgram } from '@solana/web3.js' +import { web3 } from "@project-serum/anchor"; import { - CANDY_MACHINE_PROGRAM_V2_ID, - TOKEN_METADATA_PROGRAM_ID, - TOKEN_PROGRAM_ID, -} from '../constants' -import { getAtaForMint, getCandyMachineCreator, getMasterEdition, getMetadata } from './helpers' -import { SetupState, CandyMachineAccount } from '../interfaces' + Transaction, + SYSVAR_SLOT_HASHES_PUBKEY, + SystemProgram, +} from "@solana/web3.js"; import { - createInitializeMintInstruction, - createMintToInstruction, - createAssociatedTokenAccountInstruction, - MintLayout, -} from '@solana/spl-token' -import { sendTransactions, SequenceType } from '../upload/transactions' + CANDY_MACHINE_PROGRAM_V2_ID, + TOKEN_METADATA_PROGRAM_ID, + TOKEN_PROGRAM_ID, +} from "../constants"; +import { + getAtaForMint, + getCandyMachineCreator, + getMasterEdition, + getMetadata, +} from "./helpers"; +import { SetupState, CandyMachineAccount } from "../interfaces"; +import { + createInitializeMintInstruction, + createMintToInstruction, + createAssociatedTokenAccountInstruction, + MintLayout, +} from "@solana/spl-token"; +import { sendTransactions, SequenceType } from "../upload/transactions"; -export const getCollectionPDA = async (candyMachineAddress: web3.PublicKey): Promise<[web3.PublicKey, number]> => { - return await web3.PublicKey.findProgramAddress( - [Buffer.from('collection'), candyMachineAddress.toBuffer()], - CANDY_MACHINE_PROGRAM_V2_ID - ) -} +/** + * 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 + */ +export const getCollectionPDA = async ( + candyMachineAddress: web3.PublicKey +): Promise<[web3.PublicKey, number]> => { + return await web3.PublicKey.findProgramAddress( + [Buffer.from("collection"), candyMachineAddress.toBuffer()], + CANDY_MACHINE_PROGRAM_V2_ID + ); +}; + +/** + * 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 = async ( - mint: web3.PublicKey, - newAuthority: web3.PublicKey + mint: web3.PublicKey, + newAuthority: web3.PublicKey ): Promise => { - return ( - await web3.PublicKey.findProgramAddress( - [ - Buffer.from('metadata'), - TOKEN_METADATA_PROGRAM_ID.toBuffer(), - mint.toBuffer(), - Buffer.from('collection_authority'), - newAuthority.toBuffer(), - ], - TOKEN_METADATA_PROGRAM_ID - ) - )[0] -} - + return ( + await web3.PublicKey.findProgramAddress( + [ + Buffer.from("metadata"), + TOKEN_METADATA_PROGRAM_ID.toBuffer(), + mint.toBuffer(), + Buffer.from("collection_authority"), + newAuthority.toBuffer(), + ], + 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. + */ export const createAccountsForMint = async ( - candyMachine: CandyMachineAccount | undefined, - payer: web3.PublicKey + candyMachine: CandyMachineAccount | undefined, + payer: web3.PublicKey ): Promise => { - const mint = web3.Keypair.generate() - const userTokenAccountAddress = (await getAtaForMint(mint.publicKey, payer))[0] - const signers: web3.Keypair[] = [mint] + const mint = web3.Keypair.generate(); + const userTokenAccountAddress = ( + await getAtaForMint(mint.publicKey, payer) + )[0]; + const signers: web3.Keypair[] = [mint]; - if (!candyMachine) return + if (!candyMachine) return; - const instructions = [ - web3.SystemProgram.createAccount({ - fromPubkey: payer, - newAccountPubkey: mint.publicKey, - space: MintLayout.span, - lamports: await candyMachine.program.provider.connection.getMinimumBalanceForRentExemption(MintLayout.span), - programId: TOKEN_PROGRAM_ID, - }), - createInitializeMintInstruction(mint.publicKey, 0, payer, payer, TOKEN_PROGRAM_ID), - createAssociatedTokenAccountInstruction(payer, userTokenAccountAddress, payer, mint.publicKey), - createMintToInstruction(mint.publicKey, userTokenAccountAddress, payer, 1, [], TOKEN_PROGRAM_ID), - ] + const instructions = [ + web3.SystemProgram.createAccount({ + fromPubkey: payer, + newAccountPubkey: mint.publicKey, + space: MintLayout.span, + lamports: + await candyMachine.program.provider.connection.getMinimumBalanceForRentExemption( + MintLayout.span + ), + programId: TOKEN_PROGRAM_ID, + }), + createInitializeMintInstruction( + mint.publicKey, + 0, + payer, + payer, + TOKEN_PROGRAM_ID + ), + createAssociatedTokenAccountInstruction( + payer, + userTokenAccountAddress, + payer, + mint.publicKey + ), + createMintToInstruction( + mint.publicKey, + userTokenAccountAddress, + payer, + 1, + [], + TOKEN_PROGRAM_ID + ), + ]; - const sendTx = await sendTransactions( - candyMachine.program.provider.connection, - // @ts-ignore - candyMachine.program.provider.wallet, - [instructions], - [signers], - SequenceType.StopOnFailure, - 'singleGossip', - () => {}, - () => false, - undefined, - [], - [] - ) - console.log(sendTx) + const sendTx = await sendTransactions( + candyMachine.program.provider.connection, + // @ts-ignore + candyMachine.program.provider.wallet, + [instructions], + [signers], + SequenceType.StopOnFailure, + "singleGossip", + () => {}, + () => false, + undefined, + [], + [] + ); + console.log(sendTx); - const txid = sendTx.txs[0].txid + const txid = sendTx.txs[0].txid; - return { - mint: mint, - userTokenAccount: userTokenAccountAddress, - transaction: txid, - } -} + return { + mint: mint, + userTokenAccount: userTokenAccountAddress, + transaction: txid, + }; +}; type MintResult = { - mintTxId: string - metadataKey: web3.PublicKey -} - + mintTxId: string; + metadataKey: web3.PublicKey; +}; +/** + * 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. + */ export const mintOneNft = async ( - candyMachine: CandyMachineAccount | undefined, - payer: web3.PublicKey, - beforeTransactions: Transaction[] = [], - afterTransactions: Transaction[] = [], - setupState?: SetupState + candyMachine: CandyMachineAccount | undefined, + payer: web3.PublicKey, + beforeTransactions: Transaction[] = [], + afterTransactions: Transaction[] = [], + setupState?: SetupState ): Promise => { - const mint = setupState?.mint ?? web3.Keypair.generate() - const userTokenAccountAddress = (await getAtaForMint(mint.publicKey, payer))[0] + const mint = setupState?.mint ?? web3.Keypair.generate(); + const userTokenAccountAddress = ( + await getAtaForMint(mint.publicKey, payer) + )[0]; - if (!candyMachine) return null - const userPayingAccountAddress = candyMachine.state.tokenMint - ? (await getAtaForMint(candyMachine.state.tokenMint, payer))[0] - : payer - - const candyMachineAddress = candyMachine.id - const remainingAccounts = [] - const instructions = [] - const signers: web3.Keypair[] = [] - if (!setupState) { - signers.push(mint) - instructions.push( - ...[ - web3.SystemProgram.createAccount({ - fromPubkey: payer, - newAccountPubkey: mint.publicKey, - space: MintLayout.span, - lamports: await candyMachine.program.provider.connection.getMinimumBalanceForRentExemption( - MintLayout.span - ), - programId: TOKEN_PROGRAM_ID, - }), - createInitializeMintInstruction(mint.publicKey, 0, payer, payer, TOKEN_PROGRAM_ID), - createAssociatedTokenAccountInstruction(payer, userTokenAccountAddress, payer, mint.publicKey), - createMintToInstruction(mint.publicKey, userTokenAccountAddress, payer, 1, [], TOKEN_PROGRAM_ID), - ] - ) - } + if (!candyMachine) return null; + const userPayingAccountAddress = candyMachine.state.tokenMint + ? (await getAtaForMint(candyMachine.state.tokenMint, payer))[0] + : payer; - if (candyMachine.state.whitelistMintSettings) { - const mint = new web3.PublicKey(candyMachine.state.whitelistMintSettings.mint) + const candyMachineAddress = candyMachine.id; + const remainingAccounts = []; + const instructions = []; + const signers: web3.Keypair[] = []; + if (!setupState) { + signers.push(mint); + instructions.push( + ...[ + web3.SystemProgram.createAccount({ + fromPubkey: payer, + newAccountPubkey: mint.publicKey, + space: MintLayout.span, + lamports: + await candyMachine.program.provider.connection.getMinimumBalanceForRentExemption( + MintLayout.span + ), + programId: TOKEN_PROGRAM_ID, + }), + createInitializeMintInstruction( + mint.publicKey, + 0, + payer, + payer, + TOKEN_PROGRAM_ID + ), + createAssociatedTokenAccountInstruction( + payer, + userTokenAccountAddress, + payer, + mint.publicKey + ), + createMintToInstruction( + mint.publicKey, + userTokenAccountAddress, + payer, + 1, + [], + TOKEN_PROGRAM_ID + ), + ] + ); + } - const whitelistToken = (await getAtaForMint(mint, payer))[0] - remainingAccounts.push({ - pubkey: whitelistToken, - isWritable: true, - isSigner: false, - }) + if (candyMachine.state.whitelistMintSettings) { + const mint = new web3.PublicKey( + candyMachine.state.whitelistMintSettings.mint + ); - if (candyMachine.state.whitelistMintSettings.mode.burnEveryTime) { - remainingAccounts.push({ - pubkey: mint, - isWritable: true, - isSigner: false, - }) - remainingAccounts.push({ - pubkey: payer, - isWritable: false, - isSigner: true, - }) - } - } + const whitelistToken = (await getAtaForMint(mint, payer))[0]; + remainingAccounts.push({ + pubkey: whitelistToken, + isWritable: true, + isSigner: false, + }); - if (candyMachine.state.tokenMint) { - remainingAccounts.push({ - pubkey: userPayingAccountAddress, - isWritable: true, - isSigner: false, - }) - remainingAccounts.push({ - pubkey: payer, - isWritable: false, - isSigner: true, - }) + if (candyMachine.state.whitelistMintSettings.mode.burnEveryTime) { + remainingAccounts.push({ + pubkey: mint, + isWritable: true, + isSigner: false, + }); + remainingAccounts.push({ + pubkey: payer, + isWritable: false, + isSigner: true, + }); } - const metadataAddress = await getMetadata(mint.publicKey) - const masterEdition = await getMasterEdition(mint.publicKey) + } - const [candyMachineCreator, creatorBump] = await getCandyMachineCreator(candyMachineAddress) + if (candyMachine.state.tokenMint) { + remainingAccounts.push({ + pubkey: userPayingAccountAddress, + isWritable: true, + isSigner: false, + }); + remainingAccounts.push({ + pubkey: payer, + isWritable: false, + isSigner: true, + }); + } + const metadataAddress = await getMetadata(mint.publicKey); + const masterEdition = await getMasterEdition(mint.publicKey); - instructions.push( - await 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: TOKEN_METADATA_PROGRAM_ID, - tokenProgram: TOKEN_PROGRAM_ID, - systemProgram: SystemProgram.programId, - rent: web3.SYSVAR_RENT_PUBKEY, - clock: web3.SYSVAR_CLOCK_PUBKEY, - recentBlockhashes: SYSVAR_SLOT_HASHES_PUBKEY, - instructionSysvarAccount: web3.SYSVAR_INSTRUCTIONS_PUBKEY, - }, - remainingAccounts: remainingAccounts.length > 0 ? remainingAccounts : undefined, - }) - ) + const [candyMachineCreator, creatorBump] = await getCandyMachineCreator( + candyMachineAddress + ); - const [collectionPDA] = await getCollectionPDA(candyMachineAddress) - const collectionPDAAccount = await candyMachine.program.provider.connection.getAccountInfo(collectionPDA) + instructions.push( + await 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: TOKEN_METADATA_PROGRAM_ID, + tokenProgram: TOKEN_PROGRAM_ID, + systemProgram: SystemProgram.programId, + rent: web3.SYSVAR_RENT_PUBKEY, + clock: web3.SYSVAR_CLOCK_PUBKEY, + recentBlockhashes: SYSVAR_SLOT_HASHES_PUBKEY, + instructionSysvarAccount: web3.SYSVAR_INSTRUCTIONS_PUBKEY, + }, + remainingAccounts: + remainingAccounts.length > 0 ? remainingAccounts : undefined, + }) + ); - if (collectionPDAAccount && candyMachine.state.retainAuthority) { - try { - const collectionData = (await candyMachine.program.account.collectionPda.fetch(collectionPDA)) as any - const collectionMint = collectionData.mint - const collectionAuthorityRecord = await getCollectionAuthorityRecordPDA(collectionMint, collectionPDA) - if (collectionMint) { - const collectionMetadata = await getMetadata(collectionMint) - const collectionMasterEdition = await getMasterEdition(collectionMint) - instructions.push( - await candyMachine.program.instruction.setCollectionDuringMint({ - accounts: { - candyMachine: candyMachineAddress, - metadata: metadataAddress, - payer: payer, - collectionPda: collectionPDA, - tokenMetadataProgram: TOKEN_METADATA_PROGRAM_ID, - instructions: web3.SYSVAR_INSTRUCTIONS_PUBKEY, - collectionMint, - collectionMetadata, - collectionMasterEdition, - authority: candyMachine.state.authority, - collectionAuthorityRecord, - }, - }) - ) - } - } catch (error) { - console.error(error) - } - } + const [collectionPDA] = await getCollectionPDA(candyMachineAddress); + const collectionPDAAccount = + await candyMachine.program.provider.connection.getAccountInfo( + collectionPDA + ); + if (collectionPDAAccount && candyMachine.state.retainAuthority) { try { - const txns = ( - await sendTransactions( - candyMachine.program.provider.connection, - // @ts-ignore - candyMachine.program.provider.wallet, - [instructions], - [signers], - 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) + const collectionData = + (await candyMachine.program.account.collectionPda.fetch( + collectionPDA + )) as any; + const collectionMint = collectionData.mint; + const collectionAuthorityRecord = await getCollectionAuthorityRecordPDA( + collectionMint, + collectionPDA + ); + if (collectionMint) { + const collectionMetadata = await getMetadata(collectionMint); + const collectionMasterEdition = await getMasterEdition(collectionMint); + instructions.push( + await candyMachine.program.instruction.setCollectionDuringMint({ + accounts: { + candyMachine: candyMachineAddress, + metadata: metadataAddress, + payer: payer, + collectionPda: collectionPDA, + tokenMetadataProgram: TOKEN_METADATA_PROGRAM_ID, + instructions: web3.SYSVAR_INSTRUCTIONS_PUBKEY, + collectionMint, + collectionMetadata, + collectionMasterEdition, + authority: candyMachine.state.authority, + collectionAuthorityRecord, + }, + }) + ); + } + } catch (error) { + console.error(error); } - return null -} + } + + try { + const txns = ( + await sendTransactions( + candyMachine.program.provider.connection, + // @ts-ignore + candyMachine.program.provider.wallet, + [instructions], + [signers], + 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; +}; diff --git a/src/lib/update.ts b/src/lib/update.ts index 9793b82..543b84f 100644 --- a/src/lib/update.ts +++ b/src/lib/update.ts @@ -2,6 +2,19 @@ import { Program } from "@project-serum/anchor"; import { PublicKey } from "@solana/web3.js"; import { ICache, saveCache } from "./cache"; +/** + * + * Update a given candy machine with the new settings passed and cache. + * At the end of the update, the updated cache can be downloaded. + * @newSettings The new settings to update the candy machine with. + * @candyMachinePubkey The public key of the candy machine to update. + * @publicKey The public key of the authority of the candy machine + * @treasuryWallet The public key of the treasury wallet + * @anchorProgram The program that is used to update the candy machine. + * @cache The cache to update the candy machine with. + * @newAuthority (Optional) The new authority of the candy machine. + */ + export async function updateV2({ newSettings, candyMachinePubkey, diff --git a/src/lib/upload/arweave.ts b/src/lib/upload/arweave.ts index 63b9476..670bc1c 100644 --- a/src/lib/upload/arweave.ts +++ b/src/lib/upload/arweave.ts @@ -58,7 +58,17 @@ function estimateManifestSize(filenames: string[]) { console.log('Estimated manifest size:', data.length) return data.length } - +/** + * Upload the NFTs and their metadata to the Arweave network. + * @param walletKeyPair - keypair of the wallet to use for the mint transaction + * @param anchorProgram - anchor program to use for the mint transaction + * @param env - environment to use for the mint transaction (mainnet-beta, devnet, testnet) + * @param image - image to upload + * @param manifestBuffer - buffer of the manifest to upload + * @param manifest - manifest to upload + * @param index - index of the image to upload + * @returns - The links for the manifest and the image in Arweave. + */ export async function arweaveUpload( walletKeyPair: any, anchorProgram: anchor.Program, diff --git a/src/lib/upload/config.ts b/src/lib/upload/config.ts index cde7559..492d80b 100644 --- a/src/lib/upload/config.ts +++ b/src/lib/upload/config.ts @@ -12,6 +12,13 @@ import { JSON_EXTENSION } from '../constants' import { getAtaForMint } from '../mint/helpers' import { parseDate } from './helpers' +/** + * Load the candy program v2.. + * @param provider The anchor provider. + * @param customRpcUrl (Optional) The custom rpc url of the candy machine. + * @returns The IDL. + * + */ export async function loadCandyProgramV2(provider: anchor.Provider, customRpcUrl?: string) { if (customRpcUrl) console.log('USING CUSTOM URL', customRpcUrl) const idl = (await anchor.Program.fetchIdl(CANDY_MACHINE_PROGRAM_V2_ID, provider)) as anchor.Idl @@ -20,7 +27,13 @@ export async function loadCandyProgramV2(provider: anchor.Provider, customRpcUrl console.log('program id from anchor', program.programId.toBase58()) return program } - +/** + * Get the candy machine settings. + * @param walletKeyPair The wallet key pair. + * @param configForm The config form. + * @param anchorProgram The anchor program. + * @returns The settings of the candy machine. + */ export async function getCandyMachineV2Config( walletKeyPair: PublicKey, configForm: ICandyMachineConfig, diff --git a/src/lib/upload/helpers.ts b/src/lib/upload/helpers.ts index e04c1a5..3d0630f 100644 --- a/src/lib/upload/helpers.ts +++ b/src/lib/upload/helpers.ts @@ -6,7 +6,11 @@ import { CONFIG_LINE_SIZE_V2, } from '../constants' import { ICandyMachineData } from '../interfaces' - +/** + * Parse a date. + * @param date The date to parse. + * @returns The parsed date. + */ export function parseDate(date: string) { if (date === 'now') { return Date.now() / 1000 @@ -21,7 +25,14 @@ export function sleep(milliseconds: number): Promise { export function uuidFromConfigPubkey(configAccount: PublicKey) { return configAccount.toBase58().slice(0, 6) } - +/** + * Create a candy machine v2. + * @param anchorProgram The anchor program to use. + * @param payerWallet The payer wallet to use. + * @param treasuryWallet The treasury wallet to use. + * @param candyData The candy machine data to use. + * @returns The config account. + */ export const createCandyMachineV2 = async function ( anchorProgram: anchor.Program, payerWallet: any, @@ -84,6 +95,14 @@ export const createCandyMachineV2 = async function ( return cmCreation } +/** + * Create a candy machine v2 account. + * @param anchorProgram The anchor program to use. + * @param candyData The candy machine data to use. + * @param payerWallet The payer wallet to use. + * @param candyAccount The candy machine account to use. + * @returns The instruction to create the candy machine account. + */ export async function createCandyMachineV2Account( anchorProgram: anchor.Program, candyData: ICandyMachineData, diff --git a/src/lib/upload/transactions.ts b/src/lib/upload/transactions.ts index 541f1a3..6699ad8 100644 --- a/src/lib/upload/transactions.ts +++ b/src/lib/upload/transactions.ts @@ -17,7 +17,16 @@ interface IBlockhashAndFeeCalculator { blockhash: Blockhash lastValidBlockHeight: number } - +/** + * Attempt to send a transaction to the network. + * @param connection The connection to the cluster. + * @param wallet The wallet to use for signing. + * @param instructions The instructions to sign. + * @param commitment The commitment to use for the transaction. + * @param block The blockhash to use for the transaction. + * @param beforeSend The function to call before sending the transaction. + * @returns The transaction signature. + */ export const sendTransactionWithRetryWithKeypair = async ( connection: Connection, wallet: any, @@ -43,7 +52,12 @@ export const sendTransactionWithRetryWithKeypair = async ( return { txid, slot } } - +/** + * Attempt to send a signed transaction to the network. + * @param signedTransaction The signed transaction to send. + * @param connection The connection to the cluster. + * @returns The transaction id and slot. + */ export async function sendSignedTransaction({ signedTransaction, connection, @@ -118,7 +132,13 @@ export async function sendSignedTransaction({ console.log('Latency (ms)', txid, getUnixTs() - startTime) return { txid, slot } } - +/** + * Simualate a transaction. + * @param connection The connection to the cluster. + * @param transaction The transaction to simulate. + * @param commitment The commitment to use for the transaction. + * @returns The simulated transaction response. + */ async function simulateTransaction( connection: Connection, transaction: Transaction, @@ -138,7 +158,15 @@ async function simulateTransaction( } return res.result } - +/** + * Wait for a transaction to be confirmed. + * @param txid The transaction id to await confirmation for. + * @param timeout The timeout in milliseconds. + * @param connection The connection to the cluster. + * @param commitment The commitment to use for the transaction. + * @param queryStatus Whether to query the status of the transaction. + * @returns The transaction signature. + */ export async function awaitTransactionSignatureConfirmation( txid: TransactionSignature, timeout: number, @@ -231,7 +259,10 @@ export enum SequenceType { Parallel, StopOnFailure, } - +/** + * Execute a sequence of transactions. + * @returns The transaction signature. + */ export const sendTransactions = async ( connection: Connection, wallet: any, diff --git a/src/lib/upload/upload.ts b/src/lib/upload/upload.ts index 8adf7a3..669efad 100644 --- a/src/lib/upload/upload.ts +++ b/src/lib/upload/upload.ts @@ -21,6 +21,11 @@ import { createCandyMachineV2, getFileExtension, getFileName, sleep } from './he type AssetKey = { mediaExt: string; index: string } type Env = 'mainnet-beta' | 'devnet' +/** + * + * Upload a new candy machine with the settings passed and cache. + * At the end of the upload, the cache can be downloaded. + */ export async function uploadV2({ files, cacheName,