AnchorPy

0.21.0 · active · verified Fri Apr 17

AnchorPy is the official Python client for interacting with Solana programs built using the Anchor framework. It provides a robust and type-safe way to connect to Solana clusters, manage wallets, deserialize IDLs, and call program instructions. Currently at version 0.21.0, it sees regular updates with minor versions released every few weeks or months to keep pace with the evolving Solana ecosystem.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to establish a connection to a Solana cluster, initialize a wallet (using an ephemeral keypair for demonstration), load a placeholder IDL, and create an AnchorPy Program client. Remember to replace `SOLANA_RPC_URL` and `program_id` with your actual values, and provide a valid IDL for your program. Interactions with Solana programs are asynchronous and require `asyncio`.

import asyncio
import os
from solana.keypair import Keypair
from solana.publickey import PublicKey
from solana.rpc.api import Client
from solana.rpc.commitment import Commitment
from anchorpy import Provider, Wallet, Program, Idl

async def main():
    # 1. Setup Solana client and provider
    rpc_url = os.environ.get("SOLANA_RPC_URL", "https://api.devnet.solana.com")
    connection = Client(rpc_url)

    # For a real wallet, you would load a private key
    # e.g., from an environment variable or file.
    # For this example, we use a dummy keypair (not funded).
    payer = Keypair.generate() # Generate a new ephemeral keypair
    wallet = Wallet(payer)
    provider = Provider(connection, wallet, opts=Commitment("confirmed"))

    # 2. Define a placeholder program ID and a minimal IDL
    #    (Replace with your actual program ID and IDL)
    program_id = PublicKey("11111111111111111111111111111111") # Example: System Program
    idl_json = {
        "version": "0.1.0",
        "name": "my_anchor_program",
        "instructions": [] # Populate with your program's instructions
    }
    idl = Idl.from_json(idl_json)

    # 3. Instantiate the Program client
    program = Program(idl, program_id, provider)

    print(f"Successfully connected to Solana cluster: {rpc_url}")
    print(f"Using wallet public key: {provider.wallet.public_key}")
    print(f"Interacting with program ID: {program.program_id}")

if __name__ == "__main__":
    asyncio.run(main())

view raw JSON →