0
import { getKeypairFromFile } from "@solana-developers/helpers";
import { ExtensionType, LENGTH_SIZE, TOKEN_2022_PROGRAM_ID, TYPE_SIZE, createInitializeMetadataPointerInstruction, createInitializeMintInstruction, getMintLen, mintTo } from "@solana/spl-token";
import { TokenMetadata, createInitializeInstruction, createUpdateFieldInstruction, pack } from "@solana/spl-token-metadata";
import { Connection, Keypair, SystemProgram, Transaction, clusterApiUrl, sendAndConfirmTransaction } from "@solana/web3.js";

const connection = new Connection(clusterApiUrl("devnet"))

const payer = await getKeypairFromFile("~/.config/solana/id.json")
console.log("payer", payer.publicKey.toBase58);

const mint = Keypair.generate();
console.log("mint", mint.publicKey.toBase58);

const metadata : TokenMetadata = {
    mint: mint.publicKey,
    name: "Test Token",
    symbol: "TTK",
    uri: "",
    additionalMetadata: [
        ["key", "value"]
    ]
}

const mintSpace = getMintLen([
    ExtensionType.MetadataPointer
])

const metadataSpace = TYPE_SIZE + LENGTH_SIZE + pack(metadata).length

const lamports = await connection.getMinimumBalanceForRentExemption(
    mintSpace + metadataSpace
)

const createAccountIx = SystemProgram.createAccount({
    fromPubkey: payer.publicKey,
    newAccountPubkey: mint.publicKey,
    space: mintSpace,
    lamports,
    programId: TOKEN_2022_PROGRAM_ID
})

const initializeMetadataPointerIx = createInitializeMetadataPointerInstruction(
    mint.publicKey,
    payer.publicKey,
    mint.publicKey,
    TOKEN_2022_PROGRAM_ID
)

const initializeMintIx = createInitializeMintInstruction(
    mint.publicKey,
    2,
    payer.publicKey,
    null,
    TOKEN_2022_PROGRAM_ID
)

const initializeMetadataIx = createInitializeInstruction({
    mint: mint.publicKey,
    metadata: mint.publicKey,
    mintAuthority: payer.publicKey,
    name: metadata.name,
    symbol: metadata.symbol,
    uri: metadata.uri,
    programId: TOKEN_2022_PROGRAM_ID,
    updateAuthority: payer.publicKey
})

const updateMetadataField = createUpdateFieldInstruction({
    metadata: mint.publicKey,
    programId: TOKEN_2022_PROGRAM_ID,
    updateAuthority: payer.publicKey,
    field: metadata.additionalMetadata[0][0],
    value: metadata.additionalMetadata[0][1]
})

const transaction = new Transaction().add(
    createAccountIx,
    initializeMetadataPointerIx,
    initializeMintIx,
    initializeMetadataIx,
    updateMetadataField
)

const sig = await sendAndConfirmTransaction(
    connection,
    transaction,
    [payer, mint]
)

console.log('sig:', sig);


// here the error
const token = await mintTo(
    connection,
    payer,
    mint.publicKey,
    payer.publicKey,
    payer.publicKey,
    10000
)
//

console.log("token:", token)

so there is my code (I followed this tutorial https://youtu.be/l7EyQUlNAdw?si=ZeSSOuC2Zx30qkO7) but when I try to mint some of the spl-token I just created this doesn't work, can someone help me? also is this normal that my token name is, can someone explain me how to eventually change it enter image description here https://explorer.solana.com/address/EGVXSafYzNvr6feauLZeBgEzTmXi6Xe9g54KKLXdVaVZ?cluster=devnet

thank in advance

1
  • I'm assuming that the "doesn't work" part is referring to the "Unknown Token" name at the top, correct? If that's the case, you've actually done everything correctly, the Solana Explorer just hasn't been updated to consume the token-2022 metadata yet.
    – Jon C
    Mar 22 at 12:35

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.