solanasolana-web3jssolana-program-library

How to add token metadata to solana token 2022 token created using createInitializeTransferFeeConfigInstruction extension


I am trying to create a token using solana token program 2022 extension createInitializeTransferFeeConfigInstruction. How can I add token metadata (name, symbol ) to it?

Here is my current code.

// Define the extensions to be used by the mint
const extensions = [
    ExtensionType.TransferFeeConfig,
];

// Calculate the length of the mint
const mintLen = getMintLen(extensions);   

   const mintLamports = await connection.getMinimumBalanceForRentExemption(mintLen);
        const mintTransaction = new Transaction().add(  
            SystemProgram.createAccount({
                fromPubkey: payer.publicKey,
                newAccountPubkey: mint,
                space: mintLen,
                lamports: mintLamports,
                programId: TOKEN_2022_PROGRAM_ID,
            }),
            
            createInitializeTransferFeeConfigInstruction(
                mint,
                transferFeeConfigAuthority.publicKey,
                withdrawWithheldAuthority.publicKey,
                feeBasisPoints,
                maxFee,
                TOKEN_2022_PROGRAM_ID
            ),
            createInitializeMintInstruction(mint, decimals, mintAuthority.publicKey, null, TOKEN_2022_PROGRAM_ID)
        );
        const newTokenTx = await sendAndConfirmTransaction(connection, mintTransaction, [payer, mintKeypair], undefined);
        console.log("New Token Created:", generateExplorerTxUrl(newTokenTx));

I would like to add the metadata to the token.


Solution

  • You also need to include the metadata pointer and metadata extensions during init. This would look something like:

    // Define the extensions to be used by the mint
    const extensions = [
        ExtensionType.TransferFeeConfig,
        ExtensionType.MetadataPointer,
    ];
    
    // Calculate the length of the mint
    const mintLen = getMintLen(extensions);   
    
       const mintLamports = await connection.getMinimumBalanceForRentExemption(mintLen);
            const mintTransaction = new Transaction().add(  
                SystemProgram.createAccount({
                    fromPubkey: payer.publicKey,
                    newAccountPubkey: mint,
                    space: mintLen,
                    lamports: mintLamports,
                    programId: TOKEN_2022_PROGRAM_ID,
                }),
                
                createInitializeTransferFeeConfigInstruction(
                    mint,
                    transferFeeConfigAuthority.publicKey,
                    withdrawWithheldAuthority.publicKey,
                    feeBasisPoints,
                    maxFee,
                    TOKEN_2022_PROGRAM_ID
                ),
                createInitializeMetadataPointerInstruction(
                    mint,
                    payer.publicKey,
                    mint,
                    TOKEN_2022_PROGRAM_ID
                ),
                createInitializeMintInstruction(mint, decimals, mintAuthority.publicKey, null, TOKEN_2022_PROGRAM_ID),
                createInitializeInstruction({
                    programId: TOKEN_2022_PROGRAM_ID,
                    mint: mint.publicKey,
                    metadata: mint.publicKey,
                    name: metadata.name,
                    symbol: metadata.symbol,
                    uri: metadata.uri,
                    mintAuthority: payer.publicKey,
                    updateAuthority: payer.publicKey,
                }),
            );
            const newTokenTx = await sendAndConfirmTransaction(connection, mintTransaction, [payer, mintKeypair], undefined);
            console.log("New Token Created:", generateExplorerTxUrl(newTokenTx));
    

    Feel free to copy the example at https://github.com/solana-labs/solana-program-library/blob/master/token/js/examples/metadata.ts