-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.js
54 lines (41 loc) · 1.69 KB
/
store.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
require('dotenv').config();
const { NFTStorage, File, Blob } = require('nft.storage')
const fs = require('fs');
const client = new NFTStorage({ token: process.env.NFT_STORAGE_KEY })
const totalNfts = 200;
// store directory
async function storeNftDirectory(files) {
const cid = await client.storeDirectory(files);
return cid;
}
async function storeNftImages() {
const files = [];
for (let i = 0; i < totalNfts; i++) {
const imagePath = `./images/${i}.png`;
const imageName = i; //tokenId ---------> Will be shown in ipfs url
const imageFile = new File([await fs.promises.readFile(imagePath)], `${imageName}.png`, { type: 'image/png' });
files.push(imageFile);
}
const imageCid = await storeNftDirectory(files);
console.log(`nft images published successfully! ${imageCid}`);
return imageCid;
}
async function storeNfts() {
const imgsCid = await storeNftImages();
// update 'images' inside meta files
const files = [];
for (let i = 0; i < totalNfts; i++) {
const metaPath = `./metadata/${i}.json`;
const metaName = i; //tokenId
const data = await fs.promises.readFile(metaPath, 'utf-8');
const nftMetadata = JSON.parse(data);
nftMetadata.image = `ipfs://${imgsCid}/${metaName}.png`;
const updatedNftMetadata = JSON.stringify(nftMetadata, null, 2);
await fs.promises.writeFile(metaPath, updatedNftMetadata, 'utf-8');
const metaFile = new File([await fs.promises.readFile(metaPath)], `${metaName}.json`);
files.push(metaFile);
}
const metaCid = await storeNftDirectory(files);
console.log(`metadata published successfully! ${metaCid}`);
}
storeNfts();