{"id":9459,"library":"anchorpy","title":"AnchorPy","description":"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.","status":"active","version":"0.21.0","language":"en","source_language":"en","source_url":"https://github.com/kevinheavey/anchorpy","tags":["solana","blockchain","web3","anchor","cryptocurrency"],"install":[{"cmd":"pip install anchorpy","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Core library for Solana blockchain interactions.","package":"solana","optional":false},{"reason":"Optional dependency for interacting with Pyth oracle programs.","package":"pythclient","optional":true},{"reason":"Used for WebSocket subscriptions to listen for real-time events.","package":"websocket-client","optional":true}],"imports":[{"symbol":"Provider","correct":"from anchorpy import Provider"},{"symbol":"Wallet","correct":"from anchorpy import Wallet"},{"note":"Program is now typically imported directly from the top-level `anchorpy` package.","wrong":"from anchorpy.client import Program","symbol":"Program","correct":"from anchorpy import Program"},{"symbol":"Idl","correct":"from anchorpy import Idl"},{"note":"Moved from `solana.rpc.core` to `solana.rpc.commitment` in `solana-py` v0.20.0, impacting `anchorpy` users.","wrong":"from solana.rpc.core import Commitment","symbol":"Commitment","correct":"from solana.rpc.commitment import Commitment"},{"symbol":"PublicKey","correct":"from solana.publickey import PublicKey"},{"symbol":"Keypair","correct":"from solana.keypair import Keypair"}],"quickstart":{"code":"import asyncio\nimport os\nfrom solana.keypair import Keypair\nfrom solana.publickey import PublicKey\nfrom solana.rpc.api import Client\nfrom solana.rpc.commitment import Commitment\nfrom anchorpy import Provider, Wallet, Program, Idl\n\nasync def main():\n    # 1. Setup Solana client and provider\n    rpc_url = os.environ.get(\"SOLANA_RPC_URL\", \"https://api.devnet.solana.com\")\n    connection = Client(rpc_url)\n\n    # For a real wallet, you would load a private key\n    # e.g., from an environment variable or file.\n    # For this example, we use a dummy keypair (not funded).\n    payer = Keypair.generate() # Generate a new ephemeral keypair\n    wallet = Wallet(payer)\n    provider = Provider(connection, wallet, opts=Commitment(\"confirmed\"))\n\n    # 2. Define a placeholder program ID and a minimal IDL\n    #    (Replace with your actual program ID and IDL)\n    program_id = PublicKey(\"11111111111111111111111111111111\") # Example: System Program\n    idl_json = {\n        \"version\": \"0.1.0\",\n        \"name\": \"my_anchor_program\",\n        \"instructions\": [] # Populate with your program's instructions\n    }\n    idl = Idl.from_json(idl_json)\n\n    # 3. Instantiate the Program client\n    program = Program(idl, program_id, provider)\n\n    print(f\"Successfully connected to Solana cluster: {rpc_url}\")\n    print(f\"Using wallet public key: {provider.wallet.public_key}\")\n    print(f\"Interacting with program ID: {program.program_id}\")\n\nif __name__ == \"__main__\":\n    asyncio.run(main())\n","lang":"python","description":"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`."},"warnings":[{"fix":"Remove `payer` and `http_client` from `Program` and `Provider` initialization. Pass `payer` directly to program RPC methods (e.g., `program.rpc['my_instruction'](accounts={'payer': my_keypair.public_key}, signers=[my_keypair])`).","message":"The `Program` and `Provider` constructors no longer accept `payer` or `http_client` arguments (since v0.20.0). `payer` is now passed directly to `rpc` methods.","severity":"breaking","affected_versions":">=0.20.0"},{"fix":"Update `Commitment` imports to `from solana.rpc.commitment import Commitment`.","message":"The `Commitment` enum import path changed in `solana-py` to `from solana.rpc.commitment import Commitment` from `solana.rpc.core.Commitment`.","severity":"breaking","affected_versions":">=0.20.0"},{"fix":"Replace `skip_preflight=True, preflight_commitment=Commitment('processed')` with `opts=Commitment('processed')`.","message":"The `Provider` constructor argument `skip_preflight` and `preflight_commitment` were replaced by a single `opts` argument (which takes a `Commitment` value).","severity":"breaking","affected_versions":">=0.19.0"},{"fix":"Ensure all async function calls are prefixed with `await` and the entry point is run with `asyncio.run(main())` or similar.","message":"AnchorPy operations are asynchronous. Forgetting to `await` calls or not running them within an `asyncio` event loop will lead to `RuntimeWarning: coroutine '...' was never awaited` or program freezes.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure `signers=[my_keypair]` is passed, where `my_keypair` is an actual `Keypair` instance, for any accounts that need to sign the transaction (e.g., the fee payer, or mutable accounts).","message":"When signing transactions, the `signers` argument for RPC calls expects a list of `solana.keypair.Keypair` objects, not just public keys or `Wallet` objects.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"You must provide an argument to `PublicKey` constructor, like `PublicKey('YourProgramIdHere')` or `PublicKey(bytes_array)`.","cause":"Attempting to call `PublicKey` as a function without providing a byte array or string argument (e.g., `PublicKey()`)","error":"TypeError: object PublicKey is not callable"},{"fix":"Examine the full error message from the cluster for specifics. Debug your program logic, ensure all required accounts are provided and correctly ordered, and verify the payer wallet has enough SOL.","cause":"This is a generic error from the Solana cluster indicating an issue within the program logic, missing accounts, insufficient funds for an account, or an incorrect instruction argument.","error":"anchorpy.error.ProgramError: Failed to send transaction: Transaction simulation failed: Error processing instruction..."},{"fix":"Add the `await` keyword before the coroutine call (e.g., `await program.rpc['instruction']()`). Ensure your code is running within an `asyncio` event loop.","cause":"An asynchronous function (coroutine) was called but its execution was not awaited, meaning Python did not wait for its completion.","error":"RuntimeWarning: coroutine '...' was never awaited"},{"fix":"Ensure `from solana.rpc.commitment import Commitment` is at the top of your file.","cause":"The `Commitment` enum was used without being imported, or it was imported from an old, incorrect path.","error":"NameError: name 'Commitment' is not defined"}]}