-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmintNFTfromPinnedMetadata.js
324 lines (289 loc) · 9.2 KB
/
mintNFTfromPinnedMetadata.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
require('dotenv').config();
const {
AccountId,
PrivateKey,
Client,
TokenMintTransaction,
} = require('@hashgraph/sdk');
const fetch = require('cross-fetch');
const path = require('path');
const readlineSync = require('readline-sync');
const fs = require('fs');
const baseUrlForMainnet = 'https://mainnet-public.mirrornode.hedera.com';
const baseUrlForTestnet = 'http://testnet.mirrornode.hedera.com';
const maxRetries = 10;
// Configure accounts and client, and generate needed keys
const operatorId = AccountId.fromString(process.env.MY_ACCOUNT_ID);
const operatorKey = PrivateKey.fromString(process.env.MY_PRIVATE_KEY);
const supplyKey = PrivateKey.fromString(process.env.NFT_SUPPLY_KEY);
const nftBatchSize = Number(process.env.NFT_MINT_BATCH_SIZE) || 10;
const env = process.env.ENVIRONMENT ?? null;
async function getTokenSupplyDetails(tokenId) {
const baseUrl = env == 'MAIN' ? baseUrlForMainnet : baseUrlForTestnet;
const routeUrl = '/api/v1/tokens/' + tokenId;
try {
const json = await fetchJson(baseUrl + routeUrl);
return [json.max_supply, json.total_supply];
}
catch (err) {
console.log('Trying to get token supply details', baseUrl, routeUrl);
console.error(err);
process.exit(1);
}
}
async function fetchJson(url, depth = 0) {
if (depth >= maxRetries) return null;
if (depth > (maxRetries / 2)) console.log('Attempt: ', depth, url);
depth++;
try {
const res = await fetchWithTimeout(url);
if (res.status != 200) {
await sleep(500 * depth);
return await fetchJson(url, depth);
}
return res.json();
}
catch (err) {
await sleep(500 * depth);
return await fetchJson(url, depth);
}
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function fetchWithTimeout(resource, options = {}) {
const { timeout = 30000 } = options;
const controller = new AbortController();
const id = setTimeout(() => controller.abort(), timeout);
const response = await fetch(resource, {
...options,
signal: controller.signal,
});
clearTimeout(id);
return response;
}
function getArg(arg) {
const customIndex = process.argv.indexOf(`-${arg}`);
let customValue;
if (customIndex > -1) {
// Retrieve the value after --custom
customValue = process.argv[customIndex + 1];
}
return customValue;
}
function getArgFlag(arg) {
const customIndex = process.argv.indexOf(`-${arg}`);
if (customIndex > -1) {
return true;
}
return false;
}
async function main() {
if (getArgFlag('h')) {
console.log('Usage: node mintNFTfromPinnedMetadata.js -process <file>');
console.log(' -process json file to read in and process');
console.log(' File format: \
\n\t[ \
\n\t\t"ipfs://XXXX/metadata.json", \
\n\t\t"ipfs://YYYY/metadata.json", \
\n\t\t"ipfs://ZZZZ/metadata.json" \
\n\t]\
\nor (output format of this script)\n\
\n\t{\
\n\t\t"0": {\
\n\t\t\t"cid": "ipfs://XXXX/metadata.json",\
\n\t\t\t"minted": true,\
\n\t\t\t"serial": "629"\
\n\t\t},\
\n\t\t"1": {\
\n\t\t\t"cid": "ipfs://YYY/metadata.json",\
\n\t\t\t"minted": false,\
\n\t\t},\
\n\t\t"2": {\
\n\t\t\t"cid": "ipfs://ZZZZ/metadata.json",\
\n\t\t}\
\n\t}');
process.exit(0);
}
const fileToProcess = path.resolve(getArg('process'));
if (!fileToProcess) {
console.log('ERROR: must specifiy file to process - EXITING');
process.exit(1);
}
else {
console.log('Processing file:', fileToProcess);
}
// Create our connection to the Hedera network
let client;
if (env == 'TEST') {
client = Client.forTestnet();
console.log('Proposing to mint tokens in *TESTNET*');
}
else if (env == 'MAIN') {
client = Client.forMainnet();
console.log('Proposing to mint tokens in *MAINNET*');
}
else {
console.log('ERROR: Must specify either MAIN or TEST as environment in .env file');
return;
}
client.setOperator(operatorId, operatorKey);
client.setMaxAttempts(30);
client.setMaxNodeAttempts(2);
client.setRequestTimeout(30000);
// Get the token ID
const tokenId = process.env.NFT_TOKEN_ID;
if (!tokenId) {
console.log('NFT_TOKEN_ID (in .env file) must be specified to use the script - exiting');
process.exit(1);
}
// get max supply / current supply
const [maxSupply, totSupply] = await getTokenSupplyDetails(tokenId);
// parse file to see how many NFTs to mint
let pinnedMetadataJSONString;
// read in the file specified
try {
pinnedMetadataJSONString = fs.readFileSync(fileToProcess, 'utf8');
}
catch (err) {
console.log(`ERROR: Could not read file (${fileToProcess})`, err);
process.exit(1);
}
// parse JSON
let pinnedMetadataObjFromFile;
try {
pinnedMetadataObjFromFile = JSON.parse(pinnedMetadataJSONString);
}
catch (err) {
console.log('ERROR: failed to parse the specified JSON', err, pinnedMetadataJSONString);
process.exit(1);
}
let plannedMint = 0;
const pinnedMetadataObjFromFileLength = Object.keys(pinnedMetadataObjFromFile).length;
const pinnedMetadataObj = {};
for (let p = 0; p < pinnedMetadataObjFromFileLength; p++) {
const pinCID = pinnedMetadataObjFromFile[p];
if (!pinCID.cid) {
// convert to functional format
plannedMint++;
pinnedMetadataObj[p] = {
cid: pinCID,
minted: false,
};
}
else {
if (!pinCID.minted) plannedMint++;
// move the object accross
pinnedMetadataObj[p] = pinCID;
}
}
await writeProgress(fileToProcess, pinnedMetadataObj);
// check enough space on token to mint
if (maxSupply == 0 || (maxSupply - totSupply - plannedMint) >= 0) {
console.log('Precheck passed - enough space on token to mint');
}
else {
console.log('Not enough space on token to mint - please check file specified', `Max Supply: ${maxSupply}`, `Current Supply: ${totSupply}`, `Planned mint: ${plannedMint}`);
process.exit(1);
}
// Log the token ID
console.log(`- Minting additional supply on NFT with Token ID: ${tokenId} \n`);
console.log(`- Using account: ${operatorId} to pay`);
console.log('- Using ENIVRONMENT:', env);
console.log('- Planning to mint:', plannedMint);
console.log('- Using batch size:', nftBatchSize);
const execute = readlineSync.keyInYNStrict('Do wish to execute the mint?');
if (!execute) {
console.log('User aborted');
process.exit(0);
}
const elementListLength = Object.keys(pinnedMetadataObj).length;
console.log('Using mint batch size of', nftBatchSize);
for (let outer = 0; outer < elementListLength; outer += nftBatchSize) {
const tokenMintTx = new TokenMintTransaction();
const indexBeingProcessed = [];
for (let inner = 0; (inner < nftBatchSize) && ((outer + inner) < elementListLength); inner++) {
const pinCID = pinnedMetadataObj[outer + inner];
// if element marked as minted then skip.
if (pinCID.minted) continue;
indexBeingProcessed.push(outer + inner);
tokenMintTx.addMetadata(Buffer.from(pinCID.cid));
}
// assumes the account sending is treasury account
tokenMintTx
.setTokenId(tokenId)
.setMaxTransactionFee(10)
.freezeWith(client);
// sign
const signedTx = await tokenMintTx.sign(supplyKey);
// submit
try {
console.log('- MINTING items ->', indexBeingProcessed);
const tokenMintSubmit = await signedTx.execute(client);
// check it worked
const tokenMintRx = await tokenMintSubmit.getReceipt(client);
console.log('Tx processed - status:', tokenMintRx.status.toString());
if (tokenMintRx.status.toString() == 'SUCCESS') {
// mark the JSON entries as success
const mintedSerials = tokenMintRx.serials;
let serialIdx = 0;
console.log(`- Created NFT ${tokenId} with serial: ${mintedSerials}`);
for (let i = 0; i < indexBeingProcessed.length; i++) {
const idx = indexBeingProcessed[i];
const cid = pinnedMetadataObj[idx].cid;
pinnedMetadataObj[idx] = {
cid: cid,
minted: true,
serial: mintedSerials[serialIdx].toString(),
};
serialIdx++;
}
// write back out the updated file
await writeProgress(fileToProcess, pinnedMetadataObj);
}
else {
// mark the attempted file for preprocessing
for (let idx = 0; idx < indexBeingProcessed.length; idx++) {
const cid = pinnedMetadataObj[idx].cid;
pinnedMetadataObj[idx] = {
cid: cid,
minted: false,
};
}
// write back out the updated file
await writeProgress(fileToProcess, pinnedMetadataObj);
}
}
catch (err) {
console.log('Error occured executing tx:', err);
// mark the attempted file for preprocessing
for (let idx = 0; idx < indexBeingProcessed.length; idx++) {
const cid = pinnedMetadataObj[idx].cid;
pinnedMetadataObj[idx] = {
cid: cid,
minted: false,
};
}
// write back out the updated file
await writeProgress(fileToProcess, pinnedMetadataObj);
}
}
}
async function writeProgress(filepath, pinnedMetadataObj) {
// write back out the updated file
const filename = path.basename(filepath);
const saveFilename = path.join(path.dirname(filepath), 'output_' + env + '_' + filename.replace(/^\.\\/, ''));
const outputStr = JSON.stringify(pinnedMetadataObj, null, 4);
fs.writeFile(saveFilename, outputStr, { flag: 'w' }, function(err) {
if (err) {return console.error(err);}
// read it back in to be sure it worked.
fs.readFile(saveFilename, 'utf-8', function(err) {
if (err) {
console.log(outputStr);
return console.error(err);
}
console.log('Mints logged to DB file', saveFilename);
});
});
}
main();