-
Notifications
You must be signed in to change notification settings - Fork 3
/
uploadNFT.js
157 lines (144 loc) · 4.95 KB
/
uploadNFT.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
// call the config method that will read our environmental variables file and save them
require('dotenv').config();
const axios = require('axios');
const chalk = require('chalk');
const fs = require('fs');
const imageToBase64 = require('image-to-base64');
let unsortedFiles = [];
let filesToUpload = [];
//* ************************************************** //
//* *************** Helper Functions ***************** //
//* ************************************************** //
function compareFileNames(f1, f2) {
let name1 = f1.split('.')[0];
let name2 = f2.split('.')[0];
return name1 === name2;
}
//* ************************************************** //
//* ****** Data Staging and Upload Functions ********* //
//* ************************************************** //
// A Promise represents a value which may be available now, or in the future, or never.
// In order to create a new promise we need to instantiate a new promise object
// Now since this takes a callback add the resolve and reject to it
const getFiles = () => {
return new Promise((resolve, reject) => {
fs.readdir('./racecars/', (err, files) => {
if (err) {
reject(err);
}
files.forEach((file) => {
unsortedFiles.push(file);
});
resolve(true);
});
});
}
// Loops through the "unsortedFiles" array and compares the file names
// If there is a match they get put inside their own array
const modifyFilesForUpload = async () => {
for (let index = 0; index < unsortedFiles.length; ++index) {
const element1 = unsortedFiles[index];
const element2 = unsortedFiles[index + 1];
if (index + 1 === unsortedFiles.length) {
return;
}
if (compareFileNames(element1, element2)) {
await imageToBase64('./racecars/' + element2) // path to the image
.then((response) => {
filesToUpload.push({
fileReference: element2.split('.')[0],
meta: fs.readFileSync('./racecars/' + element1, {
encoding: 'utf8',
flag: 'r'
}),
img: response,
});
})
.catch((error) => {
console.log(chalk.yellow.bold(`Error in imageToBase64 ${error} .............`));
});
}
}
}
async function uploadMyNFT(metadata, assetNumber, base64ImageData) {
const body = {
AssetName: assetNumber,
previewImageNft: {
mimetype: 'image/png',
fileFromBase64: base64ImageData,
metadataPlaceHolder: [
{
name: 'car',
value: metadata.car,
},
{
name: 'background',
value: metadata.background,
},
{
name: 'helmet',
value: metadata.helmet,
},
{
name: 'driverName',
value: metadata.driverName,
},
{
name: 'sexuality',
value: metadata.sexuality,
},
{
name: 'topSpeed',
value: metadata.topSpeed,
},
],
},
};
const data = JSON.stringify(body);
const config = {
method: 'post',
url: `https://api.nft-maker.io/UploadNft/${process.env.API_KEY}/${process.env.PROJECT_ID}`,
headers: {
accept: 'text/plain',
'Content-Type': 'application/json'
},
data: data,
};
await axios(config)
.then(function (response) {
console.log(chalk.blueBright.bgBlackBright.bold(`🚀 Success ${assetNumber} has been uploaded`));
})
.catch(function (error) {
console.log(chalk.yellow.bold(`Whoops!!! ${error} ............... 🖕🏿`));
});
}
//* ************************************************** //
//* ***************** Run the script ***************** //
//* ************************************************** //
// The purpose of a generator is to run some code and then return a value, and then run some more code and return another value
// Make an object that asynchronously generates a sequence of values
// The yield keyword causes the call to the generator's next()
// The next() method should return a promise (to be fulfilled with the next value).
async function* asyncGenerator() {
let i = 0; // //The asset number to start on
yield getFiles();
yield modifyFilesForUpload();
while (i < 5) {
const metadata = JSON.parse(filesToUpload[i].meta);
const assetNumber = filesToUpload[i].fileReference.padStart(5, '0');
const base64ImageData = filesToUpload[i].img;
yield uploadMyNFT(metadata, assetNumber, base64ImageData);
yield i++;
}
}
// The for await...of statement creates a loop iterating over async iterable objects
// In order to use the generator we need to run the generator function which will return a generator object that allows use to use the generator
(async function () {
// As the generator is asynchronous, we can use await inside it,
for await (let num of asyncGenerator()) {
if (num === 4) {
console.log(chalk.magentaBright.bold('Finished generating images, now Axios will upload the shit. lmao...'));
process.exitCode = 0;
}
}
})();