Skip to content

Instantly share code, notes, and snippets.

@ZaK3939
Created September 21, 2023 17:50
Show Gist options
  • Save ZaK3939/f99f97e625c44003ff8dd6806e066430 to your computer and use it in GitHub Desktop.
Save ZaK3939/f99f97e625c44003ff8dd6806e066430 to your computer and use it in GitHub Desktop.
import axios from "axios";
import { ethers } from "ethers";
import fs from "fs";
import WawaNFT from "../lambda-fn/utility/abi/WawaNFT.json";
async function fetchMetadata() {
const provider = new ethers.providers.AlchemyProvider("homestead", "<ALCHEMY_API_KEY_ETH>");
const contractAddress = "0x2d9181b954736971bb74043d4782dfe93b55a9af";
const contract = new ethers.Contract(contractAddress, WawaNFT.abi, provider);
const totalSupply = await contract.totalSupply();
console.log("Total supply: ", totalSupply.toString());
const metadataList = [];
for (let i = 1; i <= totalSupply; i++) {
const tokenURI = await contract.tokenURI(i);
const response = await axios.get(tokenURI);
console.log(response.data, tokenURI, response.data.properties.files[2].uri);
metadataList.push(response.data.properties.files[2].uri);
}
fs.writeFileSync("metadataList.json", JSON.stringify(metadataList, null, 2));
console.log("Metadata list saved to metadataList.json");
}
async function downloadImage(url: string, filename: string) {
const response = await axios.get(url, { responseType: "arraybuffer" });
fs.writeFileSync(filename, response.data);
}
async function main() {
await fetchMetadata().catch(console.error);
const rawData = fs.readFileSync("metadataList.json", "utf-8");
const urls: string[] = JSON.parse(rawData);
for (let i = 0; i < urls.length; i++) {
const url = urls[i];
await downloadImage(url, `./scripts/data/wawa/${i + 1}.png`);
console.log(`Downloaded ${i + 1}.png`);
}
}
main().catch(error => {
console.error("Error downloading images:", error);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment