Skip to main content

Overview

The Provider interface and AnchorProvider class manage the wallet and network context for Anchor programs. They handle transaction signing, sending, and confirmation.

Provider Interface

The base Provider interface defines the contract for network and wallet interactions.
interface Provider {
  readonly connection: Connection;
  readonly publicKey?: PublicKey;
  readonly wallet?: Wallet;

  send?(tx: Transaction | VersionedTransaction, signers?: Signer[], opts?: SendOptions): Promise<TransactionSignature>;
  sendAndConfirm?(tx: Transaction | VersionedTransaction, signers?: Signer[], opts?: ConfirmOptions): Promise<TransactionSignature>;
  sendAll?(txWithSigners: { tx: Transaction | VersionedTransaction; signers?: Signer[] }[], opts?: ConfirmOptions): Promise<TransactionSignature[]>;
  simulate?(tx: Transaction | VersionedTransaction, signers?: Signer[], commitment?: Commitment, includeAccounts?: boolean | PublicKey[]): Promise<SuccessfulTxSimulationResponse>;
}

Properties

connection
Connection
required
The Solana RPC connection to the cluster.
const slot = await provider.connection.getSlot();
publicKey
PublicKey
The public key of the wallet, if available.
console.log("Wallet:", provider.publicKey?.toString());
wallet
Wallet
The wallet instance used for signing transactions.
const signedTx = await provider.wallet.signTransaction(tx);

AnchorProvider

The standard implementation of the Provider interface.

Constructor

constructor
(connection, wallet, opts?) => AnchorProvider
Creates a new AnchorProvider instance.
import { AnchorProvider } from "@anchor-lang/anchor";
import { Connection } from "@solana/web3.js";

const connection = new Connection("https://api.devnet.solana.com");
const provider = new AnchorProvider(connection, wallet, {
  commitment: "confirmed",
});

Static Methods

AnchorProvider.defaultOptions
() => ConfirmOptions
Returns the default confirmation options.
const opts = AnchorProvider.defaultOptions();
// { preflightCommitment: "processed", commitment: "processed" }
AnchorProvider.local
(url?, opts?) => AnchorProvider
Creates a provider with a wallet from the local filesystem. Node.js only.
const provider = AnchorProvider.local("http://127.0.0.1:8899");
This method is only available in Node.js environments. It reads the wallet from ~/.config/solana/id.json.
AnchorProvider.env
() => AnchorProvider
Creates a provider from the ANCHOR_PROVIDER_URL environment variable. Node.js only.
// Requires ANCHOR_PROVIDER_URL environment variable
const provider = AnchorProvider.env();
This method is only available in Node.js environments and requires the ANCHOR_PROVIDER_URL environment variable to be set.

Instance Properties

connection
Connection
The Solana cluster connection.
const balance = await provider.connection.getBalance(publicKey);
wallet
Wallet
The wallet instance for signing transactions.
console.log("Wallet pubkey:", provider.wallet.publicKey.toString());
publicKey
PublicKey
The public key of the wallet.
const airdropSig = await provider.connection.requestAirdrop(
  provider.publicKey,
  1000000000
);
opts
ConfirmOptions
Default transaction confirmation options.
console.log("Commitment:", provider.opts.commitment);

Methods

sendAndConfirm
async (tx, signers?, opts?) => Promise<TransactionSignature>
Sends a transaction, waits for confirmation, and returns the signature.
const transaction = new Transaction().add(instruction);

const signature = await provider.sendAndConfirm(
  transaction,
  [signer],
  { commitment: "confirmed" }
);
sendAll
async (txWithSigners, opts?) => Promise<TransactionSignature[]>
Sends multiple transactions in parallel and waits for all confirmations.
const signatures = await provider.sendAll(
  [
    { tx: transaction1, signers: [signer1] },
    { tx: transaction2, signers: [signer2] },
  ],
  { commitment: "confirmed" }
);
simulate
async (tx, signers?, commitment?, includeAccounts?) => Promise<SuccessfulTxSimulationResponse>
Simulates a transaction without sending it to the network.
const result = await provider.simulate(
  transaction,
  [signer],
  "confirmed",
  [accountToInclude]
);

console.log("Logs:", result.logs);

Wallet Interface

The Wallet interface defines how wallets sign transactions.
interface Wallet {
  signTransaction<T extends Transaction | VersionedTransaction>(tx: T): Promise<T>;
  signAllTransactions<T extends Transaction | VersionedTransaction>(txs: T[]): Promise<T[]>;
  publicKey: PublicKey;
  payer?: Keypair; // Node only
}
signTransaction
async (tx) => Promise<Transaction | VersionedTransaction>
Signs a single transaction.
const signedTx = await wallet.signTransaction(transaction);
signAllTransactions
async (txs) => Promise<(Transaction | VersionedTransaction)[]>
Signs multiple transactions.
const signedTxs = await wallet.signAllTransactions([
  transaction1,
  transaction2,
]);
publicKey
PublicKey
The wallet’s public key.
console.log(wallet.publicKey.toString());

Helper Functions

setProvider
(provider) => void
Sets the global default provider.
import { setProvider, AnchorProvider } from "@anchor-lang/anchor";

const provider = AnchorProvider.local();
setProvider(provider);
getProvider
() => Provider
Gets the current global provider.
import { getProvider } from "@anchor-lang/anchor";

const provider = getProvider();

Usage Examples

Setting Up a Provider

import * as anchor from "@anchor-lang/anchor";
import { Connection, clusterApiUrl } from "@solana/web3.js";

// Option 1: Use environment provider (Node.js)
const provider = anchor.AnchorProvider.env();
anchor.setProvider(provider);

// Option 2: Use local provider (Node.js)
const localProvider = anchor.AnchorProvider.local();

// Option 3: Create custom provider
const connection = new Connection(clusterApiUrl("devnet"), "confirmed");
const customProvider = new anchor.AnchorProvider(
  connection,
  window.solana, // Browser wallet
  { commitment: "confirmed" }
);

Sending Transactions

const provider = anchor.AnchorProvider.env();

// Build transaction
const transaction = new anchor.web3.Transaction().add(
  anchor.web3.SystemProgram.transfer({
    fromPubkey: provider.wallet.publicKey,
    toPubkey: recipient,
    lamports: 1000000,
  })
);

// Send and confirm
const signature = await provider.sendAndConfirm(transaction);
console.log("Transaction signature:", signature);

Simulating Transactions

const provider = anchor.AnchorProvider.env();

// Build transaction
const transaction = new anchor.web3.Transaction().add(instruction);

// Simulate without sending
const simulation = await provider.simulate(transaction, [], "confirmed");

if (simulation.logs) {
  console.log("Program logs:", simulation.logs);
}

Browser Integration

import { AnchorProvider } from "@anchor-lang/anchor";
import { Connection, clusterApiUrl } from "@solana/web3.js";

// Connect to Phantom wallet in browser
if (window.solana) {
  await window.solana.connect();
  
  const connection = new Connection(clusterApiUrl("devnet"));
  const provider = new AnchorProvider(
    connection,
    window.solana,
    { commitment: "confirmed" }
  );
  
  // Use provider with programs
  const program = new anchor.Program(idl, provider);
}

Build docs developers (and LLMs) love