> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/solana-foundation/anchor/llms.txt
> Use this file to discover all available pages before exploring further.

# Provider & AnchorProvider

> Network and wallet context for sending transactions

## 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.

```typescript theme={null}
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

<ResponseField name="connection" type="Connection" required>
  The Solana RPC connection to the cluster.

  ```typescript theme={null}
  const slot = await provider.connection.getSlot();
  ```
</ResponseField>

<ResponseField name="publicKey" type="PublicKey">
  The public key of the wallet, if available.

  ```typescript theme={null}
  console.log("Wallet:", provider.publicKey?.toString());
  ```
</ResponseField>

<ResponseField name="wallet" type="Wallet">
  The wallet instance used for signing transactions.

  ```typescript theme={null}
  const signedTx = await provider.wallet.signTransaction(tx);
  ```
</ResponseField>

## AnchorProvider

The standard implementation of the `Provider` interface.

### Constructor

<ResponseField name="constructor" type="(connection, wallet, opts?) => AnchorProvider">
  Creates a new AnchorProvider instance.

  ```typescript theme={null}
  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",
  });
  ```

  <Expandable title="parameters">
    <ResponseField name="connection" type="Connection" required>
      The cluster connection where programs are deployed
    </ResponseField>

    <ResponseField name="wallet" type="Wallet" required>
      The wallet used to pay for and sign transactions
    </ResponseField>

    <ResponseField name="opts" type="ConfirmOptions">
      Default transaction confirmation options. Defaults to `{ preflightCommitment: "processed", commitment: "processed" }`
    </ResponseField>
  </Expandable>
</ResponseField>

### Static Methods

<ResponseField name="AnchorProvider.defaultOptions" type="() => ConfirmOptions">
  Returns the default confirmation options.

  ```typescript theme={null}
  const opts = AnchorProvider.defaultOptions();
  // { preflightCommitment: "processed", commitment: "processed" }
  ```
</ResponseField>

<ResponseField name="AnchorProvider.local" type="(url?, opts?) => AnchorProvider">
  Creates a provider with a wallet from the local filesystem. Node.js only.

  ```typescript theme={null}
  const provider = AnchorProvider.local("http://127.0.0.1:8899");
  ```

  <Expandable title="parameters">
    <ResponseField name="url" type="string">
      The cluster URL. Defaults to `http://127.0.0.1:8899`
    </ResponseField>

    <ResponseField name="opts" type="ConfirmOptions">
      Transaction confirmation options
    </ResponseField>
  </Expandable>

  <Warning>
    This method is only available in Node.js environments. It reads the wallet from `~/.config/solana/id.json`.
  </Warning>
</ResponseField>

<ResponseField name="AnchorProvider.env" type="() => AnchorProvider">
  Creates a provider from the `ANCHOR_PROVIDER_URL` environment variable. Node.js only.

  ```typescript theme={null}
  // Requires ANCHOR_PROVIDER_URL environment variable
  const provider = AnchorProvider.env();
  ```

  <Warning>
    This method is only available in Node.js environments and requires the `ANCHOR_PROVIDER_URL` environment variable to be set.
  </Warning>
</ResponseField>

### Instance Properties

<ResponseField name="connection" type="Connection">
  The Solana cluster connection.

  ```typescript theme={null}
  const balance = await provider.connection.getBalance(publicKey);
  ```
</ResponseField>

<ResponseField name="wallet" type="Wallet">
  The wallet instance for signing transactions.

  ```typescript theme={null}
  console.log("Wallet pubkey:", provider.wallet.publicKey.toString());
  ```
</ResponseField>

<ResponseField name="publicKey" type="PublicKey">
  The public key of the wallet.

  ```typescript theme={null}
  const airdropSig = await provider.connection.requestAirdrop(
    provider.publicKey,
    1000000000
  );
  ```
</ResponseField>

<ResponseField name="opts" type="ConfirmOptions">
  Default transaction confirmation options.

  ```typescript theme={null}
  console.log("Commitment:", provider.opts.commitment);
  ```
</ResponseField>

### Methods

<ResponseField name="sendAndConfirm" type="async (tx, signers?, opts?) => Promise<TransactionSignature>">
  Sends a transaction, waits for confirmation, and returns the signature.

  ```typescript theme={null}
  const transaction = new Transaction().add(instruction);

  const signature = await provider.sendAndConfirm(
    transaction,
    [signer],
    { commitment: "confirmed" }
  );
  ```

  <Expandable title="parameters">
    <ResponseField name="tx" type="Transaction | VersionedTransaction" required>
      The transaction to send
    </ResponseField>

    <ResponseField name="signers" type="Signer[]">
      Additional signers for the transaction
    </ResponseField>

    <ResponseField name="opts" type="ConfirmOptions">
      Confirmation options for this transaction
    </ResponseField>
  </Expandable>
</ResponseField>

<ResponseField name="sendAll" type="async (txWithSigners, opts?) => Promise<TransactionSignature[]>">
  Sends multiple transactions in parallel and waits for all confirmations.

  ```typescript theme={null}
  const signatures = await provider.sendAll(
    [
      { tx: transaction1, signers: [signer1] },
      { tx: transaction2, signers: [signer2] },
    ],
    { commitment: "confirmed" }
  );
  ```

  <Expandable title="parameters">
    <ResponseField name="txWithSigners" type="{ tx: Transaction | VersionedTransaction; signers?: Signer[] }[]" required>
      Array of transactions with their signers
    </ResponseField>

    <ResponseField name="opts" type="ConfirmOptions">
      Confirmation options
    </ResponseField>
  </Expandable>
</ResponseField>

<ResponseField name="simulate" type="async (tx, signers?, commitment?, includeAccounts?) => Promise<SuccessfulTxSimulationResponse>">
  Simulates a transaction without sending it to the network.

  ```typescript theme={null}
  const result = await provider.simulate(
    transaction,
    [signer],
    "confirmed",
    [accountToInclude]
  );

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

  <Expandable title="parameters">
    <ResponseField name="tx" type="Transaction | VersionedTransaction" required>
      The transaction to simulate
    </ResponseField>

    <ResponseField name="signers" type="Signer[]">
      Transaction signers. If not provided, simulation uses `sigVerify: false`
    </ResponseField>

    <ResponseField name="commitment" type="Commitment">
      Commitment level for simulation
    </ResponseField>

    <ResponseField name="includeAccounts" type="boolean | PublicKey[]">
      Accounts to include in simulation response
    </ResponseField>
  </Expandable>
</ResponseField>

## Wallet Interface

The `Wallet` interface defines how wallets sign transactions.

```typescript theme={null}
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
}
```

<ResponseField name="signTransaction" type="async (tx) => Promise<Transaction | VersionedTransaction>">
  Signs a single transaction.

  ```typescript theme={null}
  const signedTx = await wallet.signTransaction(transaction);
  ```
</ResponseField>

<ResponseField name="signAllTransactions" type="async (txs) => Promise<(Transaction | VersionedTransaction)[]>">
  Signs multiple transactions.

  ```typescript theme={null}
  const signedTxs = await wallet.signAllTransactions([
    transaction1,
    transaction2,
  ]);
  ```
</ResponseField>

<ResponseField name="publicKey" type="PublicKey">
  The wallet's public key.

  ```typescript theme={null}
  console.log(wallet.publicKey.toString());
  ```
</ResponseField>

## Helper Functions

<ResponseField name="setProvider" type="(provider) => void">
  Sets the global default provider.

  ```typescript theme={null}
  import { setProvider, AnchorProvider } from "@anchor-lang/anchor";

  const provider = AnchorProvider.local();
  setProvider(provider);
  ```
</ResponseField>

<ResponseField name="getProvider" type="() => Provider">
  Gets the current global provider.

  ```typescript theme={null}
  import { getProvider } from "@anchor-lang/anchor";

  const provider = getProvider();
  ```
</ResponseField>

## Usage Examples

### Setting Up a Provider

```typescript theme={null}
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

```typescript theme={null}
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

```typescript theme={null}
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

```typescript theme={null}
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);
}
```
