diff --git a/clients/cli/commands/inscribe/nft.ts b/clients/cli/commands/inscribe/nft.ts index 29d903d..5aebab7 100644 --- a/clients/cli/commands/inscribe/nft.ts +++ b/clients/cli/commands/inscribe/nft.ts @@ -10,7 +10,7 @@ import yesno from "yesno"; const INSCRIPTION_GATEWAY: string = 'https://igw.metaplex.com/'; -export async function inscribe_nfts(rpc: string, keypair: string, mints: PublicKey[], concurrency: number) { +export async function inscribe_nfts(rpc: string, keypair: string, mints: PublicKey[], concurrency: number, skipJson: boolean, skipImages: boolean) { let umi = createUmi(rpc); umi = loadWalletKey(umi, keypair); umi.use(mplInscription()); @@ -37,8 +37,12 @@ export async function inscribe_nfts(rpc: string, keypair: string, mints: PublicK } }, { concurrency }); - let totalCost = jsonBytes.reduce((a, b) => a + getInscribeJsonCost(b.length), getInitCost() * mints.length); - const totalJsonBytes = jsonBytes.reduce((a, b) => a + b.length, 0); + let totalJsonBytes = 0; + let totalCost = getInitCost() * mints.length; + if (!skipJson) { + totalCost = jsonBytes.reduce((a, b) => a + getInscribeJsonCost(b.length), totalCost); + totalJsonBytes = jsonBytes.reduce((a, b) => a + b.length, 0); + } console.log(`${jsonBytes.length} JSON files are a total of ${totalJsonBytes} bytes.`); const jsonDatas = jsonBytes.map((bytes) => JSON.parse(bytes.toString())); @@ -75,8 +79,11 @@ export async function inscribe_nfts(rpc: string, keypair: string, mints: PublicK } }, { concurrency }); - totalCost = mediaBytes.reduce((a, b) => a + getInscribeMediaCost(b.length), totalCost); - const totalImageBytes = mediaBytes.reduce((a, b) => a + b.length, 0); + let totalImageBytes = 0; + if (!skipImages) { + totalCost = mediaBytes.reduce((a, b) => a + getInscribeMediaCost(b.length), totalCost); + totalImageBytes = mediaBytes.reduce((a, b) => a + b.length, 0); + } console.log(`${mediaBytes.length} Image files are a total of ${totalImageBytes} bytes.`); const ok = await yesno({ @@ -99,9 +106,17 @@ export async function inscribe_nfts(rpc: string, keypair: string, mints: PublicK }); if (!await accountExists(umi, mintInscriptionAccount[0])) { - await initializeFromMint(umi, { - mintAccount: nft.mint.publicKey, - }).sendAndConfirm(umi, { confirm: { commitment: 'finalized' } }); + let success = false; + while (!success) { + try { + await initializeFromMint(umi, { + mintAccount: nft.mint.publicKey, + }).sendAndConfirm(umi, { confirm: { commitment: 'finalized' } }); + success = true; + } catch (e) { + console.log(`\n${e}`); + } + } } return inscriptionMetadataAccount; @@ -118,37 +133,48 @@ export async function inscribe_nfts(rpc: string, keypair: string, mints: PublicK inscriptionMetadataAccount, }); - if (!await accountExists(umi, associatedInscriptionAccount[0])) { - await initializeAssociatedInscription(umi, { - inscriptionAccount: mintInscriptionAccount[0], - associationTag: 'image' - }).sendAndConfirm(umi, { confirm: { commitment: 'finalized' } }); + if (!skipImages && !await accountExists(umi, associatedInscriptionAccount[0])) { + let success = false; + while (!success) { + try { + await initializeAssociatedInscription(umi, { + inscriptionAccount: mintInscriptionAccount[0], + associationTag: 'image' + }).sendAndConfirm(umi, { confirm: { commitment: 'finalized' } }); + } catch (e) { + console.log(`\n${e}`); + } + } } return associatedInscriptionAccount; }, { concurrency }); - for (let i = 0; i < nfts.length; i += 1) { - const mintInscriptionAccount = findMintInscriptionPda(umi, { - mint: nfts[i].mint.publicKey - }); - - if (!await accountValid(umi, mintInscriptionAccount[0], jsonBytes[i])) { - console.log('Inscribing JSON...'); - await inscribe(umi, jsonBytes[i], mintInscriptionAccount, inscriptionMetadataAccounts[i], null, concurrency); - } else { - console.log('JSON already inscribed.'); + if (!skipJson) { + for (let i = 0; i < nfts.length; i += 1) { + const mintInscriptionAccount = findMintInscriptionPda(umi, { + mint: nfts[i].mint.publicKey + }); + + if (!await accountValid(umi, mintInscriptionAccount[0], jsonBytes[i])) { + console.log('Inscribing JSON...'); + await inscribe(umi, jsonBytes[i], mintInscriptionAccount, inscriptionMetadataAccounts[i], null, concurrency); + } else { + console.log('JSON already inscribed.'); + } + console.log(`JSON Inscription viewable at ${INSCRIPTION_GATEWAY}${network}/${mintInscriptionAccount[0].toString()}`); } - console.log(`JSON Inscription viewable at ${INSCRIPTION_GATEWAY}${network}/${mintInscriptionAccount[0].toString()}`); } - for (let i = 0; i < nfts.length; i += 1) { - if (!await accountValid(umi, associatedInscriptionAccounts[i][0], mediaBytes[i])) { - console.log('Inscribing image...'); - await inscribe(umi, mediaBytes[i], associatedInscriptionAccounts[i], inscriptionMetadataAccounts[i], 'image', concurrency); - } else { - console.log('Image already inscribed.'); + if (!skipImages) { + for (let i = 0; i < nfts.length; i += 1) { + if (!await accountValid(umi, associatedInscriptionAccounts[i][0], mediaBytes[i])) { + console.log('Inscribing image...'); + await inscribe(umi, mediaBytes[i], associatedInscriptionAccounts[i], inscriptionMetadataAccounts[i], 'image', concurrency); + } else { + console.log('Image already inscribed.'); + } + console.log(`Image Inscription viewable at ${INSCRIPTION_GATEWAY}${network}/${associatedInscriptionAccounts[i][0].toString()}`); } - console.log(`Image Inscription viewable at ${INSCRIPTION_GATEWAY}${network}/${associatedInscriptionAccounts[i][0].toString()}`); } } \ No newline at end of file diff --git a/clients/cli/index.ts b/clients/cli/index.ts index 036ceed..c1b1327 100644 --- a/clients/cli/index.ts +++ b/clients/cli/index.ts @@ -25,9 +25,11 @@ inscribeCmd.command('nft') .option('-k --keypair ', 'Solana wallet location') .option('-m --mint ', 'Mint address of the NFT') .option('-c --concurrency ', 'Number of concurrent writes to perform', '10') + .option('-sj --skip-json', 'Skip the JSON file creation', false) + .option('-si --skip-images', 'Skip the image file creation', false) .action(async (str, options) => { - const { rpc, keypair, mint, concurrency } = options.opts(); - await inscribe_nfts(rpc, keypair, [publicKey(mint)], parseInt(concurrency)); + const { rpc, keypair, mint, concurrency, skipJson, skipImages } = options.opts(); + await inscribe_nfts(rpc, keypair, [publicKey(mint)], parseInt(concurrency), skipJson, skipImages); }); inscribeCmd.command('hashlist') @@ -36,13 +38,15 @@ inscribeCmd.command('hashlist') .option('-k --keypair ', 'Solana wallet location') .option('-h --hashlist ', 'The file containing the hashlist') .option('-c --concurrency ', 'Number of concurrent writes to perform', '10') + .option('-sj --skip-json', 'Skip the JSON file creation', false) + .option('-si --skip-images', 'Skip the image file creation', false) .action(async (str, options) => { - const { rpc, keypair, hashlist, concurrency } = options.opts(); + const { rpc, keypair, hashlist, concurrency, skipJson, skipImages } = options.opts(); const hashlistArray = JSON.parse(readFileSync(hashlist, 'utf-8')); const mints: PublicKey[] = hashlistArray.map((mint: string) => publicKey(mint)); - await inscribe_nfts(rpc, keypair, mints, parseInt(concurrency)); + await inscribe_nfts(rpc, keypair, mints, parseInt(concurrency), skipJson, skipImages); }); const downloadCmd = program.command('download');