transfer
Transfer SOL or SPL tokens to a recipient’s wallet address.Function signature
Parameters
The agent instance containing wallet credentials and connection
The recipient’s wallet address
Amount to transfer in UI units (e.g., 1.5 for 1.5 SOL, not lamports)
Optional SPL token mint address. If not provided, transfers native SOL
Returns
Transaction signature if the transaction was sent to the network
Signed transaction object if agent is in
signOnly modeExample usage
How it works
The transfer function handles both SOL and SPL token transfers with different logic:SOL transfers
For native SOL transfers, the function:- Creates a
SystemProgram.transferinstruction - Converts the amount from SOL to lamports (
amount * LAMPORTS_PER_SOL) - Signs and sends the transaction
SPL token transfers
For SPL token transfers, the function:- Derives the sender’s associated token account (ATA)
- Derives the recipient’s ATA
- Automatically creates the recipient’s ATA if it doesn’t exist
- Fetches the token’s decimal places from the mint
- Adjusts the amount based on decimals
- Creates a transfer instruction
- Signs and sends the transaction
The function automatically handles ATA creation, so you don’t need to check if the recipient has an existing token account.
Transfer action
The Transfer action enables AI-driven natural language transfers. It’s registered with these triggers:- “send tokens”
- “transfer funds”
- “send money”
- “send sol”
- “transfer tokens”
Action schema
The action validates inputs using Zod:Natural language examples
With the action registered, users can say:- “Send 5 SOL to 8x2dR8Mpzuz2YqyZyZjUbYWKSWesBo5jMx2Q9Y86udVk”
- “Transfer 100 USDC to my friend”
- “Send money to this address”
Error handling
The transfer function throws descriptive errors:Additional utilities
closeEmptyTokenAccounts
Close empty SPL token accounts to reclaim rent (≈0.00203928 SOL per account).Function signature
How it works
- Queries all token accounts for both TOKEN_PROGRAM and TOKEN_2022_PROGRAM
- Filters for accounts with zero balance
- Excludes USDC accounts (to prevent accidental closure)
- Creates close instructions for up to 40 accounts per transaction
- Signs and sends the transaction
The function processes a maximum of 40 close instructions per transaction to avoid hitting transaction size limits.
Returns
Transaction signature (when not in signOnly mode)
Signed transaction object (when in signOnly mode)
Total number of empty token accounts found (not just those closed in this transaction)
Exception list
The following token accounts are never closed:- USDC (
EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v)
accountExceptions array in the source:
Best practices
Always check balances first
Always check balances first
Before transferring, verify the sender has sufficient balance:
Validate recipient addresses
Validate recipient addresses
Always validate that recipient addresses are valid Solana public keys:
Use UI amounts, not raw amounts
Use UI amounts, not raw amounts
Always pass amounts in UI units (e.g., 1.5 SOL, 100 USDC) rather than raw units (lamports, smallest token units). The function handles decimal conversion automatically:
Handle transaction failures gracefully
Handle transaction failures gracefully
Network issues, insufficient funds, and other errors can cause transfers to fail. Always use try-catch blocks:
Consider transaction fees
Consider transaction fees
Every transaction on Solana costs approximately 0.000005 SOL (5000 lamports) in fees. When transferring the entire balance, leave room for the fee:
Common errors
| Error | Cause | Solution |
|---|---|---|
| ”Insufficient balance” | Sender doesn’t have enough tokens | Check balance before transfer |
| ”Invalid Solana address” | Recipient address is malformed | Validate address format |
| ”Token account not found” | Sender doesn’t have the token account | Ensure sender has received this token before |
| ”Transfer failed: 0x1” | Insufficient SOL for transaction fee | Ensure wallet has SOL for fees |
| ”Transfer failed: account not found” | Sender’s token account doesn’t exist | Verify the mint address is correct |
Next steps
Balance operations
Check balances before transferring
Jupiter integration
Swap tokens before or after transfers
DeFi plugin
Explore lending and borrowing
Wallet operations
Learn about wallet management