Skip to content

Commit

Permalink
Adding cost estimate tool.
Browse files Browse the repository at this point in the history
  • Loading branch information
blockiosaurus committed Jan 15, 2024
1 parent cd7bd8f commit c3eb964
Show file tree
Hide file tree
Showing 5 changed files with 214 additions and 7 deletions.
42 changes: 42 additions & 0 deletions clients/cli/commands/cost/nft.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { fetchDigitalAsset, mplTokenMetadata } from "@metaplex-foundation/mpl-token-metadata";
import { createUmi } from "@metaplex-foundation/umi-bundle-defaults";
import { PublicKey } from "@metaplex-foundation/umi";
import pMap from "p-map";
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
import { SingleBar, Presets } from "cli-progress";
import { fileTypeFromBuffer } from "file-type";
import { globSync } from "glob";
import { getInitCost, getInscribeJsonCost, getInscribeMediaCost } from "../../utils.js";

export async function cost_nfts(mints: PublicKey[]) {
// Create the cache folder if it doesn't already exist.
if (!existsSync('./cache')) {
mkdirSync('./cache');
}

console.log(`Estimating cost for ${mints.length} JSON files...`);
let fetchJsonBar = new SingleBar({}, Presets.shades_classic)
fetchJsonBar.start(mints.length, 0);
const jsonBytes = await pMap(mints, async (mint, _i) => {
if (existsSync(`./cache/${mint}.json`)) {
fetchJsonBar.increment();
return readFileSync(`./cache/${mint}.json`);
}
}, { concurrency: 1 });
fetchJsonBar.stop();

let totalCost = jsonBytes.reduce((a, b) => a + getInscribeJsonCost(b.length), getInitCost() * mints.length);

let fetchMediaBar = new SingleBar({}, Presets.shades_classic)
fetchMediaBar.start(mints.length, 0);
const mediaBytes = await pMap(mints, async (mint, i) => {
const mediaFiles = globSync(`./cache/${mint}.*`, { ignore: ['cache/*.json', 'cache/*.metadata'] });
const media = readFileSync(mediaFiles[0]);
fetchMediaBar.increment();
return media;
}, { concurrency: 1 });
fetchMediaBar.stop();

totalCost = mediaBytes.reduce((a, b) => a + getInscribeMediaCost(b.length), totalCost);
console.log(`Total Inscription cost is ${totalCost} SOL.`);
}
7 changes: 6 additions & 1 deletion clients/cli/commands/download/nft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ export async function download_nfts(rpc: string, keypair: string, mints: PublicK

let retries = 5;
while (retries > 0) {
if (nft.metadata.uri === '') {
throw new Error(`No URI found for ${nft.metadata.mint}!`);
}

try {
const json = Buffer.from(await (await fetch(nft.metadata.uri)).arrayBuffer());
const jsonFile = `./cache/${nft.metadata.mint}.json`;
Expand All @@ -57,6 +61,7 @@ export async function download_nfts(rpc: string, keypair: string, mints: PublicK
return json;
} catch (e) {
console.log(`\n${e}\n`);
console.log(nft);
retries--;
}
}
Expand All @@ -74,7 +79,7 @@ export async function download_nfts(rpc: string, keypair: string, mints: PublicK
imageURI = jsonData.animation_url;
} else if (jsonData.image) {
imageURI = jsonData.image;
} else {
} else if (jsonData.properties && jsonData.properties.files) {
for (const file of jsonData.properties.files) {
if (file.type === 'image/png' || file.type === 'image/jpeg' || file.type === 'image/gif') {
imageURI = file.uri;
Expand Down
26 changes: 26 additions & 0 deletions clients/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { download_nfts } from './commands/download/nft.js';

import { Command } from 'commander';
import { test_createCollection } from './commands/test/createCollection.js';
import { cost_nfts } from './commands/cost/nft.js';

const program = new Command();

Expand Down Expand Up @@ -71,6 +72,31 @@ downloadCmd.command('hashlist')
await download_nfts(rpc, keypair, mints, parseInt(concurrency));
});

const costCmd = program.command('cost');

costCmd.command('nft')
.description('Calculate the cost of inscribing an NFT')
.option('-r --rpc <string>', 'The endpoint to connect to.')
.option('-m --mint <string>', 'Mint address of the NFT')
.action(async (str, options) => {
const { rpc, mint } = options.opts();

await cost_nfts([publicKey(mint)]);
});

costCmd.command('hashlist')
.description('Calculate the cost of inscribing a hashlist')
.option('-r --rpc <string>', 'The endpoint to connect to.')
.option('-h --hashlist <string>', 'The file containing the hashlist')
.action(async (str, options) => {
const { rpc, hashlist } = options.opts();

const hashlistArray = JSON.parse(readFileSync(hashlist, 'utf-8'));
const mints: PublicKey[] = hashlistArray.map((mint: string) => publicKey(mint));

await cost_nfts(mints);
});

const createCmd = program.command('create');

createCmd.command('shards')
Expand Down
1 change: 1 addition & 0 deletions clients/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"cli-progress": "^3.12.0",
"commander": "^11.1.0",
"file-type": "^19.0.0",
"glob": "^10.3.10",
"p-map": "npm:@esm2cjs/p-map@^5.5.0"
},
"devDependencies": {
Expand Down
Loading

0 comments on commit c3eb964

Please sign in to comment.