I'm trying to add metadata for a fungible SPL token, which was done via token-list before. I have tried using strata protocol, but it does not show up on the list so cannot be done. Is there a simple js codeblock or CLI command to add the metadata easily?
2 Answers
To create metadata account on any SPL token you can create using (JS version) :
const createMetadataAccount = async (metadataPDA: PublicKey, mint: PublicKey, payer: PublicKey, metadataData: any) => {
const tx = new Transaction().add(
createCreateMetadataAccountV2Instruction({
metadata: metadataPDA,
mint: mint,
mintAuthority: payer,
payer: payer,
updateAuthority: payer,
},
{
createMetadataAccountArgsV2:
{
data: metadataData,
isMutable: true
}
}
))
return tx;
}
To get the metadata PDA
const TOKEN_METADATA_PROGRAM_ID = new PublicKey(
'metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s',
);
const getMetadata = async (
mint: PublicKey,
): Promise<PublicKey> => {
return (
await PublicKey.findProgramAddress(
[
Buffer.from('metadata'),
TOKEN_METADATA_PROGRAM_ID.toBuffer(),
mint.toBuffer(),
],
TOKEN_METADATA_PROGRAM_ID,
)
)[0];
};
The data part which is being passed in instruction can be constructed like :
const metadataData = {
name: "Test Token",
symbol: "Test",
uri: uri, // Arweave URI link which uses metaplex standard
sellerFeeBasisPoints: 0,
creators: null,
collection: null,
uses: null
};
Creating the transaction and passing the parameters :
const addMetadata = (mintAddress: PublicKey, connection: Connection) => {
const payer = Keypair.fromSecretKey(new Uint8Array(JSON.parse(fs.readFileSync(keypair, { encoding: "utf8" }))));
const metadataAccount = await getMetadata(mintAddress);
const tx= await createMetadataAccount(metadataAccount, mintAddress, payer, metadataData);
const transactionId = await sendAndConfirmTransaction(
connection,
tx);
}
You can use Metaboss to create an SPL-Token with Metadata or add Metadata to an existing token.
https://metaboss.rs/create.html
Metadata
Decorate an existing SPL token mint with metadata.
Usage
Specify the existing mint address and path to a metadata file that contains the name, symbol and uri fields in a JSON format.
metaboss create metadata -a <mint_address> -m <metadata_file>