From 756f58ecd66fe7b8c0efaf47d66050bdb4528def Mon Sep 17 00:00:00 2001 From: grimAgent <90475821+grimAgent@users.noreply.github.com> Date: Tue, 14 Sep 2021 15:54:51 -0400 Subject: [PATCH] Command to verify candymachine price (#397) --- js/packages/cli/src/cli.ts | 45 ++++++++++++++++++++++++++++ js/packages/cli/src/helpers/cache.ts | 12 ++++---- 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/js/packages/cli/src/cli.ts b/js/packages/cli/src/cli.ts index 3aae533..1de61f3 100755 --- a/js/packages/cli/src/cli.ts +++ b/js/packages/cli/src/cli.ts @@ -144,6 +144,51 @@ programCommand('verify') saveCache(cacheName, env, cacheContent); }); +programCommand('verify_price') + .option('-p, --price ') + .option('--cache-path ') + .action(async (directory, cmd) => { + const { keypair, env, price, cacheName, cachePath } = cmd.opts(); + const lamports = parsePrice(price); + + if (isNaN(lamports)) { + return log.error(`verify_price requires a --price to be set`); + } + + log.info(`Expected price is: ${lamports}`); + + const cacheContent = loadCache(cacheName, env, cachePath); + + if (!cacheContent) { + return log.error( + `No cache found, can't continue. Make sure you are in the correct directory where the assets are located or use the --cache-path option.`, + ); + } + + const walletKeyPair = loadWalletKey(keypair); + const anchorProgram = await loadAnchorProgram(walletKeyPair, env); + + const [candyMachine] = await getCandyMachineAddress( + new PublicKey(cacheContent.program.config), + cacheContent.program.uuid, + ); + + const machine = await anchorProgram.account.candyMachine.fetch( + candyMachine, + ); + + //@ts-ignore + const candyMachineLamports = machine.data.price.toNumber(); + + log.info(`Candymachine price is: ${candyMachineLamports}`); + + if (lamports != candyMachineLamports) { + throw new Error(`Expected price and CandyMachine's price do not match!`); + } + + log.info(`Good to go!`); + }); + programCommand('create_candy_machine') .option('-p, --price ', 'Price denominated in SOL or spl-token override', '1') .option('-t, --spl-token ', 'SPL token used to price NFT mint. To use SOL leave this empty.') diff --git a/js/packages/cli/src/helpers/cache.ts b/js/packages/cli/src/helpers/cache.ts index 5a55825..cf1fc66 100644 --- a/js/packages/cli/src/helpers/cache.ts +++ b/js/packages/cli/src/helpers/cache.ts @@ -2,17 +2,17 @@ import path from "path"; import { CACHE_PATH } from "./constants"; import fs from "fs"; -export function cachePath(env: string, cacheName: string) { - return path.join(CACHE_PATH, `${env}-${cacheName}`); +export function cachePath(env: string, cacheName: string, cPath: string = CACHE_PATH) { + return path.join(cPath, `${env}-${cacheName}`); } -export function loadCache(cacheName: string, env: string) { - const path = cachePath(env, cacheName); +export function loadCache(cacheName: string, env: string, cPath: string = CACHE_PATH) { + const path = cachePath(env, cacheName, cPath); return fs.existsSync(path) ? JSON.parse(fs.readFileSync(path).toString()) : undefined; } -export function saveCache(cacheName: string, env: string, cacheContent) { - fs.writeFileSync(cachePath(env, cacheName), JSON.stringify(cacheContent)); +export function saveCache(cacheName: string, env: string, cacheContent, cPath: string = CACHE_PATH) { + fs.writeFileSync(cachePath(env, cacheName, cPath), JSON.stringify(cacheContent)); }