CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-solana

Comprehensive Python SDK for interacting with the Solana blockchain network including RPC clients, SPL Token support, and WebSocket subscriptions.

Overview
Eval results
Files

token-operations.mddocs/

SPL Token Operations

Comprehensive SPL Token program interface for token creation, account management, transfers, and advanced token operations. The library supports both the standard SPL Token program and the newer Token-2022 program with enhanced features.

Capabilities

Token Client Initialization

Create token instances for interacting with existing tokens or initialize new tokens with mint authorities and program configurations.

class Token:
    def __init__(self, conn: Client, pubkey: Pubkey, program_id: Pubkey, payer: Keypair):
        """
        Initialize token client for existing token.
        
        Parameters:
        - conn: RPC client connection
        - pubkey: Token mint public key
        - program_id: Token program ID (TOKEN_PROGRAM_ID or TOKEN_2022_PROGRAM_ID)
        - payer: Keypair for transaction fees
        """

class AsyncToken:
    def __init__(self, conn: AsyncClient, pubkey: Pubkey, program_id: Pubkey, payer: Keypair):
        """
        Initialize async token client for existing token.
        
        Parameters:
        - conn: Async RPC client connection
        - pubkey: Token mint public key
        - program_id: Token program ID
        - payer: Keypair for transaction fees
        """

Token Creation

Create new token mints with configurable decimal precision, mint authority, and optional freeze authority.

@staticmethod
def create_mint(conn: Client, payer: Keypair, mint_authority: Pubkey, decimals: int, program_id: Pubkey, freeze_authority: Optional[Pubkey] = None, skip_confirmation: bool = False, recent_blockhash: Optional[Blockhash] = None) -> "Token":
    """
    Create a new token mint.
    
    Parameters:
    - conn: RPC client connection
    - payer: Keypair to pay for mint creation and transaction fees
    - mint_authority: Public key with authority to mint tokens
    - decimals: Number of decimal places for the token (0-9)
    - program_id: Token program ID
    - freeze_authority: Optional authority to freeze token accounts
    - skip_confirmation: Skip waiting for transaction confirmation
    - recent_blockhash: Recent blockhash for transaction
    
    Returns:
    Token instance for the newly created mint
    """

@staticmethod  
async def create_mint(conn: AsyncClient, payer: Keypair, mint_authority: Pubkey, decimals: int, program_id: Pubkey, freeze_authority: Optional[Pubkey] = None, skip_confirmation: bool = False, recent_blockhash: Optional[Blockhash] = None) -> "AsyncToken":
    """Async version of create_mint."""

Account Management

Create and manage token accounts including standard accounts, associated token accounts, wrapped SOL accounts, and multisig accounts.

def create_account(self, owner: Pubkey, skip_confirmation: bool = False, recent_blockhash: Optional[Blockhash] = None) -> Pubkey:
    """
    Create a new token account.
    
    Parameters:
    - owner: Public key that will own the token account
    - skip_confirmation: Skip waiting for transaction confirmation
    - recent_blockhash: Recent blockhash for transaction
    
    Returns:
    Public key of the created token account
    """

def create_associated_token_account(self, owner: Pubkey, skip_confirmation: bool = False, recent_blockhash: Optional[Blockhash] = None) -> Pubkey:
    """
    Create an associated token account (ATA).
    
    Parameters:
    - owner: Public key that will own the ATA
    - skip_confirmation: Skip waiting for transaction confirmation
    - recent_blockhash: Recent blockhash for transaction
    
    Returns:
    Public key of the created associated token account
    """

@staticmethod
def create_wrapped_native_account(conn: Client, program_id: Pubkey, owner: Pubkey, payer: Keypair, amount: int, skip_confirmation: bool = False, recent_blockhash: Optional[Blockhash] = None) -> Pubkey:
    """
    Create a wrapped SOL account.
    
    Parameters:
    - conn: RPC client connection
    - program_id: Token program ID
    - owner: Owner of the wrapped SOL account
    - payer: Keypair to pay for account creation and fund the account
    - amount: Amount of SOL to wrap (in lamports)
    - skip_confirmation: Skip waiting for transaction confirmation
    - recent_blockhash: Recent blockhash for transaction
    
    Returns:
    Public key of the created wrapped SOL account
    """

def create_multisig(self, m: int, multi_signers: List[Pubkey], opts: Optional[TxOpts] = None, recent_blockhash: Optional[Blockhash] = None) -> Pubkey:
    """
    Create a multisig account.
    
    Parameters:
    - m: Number of signatures required (1 <= m <= signers)
    - multi_signers: List of signer public keys (max 11)
    - opts: Transaction options
    - recent_blockhash: Recent blockhash for transaction
    
    Returns:
    Public key of the created multisig account
    """

Account Queries

Retrieve token account information, balances, mint details, and query accounts by ownership or delegation.

def get_balance(self, pubkey: Pubkey, commitment: Optional[Commitment] = None) -> GetTokenAccountBalanceResp:
    """
    Get token account balance.
    
    Parameters:
    - pubkey: Token account public key
    - commitment: Commitment level for the query
    
    Returns:
    GetTokenAccountBalanceResp with balance information
    """

def get_account_info(self, account: Pubkey, commitment: Optional[Commitment] = None) -> AccountInfo:
    """
    Get detailed token account information.
    
    Parameters:
    - account: Token account public key
    - commitment: Commitment level for the query
    
    Returns:
    AccountInfo with account details (mint, owner, amount, delegate, etc.)
    """

def get_mint_info(self) -> MintInfo:
    """
    Get mint information for this token.
    
    Returns:
    MintInfo with mint details (authority, supply, decimals, freeze_authority)
    """

def get_accounts_by_owner(self, owner: Pubkey, commitment: Optional[Commitment] = None, encoding: str = "base64") -> GetTokenAccountsByOwnerResp:
    """
    Get all token accounts owned by a specific address.
    
    Parameters:
    - owner: Owner public key
    - commitment: Commitment level for the query
    - encoding: Account data encoding
    
    Returns:
    GetTokenAccountsByOwnerResp with array of token accounts
    """

def get_accounts_by_delegate(self, owner: Pubkey, commitment: Optional[Commitment] = None, encoding: str = "base64") -> GetTokenAccountsByDelegateResp:
    """
    Get all token accounts delegated to a specific address.
    
    Parameters:
    - owner: Delegate public key
    - commitment: Commitment level for the query
    - encoding: Account data encoding
    
    Returns:
    GetTokenAccountsByDelegateResp with array of delegated accounts
    """

Token Operations

Transfer tokens, mint new tokens, burn tokens, and manage token approvals with comprehensive token transaction capabilities.

def transfer(self, source: Pubkey, dest: Pubkey, owner: Keypair, amount: int, multi_signers: Optional[List[Keypair]] = None, opts: Optional[TxOpts] = None, recent_blockhash: Optional[Blockhash] = None) -> SendTransactionResp:
    """
    Transfer tokens between accounts.
    
    Parameters:
    - source: Source token account public key
    - dest: Destination token account public key
    - owner: Owner keypair or multisig account
    - amount: Amount to transfer (in token's smallest unit)
    - multi_signers: Additional signers for multisig accounts
    - opts: Transaction options
    - recent_blockhash: Recent blockhash for transaction
    
    Returns:
    SendTransactionResp with transaction signature
    """

def mint_to(self, dest: Pubkey, mint_authority: Keypair, amount: int, multi_signers: Optional[List[Keypair]] = None, opts: Optional[TxOpts] = None, recent_blockhash: Optional[Blockhash] = None) -> SendTransactionResp:
    """
    Mint tokens to an account.
    
    Parameters:
    - dest: Destination token account public key
    - mint_authority: Mint authority keypair
    - amount: Amount to mint (in token's smallest unit)
    - multi_signers: Additional signers for multisig mint authority
    - opts: Transaction options
    - recent_blockhash: Recent blockhash for transaction
    
    Returns:
    SendTransactionResp with transaction signature
    """

def burn(self, account: Pubkey, owner: Keypair, amount: int, multi_signers: Optional[List[Keypair]] = None, opts: Optional[TxOpts] = None, recent_blockhash: Optional[Blockhash] = None) -> SendTransactionResp:
    """
    Burn tokens from an account.
    
    Parameters:
    - account: Token account to burn from
    - owner: Account owner keypair
    - amount: Amount to burn (in token's smallest unit)
    - multi_signers: Additional signers for multisig accounts
    - opts: Transaction options
    - recent_blockhash: Recent blockhash for transaction
    
    Returns:
    SendTransactionResp with transaction signature
    """

def approve(self, source: Pubkey, delegate: Pubkey, owner: Keypair, amount: int, multi_signers: Optional[List[Keypair]] = None, opts: Optional[TxOpts] = None, recent_blockhash: Optional[Blockhash] = None) -> SendTransactionResp:
    """
    Approve a delegate to transfer tokens.
    
    Parameters:
    - source: Source token account public key
    - delegate: Delegate public key
    - owner: Account owner keypair
    - amount: Amount to approve for delegation
    - multi_signers: Additional signers for multisig accounts
    - opts: Transaction options
    - recent_blockhash: Recent blockhash for transaction
    
    Returns:
    SendTransactionResp with transaction signature
    """

def revoke(self, account: Pubkey, owner: Keypair, multi_signers: Optional[List[Keypair]] = None, opts: Optional[TxOpts] = None, recent_blockhash: Optional[Blockhash] = None) -> SendTransactionResp:
    """
    Revoke a delegate's approval.
    
    Parameters:
    - account: Token account public key
    - owner: Account owner keypair
    - multi_signers: Additional signers for multisig accounts
    - opts: Transaction options
    - recent_blockhash: Recent blockhash for transaction
    
    Returns:
    SendTransactionResp with transaction signature
    """

Enhanced Token Operations

Token operations with additional validation including decimals checking for improved safety and error prevention.

def transfer_checked(self, source: Pubkey, dest: Pubkey, owner: Keypair, amount: int, decimals: int, multi_signers: Optional[List[Keypair]] = None, opts: Optional[TxOpts] = None, recent_blockhash: Optional[Blockhash] = None) -> SendTransactionResp:
    """
    Transfer tokens with decimals validation.
    
    Parameters:
    - source: Source token account public key
    - dest: Destination token account public key
    - owner: Owner keypair
    - amount: Amount to transfer
    - decimals: Expected decimals for validation
    - multi_signers: Additional signers for multisig accounts
    - opts: Transaction options
    - recent_blockhash: Recent blockhash for transaction
    
    Returns:
    SendTransactionResp with transaction signature
    """

def mint_to_checked(self, dest: Pubkey, mint_authority: Keypair, amount: int, decimals: int, multi_signers: Optional[List[Keypair]] = None, opts: Optional[TxOpts] = None, recent_blockhash: Optional[Blockhash] = None) -> SendTransactionResp:
    """
    Mint tokens with decimals validation.
    
    Parameters:
    - dest: Destination token account public key
    - mint_authority: Mint authority keypair
    - amount: Amount to mint
    - decimals: Expected decimals for validation
    - multi_signers: Additional signers for multisig accounts
    - opts: Transaction options
    - recent_blockhash: Recent blockhash for transaction
    
    Returns:
    SendTransactionResp with transaction signature
    """

def burn_checked(self, account: Pubkey, owner: Keypair, amount: int, decimals: int, multi_signers: Optional[List[Keypair]] = None, opts: Optional[TxOpts] = None, recent_blockhash: Optional[Blockhash] = None) -> SendTransactionResp:
    """
    Burn tokens with decimals validation.
    
    Parameters:
    - account: Token account to burn from
    - owner: Account owner keypair
    - amount: Amount to burn
    - decimals: Expected decimals for validation
    - multi_signers: Additional signers for multisig accounts
    - opts: Transaction options
    - recent_blockhash: Recent blockhash for transaction
    
    Returns:
    SendTransactionResp with transaction signature
    """

def approve_checked(self, source: Pubkey, delegate: Pubkey, owner: Keypair, amount: int, decimals: int, multi_signers: Optional[List[Keypair]] = None, opts: Optional[TxOpts] = None, recent_blockhash: Optional[Blockhash] = None) -> SendTransactionResp:
    """
    Approve delegate with decimals validation.
    
    Parameters:
    - source: Source token account public key
    - delegate: Delegate public key
    - owner: Account owner keypair
    - amount: Amount to approve
    - decimals: Expected decimals for validation
    - multi_signers: Additional signers for multisig accounts
    - opts: Transaction options
    - recent_blockhash: Recent blockhash for transaction
    
    Returns:
    SendTransactionResp with transaction signature
    """

Authority Management

Manage token account authorities, freeze/unfreeze accounts, close accounts, and transfer ownership with comprehensive authority control.

def set_authority(self, account: Pubkey, current_authority: Keypair, authority_type: AuthorityType, new_authority: Optional[Pubkey] = None, multi_signers: Optional[List[Keypair]] = None, opts: Optional[TxOpts] = None, recent_blockhash: Optional[Blockhash] = None) -> SendTransactionResp:
    """
    Change or remove authority for an account or mint.
    
    Parameters:
    - account: Account or mint public key
    - current_authority: Current authority keypair
    - authority_type: Type of authority to change (MINT_TOKENS, FREEZE_ACCOUNT, ACCOUNT_OWNER, CLOSE_ACCOUNT)
    - new_authority: New authority public key (None to remove authority)
    - multi_signers: Additional signers for multisig authorities
    - opts: Transaction options
    - recent_blockhash: Recent blockhash for transaction
    
    Returns:
    SendTransactionResp with transaction signature
    """

def freeze_account(self, account: Pubkey, authority: Keypair, multi_signers: Optional[List[Keypair]] = None, opts: Optional[TxOpts] = None, recent_blockhash: Optional[Blockhash] = None) -> SendTransactionResp:
    """
    Freeze a token account.
    
    Parameters:
    - account: Token account to freeze
    - authority: Freeze authority keypair
    - multi_signers: Additional signers for multisig authorities
    - opts: Transaction options
    - recent_blockhash: Recent blockhash for transaction
    
    Returns:
    SendTransactionResp with transaction signature
    """

def thaw_account(self, account: Pubkey, authority: Keypair, multi_signers: Optional[List[Keypair]] = None, opts: Optional[TxOpts] = None, recent_blockhash: Optional[Blockhash] = None) -> SendTransactionResp:
    """
    Unfreeze a token account.
    
    Parameters:
    - account: Token account to unfreeze
    - authority: Freeze authority keypair
    - multi_signers: Additional signers for multisig authorities
    - opts: Transaction options
    - recent_blockhash: Recent blockhash for transaction
    
    Returns:
    SendTransactionResp with transaction signature
    """

def close_account(self, account: Pubkey, dest: Pubkey, authority: Keypair, multi_signers: Optional[List[Keypair]] = None, opts: Optional[TxOpts] = None, recent_blockhash: Optional[Blockhash] = None) -> SendTransactionResp:
    """
    Close a token account and transfer remaining SOL.
    
    Parameters:
    - account: Token account to close
    - dest: Destination for remaining SOL
    - authority: Close authority keypair
    - multi_signers: Additional signers for multisig authorities
    - opts: Transaction options
    - recent_blockhash: Recent blockhash for transaction
    
    Returns:
    SendTransactionResp with transaction signature
    """

Utility Functions

Helper functions for calculating rent exemption amounts and other token-related utilities.

@staticmethod
def get_min_balance_rent_for_exempt_for_account(conn: Client) -> int:
    """
    Get minimum balance for rent exemption for token accounts.
    
    Parameters:
    - conn: RPC client connection
    
    Returns:
    Minimum balance in lamports for token account rent exemption
    """

@staticmethod
def get_min_balance_rent_for_exempt_for_mint(conn: Client) -> int:
    """
    Get minimum balance for rent exemption for mint accounts.
    
    Parameters:
    - conn: RPC client connection
    
    Returns:
    Minimum balance in lamports for mint account rent exemption
    """

@staticmethod
def get_min_balance_rent_for_exempt_for_multisig(conn: Client) -> int:
    """
    Get minimum balance for rent exemption for multisig accounts.
    
    Parameters:
    - conn: RPC client connection
    
    Returns:
    Minimum balance in lamports for multisig account rent exemption
    """

SPL Memo Operations

Simple memo functionality for attaching messages to transactions.

def create_memo(params: MemoParams) -> Instruction:
    """
    Create a memo instruction.
    
    Parameters:
    - params: MemoParams with program_id, signer, and message
    
    Returns:
    Instruction that can be added to transactions
    """

def decode_create_memo(instruction: Instruction) -> MemoParams:
    """
    Decode a memo instruction.
    
    Parameters:
    - instruction: Memo instruction to decode
    
    Returns:
    MemoParams with decoded instruction parameters
    """

Types

class AccountInfo(NamedTuple):
    mint: Pubkey
    owner: Pubkey
    amount: int
    delegate: Optional[Pubkey]
    delegated_amount: int
    is_initialized: bool
    is_frozen: bool
    is_native: Optional[int]
    rent_exempt_reserve: Optional[int]
    close_authority: Optional[Pubkey]

class MintInfo(NamedTuple):
    mint_authority: Optional[Pubkey]
    supply: int
    decimals: int
    is_initialized: bool
    freeze_authority: Optional[Pubkey]

class AuthorityType(IntEnum):
    MINT_TOKENS = 0
    FREEZE_ACCOUNT = 1
    ACCOUNT_OWNER = 2
    CLOSE_ACCOUNT = 3

class MemoParams(NamedTuple):
    program_id: Pubkey  # Memo program account
    signer: Pubkey      # Signing account
    message: bytes      # Memo message in bytes

class GetTokenAccountBalanceResp:
    value: TokenAccountBalance

class TokenAccountBalance:
    amount: str
    decimals: int
    ui_amount: Optional[float]
    ui_amount_string: str

# Constants
TOKEN_PROGRAM_ID: Pubkey
TOKEN_2022_PROGRAM_ID: Pubkey
ASSOCIATED_TOKEN_PROGRAM_ID: Pubkey
WRAPPED_SOL_MINT: Pubkey
NATIVE_DECIMALS: int = 9
MINT_LEN: int = 82
ACCOUNT_LEN: int = 165
MULTISIG_LEN: int = 355
MEMO_PROGRAM_ID: Pubkey

Install with Tessl CLI

npx tessl i tessl/pypi-solana@0.36.1

docs

index.md

rpc-client.md

token-operations.md

utilities.md

tile.json