forked from h4rkl/Ghetto-SolAir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nft-tools.js
287 lines (278 loc) · 8.99 KB
/
nft-tools.js
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
const shell = require("shelljs");
const { forEach } = require("lodash");
const fetch = require("node-fetch");
const fs = require("fs");
const { deleteFile } = require("./index");
const { Metadata } = require("@metaplex-foundation/mpl-token-metadata");
const { Connection, PublicKey } = require("@solana/web3.js");
const { Windex } = require("@wonka-labs/wonka-js");
const { AnyPublicKey } = require("@metaplex-foundation/mpl-core");
const ENDPOINTS = {
DEV: "https://api.devnet.solana.com",
TEST: "https://api.testnet.solana.com",
MAIN: "https://api.mainnet-beta.solana.com",
SERUM: "https://solana-api.projectserum.com",
GEN: "https://ssc-dao.genesysgo.net",
};
/**
* Gets all the NFTs and associated meta for a given wallet address. Useful for
* inspecting a users wallet and getting all NFT meta.
* @param {AnyPublicKey} publicAddress and Solana NFT owner account
* @param {ENDPOINTS} network one of DEV, TEST or MAIN
*/
const getNFTList = async (publicAddress, network) => {
const connect = network
? ENDPOINTS[network]
: "https://api.devnet.solana.com";
const connection = new Connection(connect);
try {
const errors = [];
const nftMeta = await Metadata.findDataByOwner(connection, publicAddress);
const accNFTs = await Promise.all(
nftMeta.map(async (metadata, i) => {
console.log(`Fetch meta for ${metadata.data.uri}`);
let NFTData;
if (metadata?.data?.uri) {
NFTData = await fetch(metadata.data.uri, null).then((response) =>
response.json()
);
}
console.log(`Meta fetched for ${metadata.data.uri} at index ${i}`);
return { mint: metadata.mint, ...NFTData };
})
);
deleteFile("data/NFT/nft-list.json");
fs.appendFile(
"data/NFT/nft-list.json",
JSON.stringify(accNFTs),
function (err) {
if (err) throw new Error(err);
console.log(`Converted ${accNFTs.length} records`);
}
);
} catch (error) {
console.log(error);
}
};
/**
* The name and family object in the collection meta
* @param {string} name
* @param {string} family
* @param {number} count will cut the collection to a specific N value
*/
const filterCollection = (name, family, count) => {
const d = require("./data/NFT/nft-list.json");
console.log(`Data contains ${d.length} records`);
const collection = d
.map((nftData) => {
if (
nftData?.collection !== undefined &&
nftData?.collection?.name === name &&
nftData?.collection?.family === family
) {
return {
mint: nftData.mint,
collection: `${nftData?.collection?.name}-${nftData?.collection?.family}`,
};
}
return;
})
.filter((pk) => !!pk);
if (count) {
collection.length = count;
}
deleteFile("data/NFT/collection.json");
fs.appendFile(
"data/NFT/collection.json",
JSON.stringify(collection),
function (err) {
if (err) throw new Error(err);
console.log(`Collection contains ${collection.length} records`);
}
);
};
/**
* WARNING!!!!!
* This will execute your NFT transfer
* @param {PubKey} address the owner account you want to bulk send the NFTs to
*/
const exeNFTDrop = (address) => {
const d = require("./data/NFT/collection.json");
const errors = [];
forEach(d, (v, i) => {
console.log(
`Sending ${JSON.stringify(v)} to ${address} at count: ${i + 1}/${
d.length
} recipients`
);
// Execute the NFT transfer here
shell.exec(
`spl-token transfer ${v.mint} 1 ${address} --fund-recipient --allow-unfunded-recipient`
);
// Push any accounts that have errors
if (shell.error()) {
errors.push(v);
}
});
// Write the error file - should be [] if none
deleteFile("data/NFT/errors.json");
fs.appendFile("data/NFT/errors.json", JSON.stringify(errors), function (err) {
if (err) return console.log(err);
console.log("Errors");
});
};
/**
* Get all the token ids in a CM. The CandyMachine id can be found in the "Metaplex NFT Candy Machine v2 instruction"
* where the "#1 - Account0 " is typically the CMv2 pubkey.
* https://stackoverflow.com/questions/70597753/how-to-find-all-nfts-minted-from-a-v2-candy-machine
* @param {string} candyMachineId
* @param {string} network should be 'MAIN' or 'DEV' - NB!!! has to use Windex endpoint
*/
const getCMAddresses = async (candyMachineId, network) => {
const cmId = new PublicKey(candyMachineId);
const endpoint =
network === "MAIN" ? Windex.MAINNET_ENDPOINT : Windex.DEVNET_ENDPOINT;
// console.log(endpoint, cmId);
const fetchNFTsByCandyMachine = async (cmId) => {
const nfts = await Windex.fetchNFTsByCandyMachineID(cmId, 5000, endpoint);
console.log(`Retrieved ${nfts.length} NFTs!`);
return nfts;
};
const hashes = await fetchNFTsByCandyMachine(cmId);
const hashMap = hashes.map((nft) => nft.address);
deleteFile(`data/NFT/${candyMachineId}-nfts.json`);
fs.appendFile(
`data/NFT/${candyMachineId}-nfts.json`,
JSON.stringify(hashMap),
function (err) {
if (err) throw new Error(err);
console.log(`Collection contains ${hashMap.length} records`);
}
);
};
/**
* Tool for quickly making address obj in arr for the airdrop tool
* @param {string} file
*/
const mapArrtoAddrObj = (file) => {
const d = require(file);
const addressed = d.map((a) => ({ address: a }));
deleteFile(`data/addressed.json`);
fs.appendFile(
`data/addressed.json`,
JSON.stringify(addressed),
function (err) {
if (err) throw new Error(err);
console.log(`Collection contains ${addressed.length} records`);
}
);
};
/**
* Get the mint info for token
* @param {string} mintAddress
* @param {ENDPOINTS} network
*/
const getNFTOwner = async (mintAddress, network) => {
const connect = network
? ENDPOINTS[network]
: "https://api.devnet.solana.com";
const connection = new Connection(connect);
const largestAccounts = await connection.getTokenLargestAccounts(
new PublicKey(mintAddress)
);
const largestAccountInfo = await connection.getParsedAccountInfo(
largestAccounts.value[0].address
);
return largestAccountInfo.value.data.parsed.info.owner;
};
/**
* Get the NFT owners for a list of mint addresses
* @param {string} file path to file
* @param {ENDPOINTS} network
*/
const listNFTOwners = async (file, network) => {
const connect = network
? ENDPOINTS[network]
: "https://api.devnet.solana.com";
const connection = new Connection(connect);
try {
const f = require(file);
// Split file into groups of 40 to avoid the rate limit
// const chunkSize = 39;
// const chunked = [];
// for (let i = 0; i < f.length; i += chunkSize) {
// const chunk = f.slice(i, i + chunkSize);
// chunked.push(chunk);
// }
const chunked = f;
let accArr = [];
const nftAccs = async () => {
let index = 0;
return await new Promise((resolve) => {
const interval = setInterval(async function () {
console.log(`Fetch Acc for ${chunked[index]} at index ${index}`);
const largestAccounts = await connection.getTokenLargestAccounts(
new PublicKey(chunked[index])
);
const largestAccountInfo = await connection.getParsedAccountInfo(
largestAccounts.value[0].address
);
accArr.push(largestAccountInfo.value.data.parsed.info.owner);
index++;
if (index === chunked.length) {
clearInterval(interval);
resolve();
}
}, 400);
});
// return await new Promise((resolve) => {
// const interval = setInterval(async function () {
// await Promise.all(
// chunked[index++].map(async (mintAddress, i) => {
// console.log(
// `Fetch Acc for ${mintAddress} at index ${index} - ${i}`
// );
// const largestAccounts = await connection.getTokenLargestAccounts(
// new PublicKey(mintAddress)
// );
// const largestAccountInfo = await connection.getParsedAccountInfo(
// largestAccounts.value[0].address
// );
// accArr.push(largestAccountInfo.value.data.parsed.info.owner);
// })
// );
// if (index === chunked.length) {
// clearInterval(interval);
// resolve();
// }
// }, 1000);
// });
};
await nftAccs();
console.log("-----------", accArr.length);
// setTimeout(function loopRequest() {
// setTimeout(loopRequest, 1000);
// console.log("boom");
// }, 0);
deleteFile("data/NFT/acc-nft-list.json");
fs.appendFile(
"data/NFT/acc-nft-list.json",
JSON.stringify(accArr),
function (err) {
if (err) throw new Error(err);
console.log(`Found ${accArr.length} records`);
}
);
} catch (error) {
console.log(error);
}
};
module.exports = {
exeNFTDrop,
filterCollection,
getNFTList,
getCMAddresses,
mapArrtoAddrObj,
getNFTOwner,
listNFTOwners,
};