The Anchor Frameworkis a popular development framework for building Solana programs that simplifies the development process with features like IDL integration and type safety. This guide will show you how to integrate Para’s wallet and signing capabilities with Anchor.

Prerequisites

To use Para, you need an API key. This key authenticates your requests to Para services and is essential for integration. Before integrating Para with your application, ensure you have:

  • Completed Para authentication setup in your application (see one of our Setup Guides)
  • A valid Para API key
  • An RPC endpoint for your desired network

Need an API key? Visit the Developer Portal to create API keys, manage billing, teams, and more.

Installation

Choose your preferred package manager to install the required dependencies:

npm install @getpara/solana-web3.js-v1-integration @solana/web3.js @project-serum/anchor

Setting Up Anchor with Para

To use Para with Anchor, you need to create a custom signer that integrates with Anchor’s wallet system. Below is an example of how to set up the Para signer with Anchor using the ParaSolanaWeb3Signer class. You can reference the Para Solana Web3.js Integration guide for more details on the signer setup.

import { ParaSolanaWeb3Signer } from "@getpara/solana-web3.js-v1-integration";
import { Connection, clusterApiUrl, Transaction, VersionedTransaction, SystemProgram } from "@solana/web3.js";
import * as anchor from "@project-serum/anchor";
import { para } from "./para"; // Your Para client initialization

const solanaConnection = new Connection(clusterApiUrl("testnet"));
const solanaSigner = new ParaSolanaWeb3Signer(para, solanaConnection);

const anchorWallet = {
  publicKey: solanaSigner.sender,
  signTransaction: async <T extends Transaction | VersionedTransaction>(tx: T): Promise<T> => {
    return await solanaSigner.signTransaction(tx);
  },
  signAllTransactions: async <T extends Transaction | VersionedTransaction>(txs: T[]): Promise<T[]> => {
    return await Promise.all(txs.map((tx) => solanaSigner.signTransaction(tx)));
  },
};

const provider = new anchor.AnchorProvider(
  solanaConnection,
  anchorWallet,
  { commitment: "confirmed" }
);

// Set the provider globally
anchor.setProvider(provider);

Creating a Read-Only Provider

For cases where you need to read from a program before the user has authenticated, you can create a read-only provider:

const readOnlyWallet = {
  publicKey: SystemProgram.programId, // Fallback to SystemProgram's public key
  signTransaction: async <T extends Transaction | VersionedTransaction>(tx: T): Promise<T> => {
    throw new Error("Read-only provider: Authenticate to sign transactions.");
  },
  signAllTransactions: async <T extends Transaction | VersionedTransaction>(txs: T[]): Promise<T[]> => {
    throw new Error("Read-only provider: Authenticate to sign transactions.");
  },
};

const readOnlyProvider = new anchor.AnchorProvider(
  solanaConnection,
  readOnlyWallet,
  { commitment: "confirmed" }
);

Working with Anchor Programs

Once you’ve set up the provider, you can initialize and interact with your Anchor program:


// Initialize the program
const program = new anchor.Program(idl as YourProgramImport, provider);

// Now you can call your program's methods
try {
  // Example: Create Token
  const tx = await program.methods
    .createToken(tokenName, tokenSymbol)
    .accounts({
      payer: solanSigner.sender,
      mintAccount: mintKeypair.publicKey,
      tokenProgram: TOKEN_2022_PROGRAM_ID,
      systemProgram: SystemProgram.programId,
      rent: anchor.web3.SYSVAR_RENT_PUBKEY,
    })
    .signers([mintKeypair])
    .rpc();
  console.log("Transaction signature:", tx);
} catch (error) {
  console.error("Error calling program method:", error);
}

We encourage you check out the Anchor documentation for more details on how to define your IDL, accounts, and methods.

Para only acts as the signer for the transaction and does not handle the program logic or state. Constructing transactions and interacting with the program is entirely up to you. Ensure you have the correct program IDL and account structures defined in your Anchor program.

Server-Side Integration

Para’s signers can also be used on the server-side with Anchor using pregen wallets or an active client-side session. To learn more about using Para on the server, check out these guides:

Examples

If you’d like to learn more about how to use Para with Anchor for different program interactions, check out this example in our Examples Hub: