In this section, you will explore another aspect of Solana DApp development, focusing on integrating the Solana contract with the client side.
Setting Up Wallet, Provider, and Program API for Transactions

First, when you run anchor build for your contract, Anchor creates a "target" folder for your Solana program. In this folder, you'll find the deploy script, the IDL (Interface Description Language) file, and a directory for types.

IDL is like a blueprint for our program. It describes how our program should work.

We need to connect the blockchain part of our DApp (which runs in its special environment) to the front end (which uses different tools and languages). The IDL file acts as a bridge, using RPC protocols to make this connection.

RPC (Remote Procedure Call) is a set of methods we use to send requests to the Solana network.

The IDL file includes all the instructions and the program ID. The "types" directory gives us special Anchor types for sending these RPC calls.

To connect everything, we need:

  1. A cluster, which is like a specific area of the blockchain network where we send our transactions.
  2. A wallet for interacting with the DApp, which signs transactions and provides a public key.

By combining these, we set up an Anchor provider, which simplifies integrating the contract.

In our "context" directory, we define certain contexts and export them for use in other parts of our DApp. We need the network (our cluster) and the wallet (using Solana Wallet Adapter) for this. These are already set up, with configurations in their respective files.

In Next.js _app

We use a main context provider in our Next.js _app file. Once we have the wallet and connection ready, we move to the next step.

In the "create mint" component, we start by importing our contract's IDL.

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

We then establish a connection, configure the wallet, and import necessary methods from various packages.

import { Program, Idl, AnchorProvider, BN, utils, web3 } from "@coral-xyz/anchor";
import { Metaplex } from "@metaplex-foundation/js";
import { useConnection, useWallet } from "@solana/wallet-adapter-react";
import { getMint } from "@solana/spl-token";

Set up the program id (the ID of our deployed Solana program).

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

Use these hooks to access the connection and wallet states.

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

Create a provider with new AnchorProvider(connection, wallet as any, opts); using the wallet and connection. Then use the Anchor Program API to create an instance of our Solana program API.

const getProgram = () => {
    const provider = new AnchorProvider(connection, wallet as any, opts);
    const program = new Program(idl as Idl, programId, provider);
    return program;

Now, you can make an RPC call with the program:

const tx = await program.methods
    .accounts({ dataAccount: dataAccount.publicKey })

This is similar to a function call in Solidity:

constructor() {}

Remember, these operations are asynchronous because we're using Solana's Web3 JS.

Breaking down the transaction:

  1. Calls our Solang program's constructor.
  2. We provide data accounts for our contract and sign them.
  3. Use .rpc() to make an RPC call to the cluster.

That's how you set up the client side to interact with your Solana contract!