Transfer SOL & SPL tokens

This page provides a comprehensive guide to transferring native SOL and SPL tokens usingKaze AI’s toolkit. Below is an explanation of the key components, functionalities, and implementation steps.

Overview

The SolanaTransferHelper and TokenTransferManager classes enable developers to execute, manage, and verify token transfers seamlessly. These tools support:

  • Transferring native SOL to any recipient.

  • Transferring SPL tokens, including validation and associated token account management.

  • Tracking transfer history and verifying transaction success.


Code Explanation

1. Native SOL Transfer

The transfer_native_sol method performs native SOL transfers by:

  • Creating a transaction that specifies the sender, recipient, and amount.

  • Sending the transaction using the AsyncClient and awaiting confirmation.

Copy

async def transfer_native_sol(agent: SolanaAgentKit, to: Pubkey, amount: float) -> str:
    # Create and send transaction for native SOL transfer

2. SPL Token Transfer

The transfer_spl_tokens method allows SPL token transfers with:

  • Validation of token mint initialization and associated token accounts.

  • Calculation of the exact token amount, accounting for decimal precision.

  • Execution of the transfer using the transfer_checked instruction.

Copy

3. Confirming Transactions

The confirm_transaction method ensures successful processing of transactions.

Copy

4. Transfer Execution

The TokenTransferManager class manages transfers by:

  • Handling both SOL and SPL token transfers dynamically.

  • Storing a history of transfers and verifying transaction success.

Copy


Key Features

  1. Native SOL Transfers Send SOL with just a recipient's address and the desired amount.

  2. SPL Token Transfers Fully handle SPL token transfers, including mint validation and error detection.

  3. Transaction Verification Verify the success of transfers directly through the blockchain.

  4. Transfer History Management Access a complete record of executed transfers for auditing and debugging.


How to Use

  • Setup: Import the required classes and initialize a SolanaAgentKit with your wallet.

  • Native SOL Transfer Example:

    Copy

  • SPL Token Transfer Example:

    Copy

  • Verification Example:

    Copy


Error Handling

  • Ensure the mint and associated token accounts are initialized for SPL transfers.

  • Handle insufficient funds or invalid decimal values gracefully.

Last updated