forked from UniqueNetwork/nft-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstep4-create-items.js
More file actions
62 lines (53 loc) · 1.98 KB
/
step4-create-items.js
File metadata and controls
62 lines (53 loc) · 1.98 KB
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
const config = require('./config');
const faces = require(`${config.outputFolder}/${config.outputJSON}`);
const initializeSdk = require('./scripts/initialize-sdk');
const { readData } = require('./scripts/utils');
async function main() {
console.log('=== Create items ===');
const collectionId = await readData('collectionId');
if (!collectionId) throw new Error('not found collectionId');
const { sdk, signer } = await initializeSdk();
const { tokenId: lastTokenId } = await sdk.collections.lastTokenId({ collectionId });
const offset = lastTokenId || 0;
if (config.desiredCount <= offset) {
console.log('tokens already created');
return;
}
const data = Array(config.desiredCount)
.slice(offset || 0)
.fill({})
.map((el, i) => {
const n = i + 1;
const image = { urlInfix: `${config.imagePrefix}${n}.png`};
const encodedAttributes = {};
faces[i].forEach((el, j) => {
if (el) {
encodedAttributes[j] = el - 1;
}
});
return {
data: {
image,
encodedAttributes
}
}
});
let result = [];
let chunkNumber = 0;
while (result.length + offset < config.desiredCount) {
if (chunkNumber > config.desiredCount / config.numberOfTokensGeneratedAtOnce) throw new Error('unexpected value chunkNumber');
const chunkData = data.slice(chunkNumber * config.numberOfTokensGeneratedAtOnce, (chunkNumber + 1) * config.numberOfTokensGeneratedAtOnce);
const { parsed } = await sdk.tokens.createMultiple.submitWaitResult({
address: signer.instance.address,
collectionId: collectionId,
tokens: chunkData
});
result = [ ...result, ...parsed];
await new Promise(resolve => setTimeout(resolve, 1000));
chunkNumber++;
console.log(`successfully created ${chunkNumber} part of tokens`);
}
console.log('Items created');
console.log(`Token Ids: ${result.map(el => el.tokenId).join(', ')}`);
}
main().catch(console.error).finally(() => process.exit());