|
1 | 1 | const fetch = require("node-fetch"); |
2 | 2 | const path = require("path"); |
3 | | -const isLocal = typeof process.pkg === "undefined"; |
4 | | -const basePath = isLocal ? process.cwd() : path.dirname(process.execPath); |
| 3 | +const basePath = process.cwd(); |
5 | 4 | const fs = require("fs"); |
6 | 5 |
|
7 | 6 | const AUTH = 'YOUR API KEY HERE'; |
8 | 7 | const CONTRACT_ADDRESS = 'YOUR CONTRACT ADDRESS HERE'; |
9 | 8 | const MINT_TO_ADDRESS = 'YOUR WALLET ADDRESS HERE'; |
10 | 9 | const CHAIN = 'rinkeby'; |
| 10 | +const TIMEOUT = 1000; // Milliseconds. This a timeout for errors only. If there is an error, it will wait then try again. 5000 = 5 seconds. |
11 | 11 |
|
12 | | -const ipfsMetas = JSON.parse( |
13 | | - fs.readFileSync(`${basePath}/build/json/_ipfsMetas.json`) |
14 | | -); |
15 | | - |
16 | | -fs.writeFileSync(`${basePath}/build/minted.json`, ""); |
17 | | -const writter = fs.createWriteStream(`${basePath}/build/minted.json`, { |
18 | | - flags: "a", |
19 | | -}); |
20 | | -writter.write("["); |
21 | | -nftCount = ipfsMetas.length; |
22 | | - |
23 | | -ipfsMetas.forEach((meta) => { |
24 | | - let url = "https://api.nftport.xyz/v0/mints/customizable"; |
25 | | - |
26 | | - const mintInfo = { |
27 | | - chain: CHAIN, |
28 | | - contract_address: CONTRACT_ADDRESS, |
29 | | - metadata_uri: meta.metadata_uri, |
30 | | - mint_to_address: MINT_TO_ADDRESS, |
31 | | - token_id: meta.custom_fields.edition, |
32 | | - }; |
33 | | - |
34 | | - let options = { |
35 | | - method: "POST", |
36 | | - headers: { |
37 | | - "Content-Type": "application/json", |
38 | | - Authorization: AUTH, |
39 | | - }, |
40 | | - body: JSON.stringify(mintInfo), |
41 | | - }; |
42 | | - |
43 | | - fetch(url, options) |
44 | | - .then((res) => res.json()) |
45 | | - .then((json) => { |
46 | | - writter.write(JSON.stringify(json, null, 2)); |
47 | | - nftCount--; |
48 | | - |
49 | | - if (nftCount === 0) { |
50 | | - writter.write("]"); |
51 | | - writter.end(); |
52 | | - } else { |
53 | | - writter.write(",\n"); |
| 12 | +if (!fs.existsSync(path.join(`${basePath}/build`, "/minted"))) { |
| 13 | + fs.mkdirSync(path.join(`${basePath}/build`, "minted")); |
| 14 | +} |
| 15 | + |
| 16 | +async function main() { |
| 17 | + const ipfsMetas = JSON.parse( |
| 18 | + fs.readFileSync(`${basePath}/build/ipfsMetas/_ipfsMetas.json`) |
| 19 | + ); |
| 20 | + |
| 21 | + for (const meta of ipfsMetas) { |
| 22 | + const mintFile = `${basePath}/build/minted/${meta.custom_fields.edition}.json`; |
| 23 | + |
| 24 | + try { |
| 25 | + fs.accessSync(mintFile); |
| 26 | + const mintedFile = fs.readFileSync(mintFile) |
| 27 | + if(mintedFile.length > 0) { |
| 28 | + const mintedMeta = JSON.parse(mintedFile) |
| 29 | + if(mintedMeta.mintData.response !== "OK") throw 'not minted' |
54 | 30 | } |
| 31 | + console.log(`${meta.name} already minted`); |
| 32 | + } catch(err) { |
| 33 | + try { |
| 34 | + let mintData = await fetchWithRetry(meta) |
| 35 | + const combinedData = { |
| 36 | + metaData: meta, |
| 37 | + mintData: mintData |
| 38 | + } |
| 39 | + writeMintData(meta.custom_fields.edition, combinedData) |
| 40 | + console.log(`Minted: ${meta.name}!`); |
| 41 | + } catch(err) { |
| 42 | + console.log(`Catch: ${err}`) |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +main(); |
| 49 | + |
| 50 | +function timer(ms) { |
| 51 | + return new Promise(res => setTimeout(res, ms)); |
| 52 | +} |
| 53 | + |
| 54 | +async function fetchWithRetry(meta) { |
| 55 | + await timer(TIMEOUT); |
| 56 | + return new Promise((resolve, reject) => { |
| 57 | + const fetch_retry = (_meta) => { |
| 58 | + let url = "https://api.nftport.xyz/v0/mints/customizable"; |
| 59 | + |
| 60 | + const mintInfo = { |
| 61 | + chain: CHAIN, |
| 62 | + contract_address: CONTRACT_ADDRESS, |
| 63 | + metadata_uri: _meta.metadata_uri, |
| 64 | + mint_to_address: MINT_TO_ADDRESS, |
| 65 | + token_id: _meta.custom_fields.edition, |
| 66 | + }; |
| 67 | + |
| 68 | + let options = { |
| 69 | + method: "POST", |
| 70 | + headers: { |
| 71 | + "Content-Type": "application/json", |
| 72 | + Authorization: AUTH, |
| 73 | + }, |
| 74 | + body: JSON.stringify(mintInfo), |
| 75 | + }; |
| 76 | + |
| 77 | + return fetch(url, options).then(async (res) => { |
| 78 | + const status = res.status; |
| 79 | + |
| 80 | + if(status === 200) { |
| 81 | + return res.json(); |
| 82 | + } |
| 83 | + else { |
| 84 | + console.error(`ERROR STATUS: ${status}`) |
| 85 | + console.log('Retrying') |
| 86 | + await timer(TIMEOUT) |
| 87 | + fetch_retry(_meta) |
| 88 | + } |
| 89 | + }) |
| 90 | + .then(async (json) => { |
| 91 | + if(json.response === "OK"){ |
| 92 | + return resolve(json); |
| 93 | + } else { |
| 94 | + console.error(`NOK: ${json.error}`) |
| 95 | + console.log('Retrying') |
| 96 | + await timer(TIMEOUT) |
| 97 | + fetch_retry(_meta) |
| 98 | + } |
| 99 | + }) |
| 100 | + .catch(async (error) => { |
| 101 | + console.error(`CATCH ERROR: ${error}`) |
| 102 | + console.log('Retrying') |
| 103 | + await timer(TIMEOUT) |
| 104 | + fetch_retry(_meta) |
| 105 | + }); |
| 106 | + } |
| 107 | + return fetch_retry(meta); |
| 108 | + }); |
| 109 | +} |
55 | 110 |
|
56 | | - console.log(`Minted: ${json.transaction_external_url}`); |
57 | | - }) |
58 | | - .catch((err) => console.error("error:" + err)); |
59 | | -}); |
| 111 | +const writeMintData = (_edition, _data) => { |
| 112 | + fs.writeFileSync(`${basePath}/build/minted/${_edition}.json`, JSON.stringify(_data, null, 2)); |
| 113 | +}; |
0 commit comments