a
D-Application-setup

The DApp's mission is to allow users to mint NFTs, using a PDA as the mint authority and transfer NFTs to other users.

Go to the idl.json file in the DApp's root directory. It will look like this:

{
    "version": "0.0.1",
    "name": "pda_mint_authority",
    "instructions": [
          {...},{...},{...},
           {
            "name": "transferNft",
            "accounts": [
                {
                    "name": "from",
                    "isMut": true,
                    "isSigner": false,
                    "isOptional": false
                },
                {
                    "name": "to",
                    "isMut": true,
                    "isSigner": false,
                    "isOptional": false
                },
                {
                    "name": "owner",
                    "isMut": false,
                    "isSigner": true,
                    "isOptional": false
                },
                {
                    "name": "tokenProgram",
                    "isMut": false,
                    "isSigner": false,
                    "isOptional": false
                },
                {
                    "name": "systemProgram",
                    "isMut": false,
                    "isSigner": false,
                    "isOptional": false
                }
            ],
            "args": []
          }
        ],
           
        "metadata": {
          "address": "8QjnMJzRhYLe7JFgZveW7HGTtRy97FMbdZSNwsNwnVfC"
      }
  }

This file contains the blueprint of our deployed Solana program. It includes the program's version, name, instructions, and deployed address. The instructions array lists several instructions, each detailing the instruction name, the accounts used, and the arguments for the RPC call.

Setting up the program to call each instruction from our IDL file:

Dive into the root of the DApp, where we set up and execute transactions:

  1. dapp’s root→ src→ components→ mintNFtbutton
  2. dapp’s root→ src→ components→ nft→ nftcard

Import the IDL file into the file where we need to call our instructions

import idl *from* "../../idl.json";

You can fetch the program ID from the IDL like this. Here, we are using the PublicKey instance from the Solana Web3.js.

const programId = new PublicKey(idl.metadata.address);

You can set the transaction commitment level like this; here, we use preflightCommitment as processed.

You can learn more about transaction commitment levels here

const opts: { preflightCommitment: Commitment } = {
 preflightCommitment: "processed",
};

We can use hooks to get the connection (connecting to the blockchain cluster) object, wallet instance (from the wallet adapter for signing all transactions related to this DApp), and wallet public key for creating and signing accounts.

const { connection } = useConnection();
const wallet = useWallet();
const { publicKey, sendTransaction } = useWallet();

Next, we set up the Program API by creating a new Anchor provider (using connection + wallet). Then, we generate a Program API instance to call the RPC methods of our deployed program through the IDL interface.

 const getProgram = () => {
        /* create the provider and return it to the caller */
        const provider = new AnchorProvider(connection, wallet as any, opts);
        /* create the program interface combining the idl, program ID, and provider */
        const program = new Program(idl as Idl, programId, provider);
        return program;
    };

 const program = getProgram();

This is what an RPC transaction looks like from the client side.

const createTx = await program.methods
  .createTokenMint(
    dataAccountPDA,
    0, // 0 decimals for NFT
    MetaData.name, // NFT name
    MetaData.symbol, // NFT symbol
    MetaData.uri // NFT URI
  )
  .accounts({
    payer: wallet.publicKey,
    mint: mintKeypair.publicKey,
    metadata: metadataAddress,
    mintAuthority: dataAccountPDA,
    rentAddress: SYSVAR_RENT_PUBKEY,
    metaplexId: new PublicKey("metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s"),
  })
  .signers([mintKeypair])
  .rpc({ skipPreflight: true });

Calling the createTokenMint method means providing the necessary arguments, instruction-related accounts and signers for this transaction.