UNPKG

@vechain/vebetterdao-contracts

Version:

Open-source repository that houses the smart contracts powering the decentralized VeBetterDAO on the VeChain Thor blockchain.

110 lines (109 loc) 4.54 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const promises_1 = __importDefault(require("fs/promises")); const path_1 = __importDefault(require("path")); const helpers_1 = require("../../helpers"); const const_1 = require("./const"); /** * Converts a record of attributes into an array of `Attribute` objects. * * @param attributes - A record object containing the attributes to convert. * @returns An array of `Attribute` objects. */ function convertAttributes(attributes) { return Object.entries(attributes).map(([key, value]) => ({ trait_type: key, value })); } /** * Generates the NFT metadata for a given level. * * @param name - The name of the level. * @param description - The description of the level. * @param imagesCID - The CID of the images directory on IPFS. * @param attributes - The attributes of the level. * @param image - The image file for the level. * * @returns The generated NFT metadata. */ function generateMetadata(name, description, attributes, image) { return { name, description, image: image, attributes: convertAttributes(attributes), }; } /** * Asynchronously saves the generated NFT metadata. * @param metadata - The `Metadata` object to save. */ async function saveMetadataToFile(metadata, fileName) { await promises_1.default.writeFile(`${const_1.METADATA_PATH}/${fileName}.json`, JSON.stringify(metadata, null, 2)); console.log(`Metadata saved to ${const_1.METADATA_PATH}/${fileName}`); } /** * Asynchronously gets the NFT images from the IMAGE_PATH. * @returns An array of NFT image file names, sorted numerically. */ async function getNFTImages() { //Get only the png files in the IMAGE_PATH const images = (await promises_1.default.readdir(const_1.IMAGE_PATH)).filter(file => file.toLowerCase().endsWith(".png")); // Sort numerically by extracting the number from the filename return images.sort((a, b) => { const numA = parseInt(a.match(/\d+/)?.[0] || "0"); const numB = parseInt(b.match(/\d+/)?.[0] || "0"); return numA - numB; }); } /** * Asynchronously gets the image blob for a given image name. * @param imageName - The name of the image to get the blob for. * @returns A promise that resolves to the image blob. */ async function getImageBlob(imageName) { const imagePath = path_1.default.join(const_1.IMAGE_PATH, imageName); const imageBuffer = await promises_1.default.readFile(imagePath); return new Blob([imageBuffer], { type: "image/png" }); } /** * Main function to generate and save NFT metadata. */ async function generateAndSaveMetadata() { try { // 1. Check if levelNames matches the amount of images in the IMAGE_PATH const images = await getNFTImages(); if (const_1.GM_IMAGES_DATA.length !== images.length) { throw new Error("Galaxy Member images and data do not match"); } // 2. Upload each image individually and store the CID const imageCids = await Promise.all(const_1.GM_IMAGES_DATA.map(async ({ levelName }, index) => { console.log(`Uploading image ${levelName}, path -> ${images[index]}`); const imageBlob = await getImageBlob(images[index]); const cid = await (0, helpers_1.uploadBlobToIPFS)(imageBlob, images[index]); console.log(`GM Image ${levelName} uploaded to CID -> ${cid}`); return cid; })); // 3. Generate and save the metadata for each level await Promise.all(const_1.GM_IMAGES_DATA.map(async ({ levelName, levelAttributes }, index) => { //Format the image CID to be used in the metadata (eg. ipfs://<cid>) const image = (0, helpers_1.toIPFSURL)(imageCids[index]); const metadata = generateMetadata(levelName, const_1.description, levelAttributes, image); const fileName = String(index + 1); return await saveMetadataToFile(metadata, fileName); })); return; } catch (error) { console.error("Error generating metadata:", error); throw error; // Rethrow the error after logging to handle it further up the call stack. } } // Generate and save the NFT metadata generateAndSaveMetadata() .then(() => process.exit(0)) .catch(error => { console.error("Unhandled error:", error); process.exit(1); });