From 6a5215264be98c387f1a147a3fbc9ae587125be8 Mon Sep 17 00:00:00 2001 From: yuetloo Date: Mon, 17 Jun 2024 20:38:19 -0400 Subject: [PATCH] add task to export project logo images --- contracts/tasks/index.ts | 1 + contracts/tasks/runners/exportImages.ts | 74 +++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 contracts/tasks/runners/exportImages.ts diff --git a/contracts/tasks/index.ts b/contracts/tasks/index.ts index 19050a901..98c143c27 100644 --- a/contracts/tasks/index.ts +++ b/contracts/tasks/index.ts @@ -15,6 +15,7 @@ import './runners/finalize' import './runners/claim' import './runners/cancel' import './runners/exportRound' +import './runners/exportImages' import './runners/mergeAllocation' import './runners/loadSimpleUsers' import './runners/loadMerkleUsers' diff --git a/contracts/tasks/runners/exportImages.ts b/contracts/tasks/runners/exportImages.ts new file mode 100644 index 000000000..b353b30b9 --- /dev/null +++ b/contracts/tasks/runners/exportImages.ts @@ -0,0 +1,74 @@ +/** + * Export the project logo images in a ClrFund round. + * + * Sample usage: + * yarn hardhat export-images \ + * --output-dir ../vue-apps/public/ipfs + * --gateway https://ipfs.io + * --round-file ../vue-app/src/rounds/arbitrum/0x4A2d90844EB9C815eF10dB0371726F0ceb2848B0.json + * + * Notes: + * 1) This script assumes the round has been exported using the `export-round` hardhat task + */ + +import { task } from 'hardhat/config' +import { FetchRequest } from 'ethers' +import { isPathExist, makeDirectory } from '../../utils/misc' +import fs from 'fs' + +/** + * Download the IPFS file with the ipfsHash to the output directory + * @param baseUrl IPFS gateway base url + * @param ipfsHash IPFS hash of the file to download + * @param outputDir The directory to store the downloaded file + */ +async function download({ + baseUrl, + ipfsHash, + outputDir, +}: { + baseUrl: string + ipfsHash: string + outputDir: string +}) { + const url = new URL(ipfsHash, baseUrl) + const req = new FetchRequest(url.href) + const res = await req.send() + if (res.hasBody()) { + console.log('Downloading', ipfsHash) + fs.writeFileSync(`${outputDir}/${ipfsHash}`, res.body) + } +} + +task('export-images', 'Export project logo images') + .addParam('outputDir', 'The output directory') + .addParam('roundFile', 'The exported funding round file path') + .addParam('gateway', 'The IPFS gateway url') + .setAction(async ({ outputDir, roundFile, gateway }) => { + console.log('Starting to download from ipfs') + + if (!isPathExist(outputDir)) { + makeDirectory(outputDir) + } + + const data = fs.readFileSync(roundFile, { encoding: 'utf-8' }) + const round = JSON.parse(data) + const projects = round.projects + const images = projects.map((project: any) => { + const { bannerImageHash, thumbnailImageHash } = project.metadata + return { bannerImageHash, thumbnailImageHash } + }) + + for (let i = 0; i < images.length; i++) { + await download({ + baseUrl: gateway, + ipfsHash: images[i].bannerImageHash, + outputDir, + }) + await download({ + baseUrl: gateway, + ipfsHash: images[i].thumbnailImageHash, + outputDir, + }) + } + })