Set LSP8 NFT Metadata
In this guide you will learn how to set the metadata of a specific NFT (represented by its bytes32 tokenId
) part of a LSP8 Identifiable Digital Asset collection.
Setupâ
You will need the address of an existing LSP8 NFT collection in order to follow this tutorial. This represent the LSP8 collection that includes the NFT you want to update metadata for. For instance 0x37934A275EFd47DFce671CA3CBaE34d9138CF2D2.
The following code snippets require the installation of the following libraries:
- web3.js
- ethers.js
npm install web3 @lukso/lsp-smart-contracts
npm install ethers @lukso/lsp-smart-contracts
Imports and constantsâ
Import web3.js
/ethers
, the LSP8IdentifiableDigitalAsset
ABI from @lukso/lsp-smart-contracts
and create an instance of this contract with the lsp8ContractAddress
.
- web3.js
- ethers.js
import Web3 from 'web3';
// import smart contract ABI
import LSP8IdentifiableDigitalAsset from '@lukso/lsp-smart-contracts/artifacts/LSP8IdentifiableDigitalAsset.json';
const web3 = new Web3(window.lukso);
await web3.eth.requestAccounts();
const accounts = await web3.eth.getAccounts();
const lsp8ContractAddress = '0x...';
import { ethers } from 'ethers';
// import smart contract ABI
import LSP8IdentifiableDigitalAsset from '@lukso/lsp-smart-contracts/artifacts/LSP8IdentifiableDigitalAsset.json';
const provider = new ethers.BrowserProvider(window.lukso);
await provider.send('eth_requestAccounts', []);
const signer = await provider.getSigner();
const lsp8ContractAddress = '0x...';
Instantiate the LSP8 contractâ
Create an instance of the LSP8 collection contract as shown below:
- web3.js
- ethers.js
const myToken = new web3.eth.Contract(
LSP8IdentifiableDigitalAsset.abi,
lsp8ContractAddress,
);
const lsp8Contract = new ethers.Contract(
lsp8ContractAddress,
LSP8IdentifiableDigitalAsset.abi,
);
Send the transaction to set metadata for a tokenIdâ
The last step is to set the metadata for a specific tokenId
. You can then send the transaction to set the metadata for the given NFT represented by its tokenId
.
Here we will use the following parameters as examples:
tokenId
:0x0000000000000000000000000000000000000000000000000000000000000001
dataKey
:LSP4Metadata
metadataValue
: some placeholder value as aVerifiableURI
.
Note that the
tokenId
is abytes32
value (hex)
You can use the encodeData
function from the erc725.js library to encode a VerifiableURI
easily.
Code example to encode a VerifiableURI
using erc725.js
import { ERC725 } from '@erc725/erc725.js';
const schema = [
{
name: 'LSP4Metadata',
key: '0x9afb95cacc9f95858ec44aa8c3b685511002e30ae54415823f406128b85b238e',
keyType: 'Singleton',
valueType: 'bytes',
valueContent: 'VerifiableURI',
},
];
const myErc725 = new ERC725(schema);
myErc725.encodeData([
{
keyName: 'LSP4Metadata',
value: {
json: nftJson,
url: 'ipfs://QmQTqheBLZFnQUxu5RDs8tA9JtkxfZqMBcmGd9sukXxwRm',
},
},
]);
You can also check the code snippet example in the LSP2 specs to learn in details the encoding of a VerifiableURI
.
- web3.js
- ethers.js
import { ERC725YDataKeys } from '@lukso/lsp-smart-contracts';
// import and instantiate contract from previous steps
// change this with the actual tokenId to update metadata for
const tokenId =
'0x0000000000000000000000000000000000000000000000000000000000000001';
// change this with the actual VerifiableURI of the metadata uploaded somewhere (S3, IPFS, etc...)
const metadataValue = '0x...';
await lsp8Contract.methods
.setDataForTokenId(
tokenId,
ERC725YDataKeys.LSP4['LSP4Metadata'],
metadataValue,
)
.send({ from: accounts[0] });
import { ERC725YDataKeys } from '@lukso/lsp-smart-contracts';
// import and instantiate contract from previous steps
// change this with the actual tokenId to update metadata for
const tokenId =
'0x0000000000000000000000000000000000000000000000000000000000000001';
// change this with the actual VerifiableURI of the metadata uploaded somewhere (S3, IPFS, etc...)
const metadataValue = '0x...';
await lsp8Contract.setDataForTokenId(
tokenId,
ERC725YDataKeys.LSP4['LSP4Metadata'],
metadataValue,
);