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:
- get your assets ready (images, videos, etc.) and create a metadata JSON file
- upload these files using a preferred storage provider
- generate an encoded value based on the URL to the metadata JSON file
- 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',
],
};
You can find all the contracts, sample metadata, and scripts of the guide within our lukso-playground repository.
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.
- ethers
- web3
- Hardhat
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);
import Web3 from 'web3';
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 web3 = new Web3(window.lukso);
await web3.eth.requestAccounts();
const accounts = await web3.eth.getAccounts();
const myAssetAddress = '0x...';
// Instantiate the token contract
const token = new web3.eth.Contract(LSP7Artifact.abi, myAssetAddress);
import { ethers } from 'hardhat';
import LSP7Artifact from '@lukso/lsp-smart-contracts/artifacts/LSP7DigitalAsset.json';
import * as dotenv from 'dotenv';
// Load the environment variables
dotenv.config();
// As generated in the Asset Preparation guide
const encodedLSP4Metadata = {
keys: ['0x9afb95cacc9f95858ec44aa8c3b685511002e30ae54415823f406128b85b238e'],
values: [
'0x00006f357c6a0020610be5a5ebf25a8323ed5a9d8735f78aaf97c7e3529da7249f17e1b4129636f3697066733a2f2f516d5154716865424c5a466e5155787535524473387441394a746b78665a714d42636d47643973756b587877526d',
],
};
const [signer] = await ethers.getSigners();
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.
- ethers
- web3
- Hardhat
// 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);
// Update the ERC725Y storage of the LSP4 metadata
const receipt = await token.methods
.setData(encodedLSP4Metadata.keys[0], encodedLSP4Metadata.values[0])
.send({ from: accounts[0] });
console.log('β
Token metadata updated:', receipt.transactionHash);
// 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);
Run the script:
npx hardhat --network luksoTestnet run scripts/attachAssetMetadataAsEOA.ts
Setting multiple data keys at onceβ
If you need to update multiple metadata keys simultaneously, use setDataBatch(bytes32[],bytes[]):
- ethers
- web3
- Hardhat
const tx = await token.setDataBatch(
encodedLSP4Metadata.keys,
encodedLSP4Metadata.values,
);
await tx.wait();
await token.methods
.setDataBatch(encodedLSP4Metadata.keys, encodedLSP4Metadata.values)
.send({ from: accounts[0] });
const tx = await token.setDataBatch(
encodedLSP4Metadata.keys,
encodedLSP4Metadata.values,
);
await tx.wait();