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

async def transfer_spl_tokens(rpc_client: AsyncClient, agent: SolanaAgentKit, recipient: Pubkey, spl_token: Pubkey, amount: float) -> str:
    # Perform token transfer with validation and error handling

3. Confirming Transactions

The confirm_transaction method ensures successful processing of transactions.

Copy

async def confirm_transaction(agent: SolanaAgentKit, signature: str) -> None:
    # Confirm transaction on the blockchain

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

async def execute_transfer(to: Pubkey, amount: float, mint: Optional[Pubkey] = None) -> TransferResult:
    # Execute either SOL or SPL token transfer based on parameters

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

    await SolanaTransferHelper.transfer_native_sol(agent, recipient_address, amount)
  • SPL Token Transfer Example:

    Copy

    await SolanaTransferHelper.transfer_spl_tokens(rpc_client, agent, recipient_address, token_mint_address, amount)
  • Verification Example:

    Copy

    is_successful = await TokenTransferManager.verify_transfer(transfer_result)

Error Handling

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

  • Handle insufficient funds or invalid decimal values gracefully.

Last updated