FORGE Header

Plugins

Token Plugin

Complete SPL token management plugin for Solana Agent Kit

Overview

The Token Plugin provides comprehensive SPL token functionality for the Solana Agent Kit. It enables token transfers, balance queries, token creation, and swap operations through a simple API.

Installation

npm install @solana-agent-kit/plugin-token

Available Methods

transferToken()

Transfer SPL tokens to another wallet address.

import { transferToken } from '@solana-agent-kit/plugin-token'; await transferToken({ recipient: 'RECIPIENT_ADDRESS', tokenMint: 'TOKEN_MINT_ADDRESS', amount: 100, decimals: 6 });
Parameters: recipient, tokenMint, amount, decimals

getTokenBalance()

Get the balance of a specific SPL token in a wallet.

import { getTokenBalance } from '@solana-agent-kit/plugin-token'; const balance = await getTokenBalance({ walletAddress: 'WALLET_ADDRESS', tokenMint: 'TOKEN_MINT_ADDRESS' }); console.log(`Balance: ${balance}`);

createToken()

Create a new SPL token with custom metadata.

import { createToken } from '@solana-agent-kit/plugin-token'; const tokenMint = await createToken({ name: 'My Token', symbol: 'MTK', decimals: 9, initialSupply: 1000000 });

swapToken()

Swap one token for another using Jupiter aggregator.

import { swapToken } from '@solana-agent-kit/plugin-token'; await swapToken({ inputMint: 'SOL_MINT_ADDRESS', outputMint: 'USDC_MINT_ADDRESS', amount: 1000000000, // 1 SOL in lamports slippage: 100 // 1% slippage });

Configuration

Configure the plugin with your agent instance:

import { SolanaAgentKit } from 'solana-agent-kit'; import { TokenPlugin } from '@solana-agent-kit/plugin-token'; const agent = new SolanaAgentKit({ privateKey: process.env.SOLANA_PRIVATE_KEY, rpcUrl: process.env.SOLANA_RPC_URL }); // Plugin is automatically loaded agent.use(TokenPlugin);

Common Use Cases

Batch Token Transfers

Send tokens to multiple recipients in a single transaction:

const recipients = [ { address: 'ADDR1', amount: 100 }, { address: 'ADDR2', amount: 200 } ]; for (const recipient of recipients) { await transferToken({ recipient: recipient.address, tokenMint: TOKEN_MINT, amount: recipient.amount }); }

Portfolio Balance Check

Get balances for all your tokens:

const tokens = ['USDC_MINT', 'USDT_MINT', 'SOL_MINT']; const balances = await Promise.all( tokens.map(mint => getTokenBalance({ walletAddress: WALLET_ADDRESS, tokenMint: mint })) );

Error Handling

Always wrap token operations in try-catch blocks:

try { await transferToken({ recipient: 'RECIPIENT_ADDRESS', tokenMint: 'TOKEN_MINT', amount: 100 }); console.log('Transfer successful'); } catch (error) { console.error('Transfer failed:', error.message); // Handle insufficient balance, invalid address, etc. }