Get Balance

The Get Balance tool, part of AgentiPy, allows users to fetch the SOL balance or a specific SPL token balance for the agent's wallet on the Solana blockchain.

Functionality

This tool is implemented in the BalanceFetcher class.

Key Methods:

  • get_balance(agent: SolanaAgentKit, token_address: Optional[Pubkey] = None) -> Optional[float]:

    • Takes a SolanaAgentKit instance and an optional SPL token mint token_address (as a solders.pubkey.Pubkey object).

    • If token_address is not provided, it fetches the native SOL balance of the agent's wallet and returns it in SOL units (float).

    • If token_address is provided, it determines the associated token account (ATA) for the agent's wallet and the given mint. It then fetches the balance of this ATA and returns it in its UI amount (float, adjusted for token decimals).

    • Returns None if a token account does not exist or has no balance information in the response.

    • Raises an exception if the balance check fails for other reasons.

Source Code

You can find the source code for this tool on GitHub: https://github.com/niceberginc/agentipy/blob/main/agentipy/tools/get_balance.py

Last updated