Skip to main content

Set LSP7 Token Metadata

πŸ‘‡πŸ» Hands on πŸ“½οΈ ethers.js workshop video for the Oxford Blockchain Society from March 2024.

In this guide, you will learn how to edit the LSP4Metadata of an LSP7 Digital Asset. You will need to:

  1. get your assets ready (images, videos, etc.) and create a metadata JSON file
  2. upload these files using a preferred storage provider
  3. generate an encoded value based on the URL to the metadata JSON file
  4. write that value in the smart contract key-value store

For steps 1 to 3, there is a dedicated Asset Preparation Guide. Once your assets are uploaded and the URL to your metadata JSON file is ready and encoded, come back here.

Show encoded LSP4 Object
const encodedLSP4Metadata = {
keys: ['0x9afb95cacc9f95858ec44aa8c3b685511002e30ae54415823f406128b85b238e'],
values: [
'0x00006f357c6a0020610be5a5ebf25a8323ed5a9d8735f78aaf97c7e3529da7249f17e1b4129636f3697066733a2f2f516d5154716865424c5a466e5155787535524473387441394a746b78665a714d42636d47643973756b587877526d',
],
};
Code repository

You can find all the contracts, sample metadata, and scripts of the guide within our lukso-playground repository.

Contract Deployment

If you want to learn more about the contract deployment itself, please have a look at the Create LSP7 Token guides before you continue.

Setup​

First, you have to prepare all imports and constants to send the transaction.

scripts/attachAssetMetadataAsEOA.ts
import { ethers } from 'hardhat';
import * as dotenv from 'dotenv';

// Load the environment variables
dotenv.config();

// As generated in the Asset guide
const encodedLSP4Metadata = {
keys: ['0x9afb95cacc9f95858ec44aa8c3b685511002e30ae54415823f406128b85b238e'],
values: [
'0x00006f357c6a0020610be5a5ebf25a8323ed5a9d8735f78aaf97c7e3529da7249f17e1b4129636f3697066733a2f2f516d5154716865424c5a466e5155787535524473387441394a746b78665a714d42636d47643973756b587877526d',
],
};

const [signer] = await ethers.getSigners();
const myAssetAddress = '0x...';

// Instantiate asset
const token = await ethers.Contract(myAssetAddress, LSP7Artifact.abi, signer);

Set data on token​

Once you have the data key and value (with the encoded VerifiableURI in it), simply call the setData() function of the Token contract.

In order to update the metadata using your EOA, you can call the setDataBatch() function directly on the asset contract.

scripts/attachAssetMetadataAsEOA.ts
// Update the ERC725Y storage of the LSP4 metadata
const tx = await token.setData(
encodedLSP4Metadata.keys[0],
encodedLSP4Metadata.values[0],
);

// Wait for the transaction to be included in a block
const receipt = await tx.wait();
console.log('Token metadata updated:', receipt);

Afterwards, you are able to run the deployment script:

npx hardhat --network luksoTestnet run scripts/attachAssetMetadataAsEOA.ts