Skip to content

Commit 73d3d04

Browse files
committed
major update adding error checks
1 parent b83adc7 commit 73d3d04

File tree

11 files changed

+209
-121
lines changed

11 files changed

+209
-121
lines changed

banner.png

-431 KB
Binary file not shown.

layers/Background/Blue.png

1.88 KB
Loading

layers/Background/Orange.png

1.88 KB
Loading

layers/Background/Yellow.png

1.88 KB
Loading

logo.png

-36.8 KB
Binary file not shown.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "10k-collection-video",
3-
"version": "1.0",
3+
"version": "1.0.0",
44
"description": "Source code from \"How To Create An ENTIRE NFT Collection (10,000+) & MINT In Under 1 Hour Without Coding Knowledge\" video.",
55
"main": "index.js",
66
"bin": "index.js",

src/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ const background = {
7979
};
8080

8181
const extraMetadata = {
82-
external_url: "https://codecats.xyz",
82+
external_url: "https://codecats.xyz", // Replace with your website or remove this line if you do not have one.
8383
};
8484

8585
const rarityDelimiter = "#";

utils/nftport/genericMetas.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const path = require("path");
2-
const isLocal = typeof process.pkg === "undefined";
3-
const basePath = isLocal ? process.cwd() : path.dirname(process.execPath);
2+
const basePath = process.cwd();
43
const fs = require("fs");
54
const buildDir = path.join(basePath, "/build");
65

utils/nftport/mint.js

Lines changed: 51 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ const AUTH = 'YOUR API KEY HERE';
77
const CONTRACT_ADDRESS = 'YOUR CONTRACT ADDRESS HERE';
88
const MINT_TO_ADDRESS = 'YOUR WALLET ADDRESS HERE';
99
const CHAIN = 'rinkeby';
10-
const TIMEOUT = 5000; // Milliseconds. This a timeout for errors only. If there is an error, it will wait then try again. 5000 = 5 seconds.
11-
const mintedArray = [];
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.
1211

1312
if (!fs.existsSync(path.join(`${basePath}/build`, "/minted"))) {
1413
fs.mkdirSync(path.join(`${basePath}/build`, "minted"));
@@ -20,29 +19,37 @@ async function main() {
2019
);
2120

2221
for (const meta of ipfsMetas) {
22+
const mintFile = `${basePath}/build/minted/${meta.custom_fields.edition}.json`;
23+
2324
try {
24-
let mintData = await fetchWithRetry(meta)
25-
mintedArray.push(mintData);
26-
console.log(`Minted: ${meta.name}`);
27-
const combinedData = {
28-
metaData: meta,
29-
mintData: mintData
30-
}
31-
writeMintData(meta.custom_fields.edition, combinedData)
25+
fs.accessSync(mintFile);
26+
console.log(`${meta.name} already minted`);
3227
} catch(err) {
33-
console.log(err)
28+
try {
29+
let mintData = await fetchWithRetry(meta)
30+
const combinedData = {
31+
metaData: meta,
32+
mintData: mintData
33+
}
34+
writeMintData(meta.custom_fields.edition, combinedData)
35+
console.log(`Minted: ${meta.name}!`);
36+
} catch(err) {
37+
console.log(`Catch: ${err}`)
38+
}
3439
}
3540
}
3641
}
3742

3843
main();
3944

45+
function timer(ms) {
46+
return new Promise(res => setTimeout(res, ms));
47+
}
48+
4049
async function fetchWithRetry(meta) {
50+
await timer(TIMEOUT);
4151
return new Promise((resolve, reject) => {
42-
let numberOfRetry = 10;
43-
let attempts = 1;
44-
45-
const fetch_retry = (_meta, _n) => {
52+
const fetch_retry = (_meta) => {
4653
let url = "https://api.nftport.xyz/v0/mints/customizable";
4754

4855
const mintInfo = {
@@ -62,37 +69,37 @@ async function fetchWithRetry(meta) {
6269
body: JSON.stringify(mintInfo),
6370
};
6471

65-
return fetch(url, options).then(res => {
66-
const status = res.status;
72+
return fetch(url, options).then(async (res) => {
73+
const status = res.status;
6774

68-
if(status === 200) {
69-
return resolve(res.json());
70-
}
71-
else if (_n === 1) {
72-
throw reject("Too many attempts.. Error in getting http data");
73-
}
74-
else {
75-
console.log("Retry again: Got back " + status);
76-
console.log("With delay " + attempts * TIMEOUT);
77-
setTimeout(() => {
78-
attempts++;
79-
80-
fetch_retry(_meta, _n - 1);
81-
}, attempts * TIMEOUT);
82-
}
83-
}).catch(function (error) {
84-
if (_n === 1) {
85-
reject(error)
86-
}
87-
else {
88-
setTimeout(() => {
89-
attempts++
90-
fetch_retry(_meta, _n - 1);
91-
}, attempts * TIMEOUT);
92-
}
75+
if(status === 200) {
76+
return res.json();
77+
}
78+
else {
79+
console.error(`ERROR STATUS: ${status}`)
80+
console.log('Retrying')
81+
await timer(TIMEOUT)
82+
fetch_retry(_meta)
83+
}
84+
})
85+
.then(async (json) => {
86+
if(json.response === "OK"){
87+
return resolve(json);
88+
} else {
89+
console.error(`NOK: ${json.error}`)
90+
console.log('Retrying')
91+
await timer(TIMEOUT)
92+
fetch_retry(_meta)
93+
}
94+
})
95+
.catch(async (error) => {
96+
console.error(`CATCH ERROR: ${error}`)
97+
console.log('Retrying')
98+
await timer(TIMEOUT)
99+
fetch_retry(_meta)
93100
});
94-
}
95-
return fetch_retry(meta, numberOfRetry);
101+
}
102+
return fetch_retry(meta);
96103
});
97104
}
98105

utils/nftport/uploadFiles.js

Lines changed: 74 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,36 @@ const fs = require("fs");
77
const AUTH = 'YOUR API KEY HERE';
88
const TIMEOUT = 1000; // Milliseconds. Extend this if needed to wait for each upload. 1000 = 1 second.
99

10+
const allMetadata = [];
11+
1012
async function main() {
1113
const files = fs.readdirSync(`${basePath}/build/images`);
14+
files.sort(function(a, b){
15+
return a.split(".")[0] - b.split(".")[0];
16+
});
1217
for (const file of files) {
13-
await uploadFile(file);
18+
const fileName = path.parse(file).name;
19+
let jsonFile = fs.readFileSync(`${basePath}/build/json/${fileName}.json`);
20+
let metaData = JSON.parse(jsonFile);
21+
if(!metaData.file_url.includes('https://')) {
22+
const response = await fetchWithRetry(file);
23+
metaData.file_url = response.ipfs_url;
24+
25+
fs.writeFileSync(
26+
`${basePath}/build/json/${fileName}.json`,
27+
JSON.stringify(metaData, null, 2)
28+
);
29+
console.log(`${response.file_name} uploaded & ${fileName}.json updated!`);
30+
} else {
31+
console.log(`${fileName} already uploaded.`);
32+
}
33+
34+
allMetadata.push(metaData);
1435
}
36+
fs.writeFileSync(
37+
`${basePath}/build/json/_metadata.json`,
38+
JSON.stringify(allMetadata, null, 2)
39+
);
1540
}
1641

1742
main();
@@ -20,36 +45,53 @@ function timer(ms) {
2045
return new Promise(res => setTimeout(res, ms));
2146
}
2247

23-
async function uploadFile(file) {
24-
await timer(TIMEOUT);
25-
const formData = new FormData();
26-
const fileStream = fs.createReadStream(`${basePath}/build/images/${file}`);
27-
formData.append("file", fileStream);
28-
29-
let url = "https://api.nftport.xyz/v0/files";
30-
let options = {
31-
method: "POST",
32-
headers: {
33-
Authorization: AUTH,
34-
},
35-
body: formData,
36-
};
37-
38-
fetch(url, options)
39-
.then((res) => res.json())
40-
.then((json) => {
41-
const fileName = path.parse(json.file_name).name;
42-
let rawdata = fs.readFileSync(`${basePath}/build/json/${fileName}.json`);
43-
let metaData = JSON.parse(rawdata);
44-
45-
metaData.file_url = json.ipfs_url;
48+
async function fetchWithRetry(file) {
49+
await timer(TIMEOUT)
50+
return new Promise((resolve, reject) => {
51+
const fetch_retry = (_file) => {
52+
const formData = new FormData();
53+
const fileStream = fs.createReadStream(`${basePath}/build/images/${_file}`);
54+
formData.append("file", fileStream);
4655

47-
fs.writeFileSync(
48-
`${basePath}/build/json/${fileName}.json`,
49-
JSON.stringify(metaData, null, 2)
50-
);
56+
let url = "https://api.nftport.xyz/v0/files";
57+
let options = {
58+
method: "POST",
59+
headers: {
60+
Authorization: AUTH,
61+
},
62+
body: formData,
63+
};
64+
65+
return fetch(url, options).then(async (res) => {
66+
const status = res.status;
5167

52-
console.log(`${json.file_name} uploaded & ${fileName}.json updated!`);
53-
})
54-
.catch((err) => console.error("error:" + err));
55-
}
68+
if(status === 200) {
69+
return res.json();
70+
}
71+
else {
72+
console.error(`ERROR STATUS: ${status}`)
73+
console.log('Retrying')
74+
await timer(TIMEOUT)
75+
fetch_retry(_file)
76+
}
77+
})
78+
.then(async (json) => {
79+
if(json.response === "OK"){
80+
return resolve(json);
81+
} else {
82+
console.error(`NOK: ${json.error}`)
83+
console.log('Retrying')
84+
await timer(TIMEOUT)
85+
fetch_retry(_file)
86+
}
87+
})
88+
.catch(async (error) => {
89+
console.error(`CATCH ERROR: ${error}`)
90+
console.log('Retrying')
91+
await timer(TIMEOUT)
92+
fetch_retry(_file)
93+
});
94+
}
95+
return fetch_retry(file);
96+
});
97+
}

0 commit comments

Comments
 (0)