Solana: Initializing Token2022 Metadata with PDA mint authority

Here is an article based on the specified code detail:

Initialize the metadata of token with the PDA mint authority: Step by step

When developing the decentralized application (DAPP), which uses Solana, it is essential to treat token metadata. One of the critical aspects of token management is the initialization of metadata with the Disigt Activity (PDA) mint authority. In this article, we examine how to do this with a unique event and make a smart contract.

The problem

It is used for Solana’s traditional events, such as “Init”, for various purposes, such as initialization, creating new accounts, or transferring tokens. However, when the metadata of token is required to initialize the PDA mint authority, these traditional events cannot be used directly.

The solution: Initialize the token metadata with the PDA mint authority

To solve the problem, we can create a unique event called “Init”, which is used to initialize the metadata of token with the PDA mint authority. Here’s an example of how the code looks like:

`rust

#[Derive (Accounts)]

#[Event_cpi]

PUB STRUCT INIT {

#[Account (MUT)]

PUB ID: PUBKEY, // The unique identifier of the new mint

PUB Owner: Account <'Info>,

PUB metadata: Account <'Info>, // New token metadata is stored here

}

In this example, we defined an “init” event with three parameters:

1.

  • “Owner”: Account responsible for creating a new mint (the “Account <'Info>” account)

  • Metadata ': a new token metadata account that stores initialized data

Calculation of smart contract

Solana: Initializing Token2022 Metadata with PDA mint authority

To calculate the individual event, we create a new intelligent contract that executesInit. Here's an example:

rust

Use Solana_program :: {{

Account_info :: {Next_account_info, AccountInfo},

Entry point,

Program_Error :: Program Error,

Program_result :: ProgramPresult,

};

Use Serde :: {Serialize, Deserialize};

#[Derive (series, fashionable, debugging)]

PUB Struct Initparams {

ID: Pubkey,

}

#[Derive (Accounts)]

#[Account (

signed_by = owner,

PUB Key_info = Keyinfo <'_>,

PUB System_program_id = System Programid,

PUB metadata_account_id = metadataaccountid,

PUB accounts = [

Accountid :: from_unique_key (metadataaccountid),

],

)]

PUB STRUCT INIT {

PUB ID: PUBKEY,

}

In this example, we defined a “Initparams” structure to keep the necessary input parameters and a new init structure that executes the” Accounts “function. The “Init” Struct is signed by the account responsible for creating a new cashier (ie the owner).

Event definition

We use the following code to determine the event:

`rust

PUB Event Init (

ID: Pubkey,

Owner: Account <'_>,

Metadata: metadataaccountid,

) {}

In this example, we defined an “init” event with three parameters:

1.

  • “Owner”: Account responsible for creating a new cashier

  • Metadata ': A new token metadata account ID

Implementation of the program

To implement an individual program, we create a new feature invited when you receive an event:

rust

PUB FN INIT (

ID: Pubkey,

Owner: Account <'_>,

Metadata: metadataaccountid,

) -> ProgramPresult {

// initialize the token metadata with the PDA mint authority

Let Mut Metadata_account = Next_account_info! (metadata)?;

metadata_account.Mint_id = id;

OK (())

}

In this example, we defined a “init” feature that takes three parameters:

1.

  • “Owner”: Account responsible for creating a new cashier

3.

Mantle

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *