-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathindex.ts
121 lines (96 loc) · 2.91 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import fs from 'fs';
import path from 'path';
import cliProgress from 'cli-progress';
import got from 'got';
import { clusterApiUrl, Connection, PublicKey } from '@solana/web3.js';
import { Data, Metadata } from './metaplex/classes';
import { METADATA_PROGRAM_ID } from './metaplex/constants';
import { decodeMetadata } from './metaplex/metadata';
const METADATA_PROGRAM_PK = new PublicKey(METADATA_PROGRAM_ID);
const OUTPUT_DIR = './results';
interface MintData {
imageUri?: string;
mintWalletAddress: string;
nftData: Data;
tokenMetadata: Metadata;
totalSupply: number;
}
async function retrieveMetadata(accountData) {
const tokenMetadata = decodeMetadata(accountData);
const nftInfoResponse = await got(tokenMetadata.data.uri);
return {
nftData: JSON.parse(nftInfoResponse.body),
tokenMetadata,
};
}
async function tryToShowOverallMetadataInfo(accountData) {
const { nftData } = await retrieveMetadata(accountData);
console.log(`Name: ${nftData.name || ''}`);
console.log(`Symbol: ${nftData.symbol || ''}`);
console.log(`Collection.Name: ${nftData.collection?.name || ''}`);
console.log(`Collection.Family: ${nftData.collection?.family || ''}`);
}
(async function () {
if (process.argv.length < 3) {
console.error('please provide a wallet address as an argument');
return;
}
const mintWalletAddress = process.argv.slice(2).shift();
const conn = new Connection(clusterApiUrl('mainnet-beta'), 'confirmed');
const response = await conn.getProgramAccounts(METADATA_PROGRAM_PK, {
filters: [
{
memcmp: {
offset: 326,
bytes: mintWalletAddress,
},
},
],
});
const totalSupply = response.length;
console.log('Mint Wallet Address: ', mintWalletAddress);
console.log('Total Supply: ', totalSupply);
// quickly show metadata from first record
const firstRecord = response[0];
await tryToShowOverallMetadataInfo(firstRecord.account.data);
const progressBar = new cliProgress.SingleBar(
{},
cliProgress.Presets.shades_classic
);
progressBar.start(totalSupply, 0);
if (!totalSupply) {
progressBar.stop();
return;
}
const mintTokenIds = [];
const mints: MintData[] = [];
for (const record of response) {
const { nftData, tokenMetadata } = await retrieveMetadata(
record.account.data
);
const mintData = {
imageUri: nftData?.image,
mintWalletAddress,
nftData,
tokenMetadata,
totalSupply,
};
mintTokenIds.push(tokenMetadata.mint);
mints.push(mintData);
progressBar.increment();
}
progressBar.stop();
if (!fs.existsSync(OUTPUT_DIR)) {
fs.mkdirSync(OUTPUT_DIR);
}
fs.writeFileSync(
path.join(OUTPUT_DIR, `mint-token-ids:${mintWalletAddress}.json`),
JSON.stringify(mintTokenIds),
'utf-8'
);
fs.writeFileSync(
path.join(OUTPUT_DIR, `mint-data:${mintWalletAddress}.json`),
JSON.stringify(mints),
'utf-8'
);
})();