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, prepare the encoded metadata and connect to your token contract.

set-token-metadata-ethers.js
import { ethers } from 'ethers';
import LSP7Artifact from '@lukso/lsp-smart-contracts/artifacts/LSP7DigitalAsset.json';

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

// Connect via the UP Browser Extension
const provider = new ethers.BrowserProvider(window.lukso);
await provider.send('eth_requestAccounts', []);
const signer = await provider.getSigner();

const myAssetAddress = '0x...';

// Instantiate the token contract
const token = new ethers.Contract(myAssetAddress, LSP7Artifact.abi, signer);

Set data on token​

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

set-token-metadata-ethers.js
// 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.hash);

Setting multiple data keys at once​

If you need to update multiple metadata keys simultaneously, use setDataBatch(bytes32[],bytes[]):

const tx = await token.setDataBatch(
encodedLSP4Metadata.keys,
encodedLSP4Metadata.values,
);
await tx.wait();