Solders
Solders is a high-performance Python toolkit for Solana, providing robust solutions for core SDK functionalities like keypairs, pubkeys, transaction signing and serialization, and RPC request/response parsing. It serves as a Python binding to the Solana Rust SDK, aiming to avoid reimplementing Solana logic in pure Python. While `solana-py` handles network interaction and SPL token clients, `solders` focuses on low-level primitives and offers a `litesvm` module for faster integration testing. The library is actively maintained with frequent updates, with the current version being 0.27.1.
Warnings
- breaking In version `0.25.0`, the `solders.bankrun` module was removed in favor of `solders.litesvm`. Users migrating from older versions need to update their testing infrastructure to use `litesvm`.
- breaking As of version `0.25.0`, methods named `to_bytes_array` were renamed to `to_bytes` to accurately reflect the changed return types.
- breaking In version `0.18.0`, transaction processing methods were refactored. `process_transaction_with_metadata` was renamed to `process_transaction`, and older methods like `process_transaction_with_preflight` were removed due to being considered 'footguns'.
- breaking When `solana-py` (which uses `solders` internally) updated its RPC parsing to use `solders`, many RPC methods now return strongly typed objects from `solders.rpc.responses` instead of raw dictionaries. This change affects how RPC results are accessed and processed.
- breaking Related to `solana-py`'s internal use of `solders`, the `Transaction.__init__` method no longer accepts a `signatures` argument. `Transaction.populate` should be used instead to construct transactions with specific signatures. Additionally, `Transaction.add_signer` was removed.
- gotcha Users have reported issues with version mismatches between `solana-py` and `solders`, particularly concerning versioned transactions and unexpected `proxy` arguments in `Client` initialization. It is crucial to ensure compatible versions of both libraries are installed.
- deprecated Pickle support was removed in version `0.25.0`. Applications relying on Python's `pickle` module for serializing `solders` objects will need to transition to alternative serialization methods (e.g., JSON support was added to most classes).
Install
-
pip install solders
Imports
- Message
from solders.message import Message
- Keypair
from solders.keypair import Keypair
- Instruction
from solders.instruction import Instruction
- Hash
from solders.hash import Hash
- Transaction
from solders.transaction import Transaction
- Pubkey
from solders.pubkey import Pubkey
- Client
from solana.rpc.api import Client
Quickstart
from solders.message import Message
from solders.keypair import Keypair
from solders.instruction import Instruction
from solders.hash import Hash
from solders.transaction import Transaction
from solders.pubkey import Pubkey
# Generate a new keypair for the payer (replace with a real keypair in production)
payer = Keypair()
# Define a program ID and some arbitrary instruction data
program_id = Pubkey.new_unique() # Using new_unique for a placeholder program ID
arbitrary_instruction_data = bytes([1, 2, 3])
accounts = [] # For a simple instruction, no accounts might be needed
# Create an instruction
instruction = Instruction(program_id, arbitrary_instruction_data, accounts)
# Create a message from the instruction and payer's public key
message = Message([instruction], payer.pubkey())
# Replace with a real blockhash from an RPC call in a real application
# For demonstration, we use a default hash.
blockhash = Hash.default()
# Create a transaction
tx = Transaction([payer], message, blockhash)
# In a real application, you would sign and send the transaction:
# from solana.rpc.api import Client
# rpc_client = Client("https://api.devnet.solana.com") # Example RPC URL
# # For this example, we won't actually send the transaction.
# # signature = rpc_client.send_transaction(tx).value
# # print(f"Transaction sent: {signature}")
print("Transaction created successfully (not sent in this example).")
print(f"Payer Public Key: {payer.pubkey()}")
print(f"Transaction Message: {message}")
print(f"Transaction: {tx}")