Skip to content

Commit

Permalink
Adding fetch by rank.
Browse files Browse the repository at this point in the history
  • Loading branch information
blockiosaurus committed Jan 18, 2024
1 parent 25f3fdf commit 5f41dbc
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ jsonz56LVLsegzJHJE5ysRC1qdeakiGFtB5ys8EWyJC.json
inscr9svLET8FFoaFi3yPt8xF7fKsHLQWC5a4jRMFpb.json
1NSCRfGeyo7wPUazGbaPBUsTM49e1k2aXewHGARfzSo.json
clients/cli/cache
clients/cli/testdata
clients/cli/testdata
clients/cli/output.json
52 changes: 52 additions & 0 deletions clients/cli/commands/fetch/number.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Key, getInscriptionMetadataGpaBuilder, mplInscription } from "@metaplex-foundation/mpl-inscription";
import { createUmi } from "@metaplex-foundation/umi-bundle-defaults";
import { writeFileSync } from "fs";
import pMap from "p-map";

export async function fetchInscriptionsByRank(rpc: string, ranks: number[], concurrency: number, output: string) {
const umi = createUmi(rpc);
umi.use(mplInscription());

const inscriptionMetadataAccounts = await pMap(ranks, async (rank) => {
const inscriptionMetadatas = [
...(await getInscriptionMetadataGpaBuilder(umi)
.whereField("key", Key.MintInscriptionMetadataAccount)
.whereField("inscriptionRank", rank)
.getDeserialized()),
...(await getInscriptionMetadataGpaBuilder(umi)
.whereField("key", Key.InscriptionMetadataAccount)
.whereField("inscriptionRank", rank)
.getDeserialized())
]

// console.log(inscriptionMetadatas);

if (inscriptionMetadatas.length === 0) {
throw new Error(`No inscription metadata found for rank ${rank}`);
} else if (inscriptionMetadatas.length > 1) {
throw new Error(`Multiple inscription metadata found for rank ${rank}. This should never happen!`);
}

const inscriptionMetadata = inscriptionMetadatas[0];

delete inscriptionMetadata["header"];
delete inscriptionMetadata["padding"];
return inscriptionMetadata;
}, { concurrency });

inscriptionMetadataAccounts.sort((a, b) => {
if (a.inscriptionRank < b.inscriptionRank) {
return -1;
}
if (a.inscriptionRank > b.inscriptionRank) {
return 1;
}
return 0;
});

writeFileSync(output, JSON.stringify([...inscriptionMetadataAccounts], (key, value) =>
typeof value === 'bigint'
? value.toString()
: value,
2));
}
15 changes: 15 additions & 0 deletions clients/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { cost_nfts } from './commands/cost/nft.js';
import { compress_images } from './commands/compress/images.js';
import { compress_json } from './commands/compress/json.js';
import { fetchInscriptionsByMint } from './commands/fetch/nft.js';
import { fetchInscriptionsByRank } from './commands/fetch/number.js';

const program = new Command();

Expand Down Expand Up @@ -155,6 +156,20 @@ fetchCmd.command('hashlist')
await fetchInscriptionsByMint(rpc, mints, parseInt(concurrency), output);
});

fetchCmd.command('rank')
.description('Fetch Inscription data by rank')
.option('-r --rpc <string>', 'The endpoint to connect to.')
.option('-rn --rankNumbers <number...>', 'The rank to search for')
.option('-c --concurrency <number>', 'Number of concurrent writes to perform', '10')
.option('o --output <string>', 'Output file', 'output.json')
.action(async (str, options) => {
const { rpc, rankNumbers, concurrency, output } = options.opts();

const ranks: number[] = rankNumbers.map((rank: string) => parseInt(rank));

await fetchInscriptionsByRank(rpc, ranks, parseInt(concurrency), output);
});

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

createCmd.command('shards')
Expand Down

0 comments on commit 5f41dbc

Please sign in to comment.