Skip to main content
added 176 characters in body
Source Link

To create metadata account on any SPL token you can create use: version 2 of @metaplex-foundation/mpl-token-metadata@2.

Version 3 exists, but is significantly more complex.

npm i "@metaplex-foundation/mpl-token-metadata@2"
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 looks 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);
};

To create metadata account on any SPL token you can create use:

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 looks 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);
};

To create metadata account on any SPL token you can create use version 2 of @metaplex-foundation/mpl-token-metadata@2.

Version 3 exists, but is significantly more complex.

npm i "@metaplex-foundation/mpl-token-metadata@2"
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 looks 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);
};
format
Source Link

To create metadata account on any SPL token you can create using (JS version) use:

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;
    }
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;
};

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];
    };
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 constructedlooks 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
           };
const metadataData = {
  name: "Test Token",
  symbol: "Test",
  uri: uri, // Arweave URI link which uses metaplex standard
  sellerFeeBasisPoints: 0,
  creators: null,
  collection: null,
  uses: null,
};
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);
    }
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);
};

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;
    }
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
           };
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);
    }

To create metadata account on any SPL token you can create use:

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;
};

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 looks 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,
};
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);
};
added 61 characters in body
Source Link
maddy
  • 358
  • 1
  • 8
const createMetadataAccount = async (metadataPDA: PublicKey, mint: PublicKey, payer: PublicKey, metadataData: any) => {
        const metadataAccounttx = new Transaction().add(
            createCreateMetadataAccountV2Instruction({
                metadata: metadataPDA,
                mint: mint.publicKey,
                mintAuthority: payer,
                payer: payer.publicKey,
                updateAuthority: payer.publicKey,
            },
                {
                    createMetadataAccountArgsV2:
                    {
                        data: metadataData,
                        isMutable: true
                    }
                }
            ))
        return metadataAccount;tx;
    }
const createMetadataAccount = async(metadataPDA,mint,payer,metadataData) => {
    const metadataAccount = new Transaction().add(createCreateMetadataAccountV2Instruction({
                metadata: metadataPDA,
                mint: mint.publicKey,
                payer: payer.publicKey,
                updateAuthority: payer.publicKey,
            },
                {
                    createMetadataAccountArgsV2:
                    {
                        data: metadataData,
                        isMutable: true
                    }
                }
            ))
  return metadataAccount;
}
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;
    }
Source Link
maddy
  • 358
  • 1
  • 8
Loading