Created
March 20, 2025 17:55
-
-
Save tmavroeid/88814cef2d8d72a484bfd134c058bbdf to your computer and use it in GitHub Desktop.
fetch CID from smart contract and fetch IPFS json file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require("dotenv").config(); | |
| const { Web3} = require("web3"); | |
| const axios = require("axios"); | |
| const fs = require("fs"); | |
| const RPC_URL = process.env.RPC_URL; | |
| const CONTRACT_ADDRESS = process.env.CONTRACT_ADDRESS; | |
| const IPFS_GATEWAY = process.env.IPFS_GATEWAY || "https://ipfs.io/ipfs"; | |
| const DATE_ENV = process.env.DATE; | |
| if (!DATE_ENV) { | |
| console.error("❌ ERROR: DATE environment variable is missing. Please set DATE=YYYY-MM-DD in .env"); | |
| process.exit(1); | |
| } | |
| const dateFormatted = parseInt(DATE_ENV.replace(/-/g, ""), 10); | |
| const contractABI = JSON.parse(fs.readFileSync("./IPFSIndexer.json")); | |
| const provider = new Web3(RPC_URL) | |
| const contract = new provider.eth.Contract(contractABI, CONTRACT_ADDRESS); | |
| async function fetchCID(date, platform, betName) { | |
| try { | |
| console.log(`Fetching CID for Date: ${date}, Platform: ${platform}, Bet: ${betName}`); | |
| const cid = await contract.methods.getCIDByDateBetPlatform(date, platform, betName).call(); | |
| if(cid){ | |
| console.log(`✅ Retrieved CID: ${cid}`); | |
| } | |
| return cid; | |
| } catch (error) { | |
| console.error("❌ Error fetching CID from contract:", error); | |
| } | |
| } | |
| // Function to download JSON from IPFS | |
| async function downloadFromIPFS(cid, savePath = `./downloaded_${cid}.json`) { | |
| try { | |
| console.log(`Fetching file from IPFS CID: ${cid}`); | |
| let response; | |
| try { | |
| response = await axios.get(`${IPFS_GATEWAY}/${cid}`, { responseType: "stream" }); | |
| } catch (error) { | |
| console.log("⚠️ Public IPFS gateway failed. Trying Pinata..."); | |
| response = await axios.get(`https://gateway.pinata.cloud/ipfs/${cid}`, { responseType: "stream" }); | |
| } | |
| const writer = fs.createWriteStream(savePath); | |
| response.data.pipe(writer); | |
| return new Promise((resolve, reject) => { | |
| writer.on("finish", () => { | |
| console.log(`✅ File downloaded from IPFS: ${savePath}`); | |
| resolve(savePath); | |
| }); | |
| writer.on("error", reject); | |
| }); | |
| } catch (error) { | |
| console.error("❌ Error downloading from IPFS:", error); | |
| } | |
| } | |
| (async () => { | |
| try { | |
| const platform = process.env.BETTING_PLATFORM; | |
| const betName = process.env.BET_NAME; | |
| const cid = await fetchCID(dateFormatted, platform, betName); | |
| if (cid) { | |
| await downloadFromIPFS(cid); | |
| console.log("✅ Script completed successfully."); | |
| process.exit(0); | |
| } else { | |
| console.log("❌ No CID found for the given date, platform, and bet."); | |
| process.exit(1); | |
| } | |
| } catch (error) { | |
| console.error("❌ Unexpected error:", error); | |
| process.exit(1); | |
| } | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment