From 0ace864da591f4631557b1062ee953ffb034788d Mon Sep 17 00:00:00 2001 From: 0xDon <22967202+thedonmon@users.noreply.github.com> Date: Sat, 24 Feb 2024 17:52:33 -0500 Subject: [PATCH] add command for update and fix --- src/cli.ts | 34 +++++++++++++++++++++++++++++++++- src/lib/helpers.ts | 24 +++++++++++++++++------- src/lib/manageNft.ts | 18 ++++++++++++------ 3 files changed, 62 insertions(+), 14 deletions(-) diff --git a/src/cli.ts b/src/cli.ts index fd4d9bb..175788a 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -14,6 +14,7 @@ import { CreateNftArgs, MetadataConfig, NftCollection, + UpdateNftArgs, } from './types/collection'; import { createCollection } from './lib/createCollection'; import { createMerkleTree } from './lib/createMerkleTree'; @@ -24,6 +25,7 @@ import { fetchCnftsByOwner, mintNftIxTokenPayment, searchCnfts, + updateNft, } from 'lib/manageNft'; import { TokenPayment } from 'types/tokenPayment'; import { @@ -271,7 +273,37 @@ programCommand('mintNftTokenPayment', { requireWallet: true }) const signature = await connection.sendTransaction(txn); ora(`NFT minted! Signature: ${magentaB(signature)}`).succeed(); - writeToFile({ signature }, `nft-${signature}.json`, { + + writeToFile(res, `nfts-${signature}.json`, { + writeToFile: opts.log, + }); + }); + +programCommand('updateNft', { requireWallet: true }) + .description('Update an NFT') + .addOption(new Option('-a, --assetId ', 'AssetId of the CNFT')) + .addOption(new Option('-n, --name ', 'Name of the CNFT')) + .addOption(new Option('-u, --uri ', 'URI of the CNFT')) + .addOption( + new Option('-co, --collection ', 'Collection mint of the CNFT'), + ) + .addOption(new Option('-l, --lut ', 'LUT address')) + .action(async (opts) => { + const keypair = loadWalletKey(opts.keypair); + const updateArgs: UpdateNftArgs = { + assetId: opts.assetId, + uri: opts.uri, + name: opts.name, + }; + const res = await updateNft( + keypair.secretKey, + updateArgs, + opts.collection, + opts.lut, + opts.rpc, + ); + ora(`NFT updated!`).succeed(); + writeToFile(res, `nft-${opts.assetId}.json`, { writeToFile: opts.log, }); }); diff --git a/src/lib/helpers.ts b/src/lib/helpers.ts index 36768b9..2a77885 100644 --- a/src/lib/helpers.ts +++ b/src/lib/helpers.ts @@ -11,11 +11,15 @@ export function extractSecret(keyPair: string | Uint8Array) { export function writeToFile( data: any, path: string, - options: { jsonFormat?: boolean; writeToFile?: boolean } = { + providedOptions: { jsonFormat?: boolean; writeToFile?: boolean } = {}, +) { + const defaultOptions = { jsonFormat: true, writeToFile: true, - }, -) { + }; + + // Merge provided options with default options + const options = { ...defaultOptions, ...providedOptions }; const dir = './out'; if (!fs.existsSync(dir)) { fs.mkdirSync(dir); @@ -23,10 +27,8 @@ export function writeToFile( if (!options.writeToFile) { return; } - fs.writeFileSync( - `${dir}/${path}`, - options.jsonFormat ? JSON.stringify(data, null, 2) : data, - ); + const dataToWrite = options.jsonFormat ? JSON.stringify(data, null, 2) : data; + fs.writeFileSync(`${dir}/${path}`, dataToWrite); ora(`Data saved to ${dir}/${path}`).succeed(); } @@ -91,3 +93,11 @@ export function nativeToUiNumber( const amt = toBigNumber(amount); return amt.div(10 ** decimals).toNumber(); } + +export function estimateTransactionSize( + serializedTxn: Uint8Array, + signaturesLength: number, +): number { + const size = serializedTxn.length + 1 + signaturesLength * 64; + return size; +} diff --git a/src/lib/manageNft.ts b/src/lib/manageNft.ts index 4ca5637..1605f5c 100644 --- a/src/lib/manageNft.ts +++ b/src/lib/manageNft.ts @@ -51,6 +51,7 @@ import { SearchAssetsRpcInput, dasApi, } from '@metaplex-foundation/digital-asset-standard-api'; +import { createCnftLUT, extendLUT } from './manageLUT'; dotenv.config(); @@ -449,12 +450,9 @@ export async function updateNft( lutAddress?: string, rpcUrl?: string, ) { - if (!process.env.NFT_STORAGE_API_KEY) { - throw new Error('NFT_STORAGE_API_KEY is not set'); - } const umi = createUmi(rpcUrl || clusterApiUrl('devnet')) .use(mplTokenMetadata()) - .use(nftStorageUploader({ token: process.env.NFT_STORAGE_API_KEY })); + .use(dasApi()); //check if keypair is string, if so convert to Uint8Array const keypair = typeof keyPair === 'string' ? bs58.decode(keyPair) : keyPair; const umiKeypair = umi.eddsa.createKeypairFromSecretKey(keypair); @@ -480,8 +478,16 @@ export async function updateNft( ]); console.log('added lut to txn'); } - const res = await ix.sendAndConfirm(umi); - return res; + console.log(ix.fitsInOneTransaction(umi)); + const res = await ix.sendAndConfirm(umi, { + send: { + skipPreflight: true, + }, + }); + return { + ...res, + signaure: bs58.encode(res.signature), + }; } export async function fetchCnftsByCollection(