πŸ–οΈPushing File Metadata Onchain

In this section, we will push uploaded file metadata on-chain through the OnchainCID Contract. Initially, we will push the file info like filename, filesize, mime-type, etc on-chain followed by updating dealIDs for that CID after a successful deal creation of the file.

Make sure to go through the quick start section before proceeding. The complete script can be copied from here

Install and import all the required modules

const lighthouse = require("@lighthouse-web3/sdk");
const ethers = require("ethers");
const OnChainCIDabi = require("./utils/abi");
const chainConfig = require("./utils/chainConfig");
require("dotenv").config();

Paste the Lighthouse API Key and signing wallet private key

const API_KEY = process.env.LIGHTHOUSE_API_KEY;
const PRIVATE_KEY = process.env.PRIVATE_KEY;

Declare the Rpc Url and onchainCID contract address either by passing the chain name or pasting it from chain config.

const chain = "baseSepolia"; // Update chain here or

// Update rpc and contract address here
const RPC_URL = chainConfig[chain].rpcUrl;
const CONTRACT_ADDRESS = chainConfig[chain].contractAddress;

Supported chains include -

  • Mainnet - polygon, base, filecoin

  • Testnet - polygonAmoy, baseSepolia, calibration

Creating the signer and contract instance

Now to add a file CID and metadata on-chain, either upload a new file using Lighthouse SDK

Or paste an uploaded file’s CID

Retrieve the file info and execute the pushCIDOnchain function

View the file metadata on the chain

you will get a response like this -

After the successful deal creation of the file, deal IDs can be updated for that CID. Fetch the deal status and then execute the updateDealID function of the contract.

Now the file metadata can be viewed again

This time, the response will include the dealIDs

Last updated

Was this helpful?