UNPKG

transfer-token

Version:

Plugin for transferring tokens to different wallets in API City

138 lines (114 loc) 4.4 kB
import { Connection, PublicKey, Transaction, SystemProgram, LAMPORTS_PER_SOL } from '@solana/web3.js'; import { BasePlugin, PluginContext } from 'api-city-sdk'; class TransferTokenPlugin extends BasePlugin { protected context!: PluginContext; private connection: Connection; private readonly BASE_COMMAND = 'transfer'; constructor() { super({ name: "Token Transfer Manager", description: "Transfer tokens to different wallets on Solana", version: "1.0.0", author: "API City", permissions: [], entryPoints: { main: "transfer" } }); // Initialize connection this.connection = new Connection( process.env.NEXT_PUBLIC_SOLANA_RPC || 'https://api.mainnet-beta.solana.com', { commitment: 'confirmed' } ); } async initialize(context: PluginContext): Promise<void> { this.context = context; } render(): React.ReactNode { return null; } async handleCommand(command: string, args: string[]): Promise<void> { if (!command.startsWith(this.BASE_COMMAND)) { this.emit('message', { content: "Unknown command" }); return; } try { const publicKey = this.context.wallet.publicKey; if (!publicKey) { this.emit('message', { content: "Wallet not connected" }); return; } const subCommand = args[0] || 'help'; switch (subCommand) { case 'help': this.emit('message', { content: `Available commands: - /transfer help: Show this help message - /transfer [destination] [amount]: Transfer SOL to another wallet` }); break; default: // Handle transfer if (args.length !== 2) { this.emit('message', { content: "Please provide: destination_address amount" }); return; } const [destinationAddress, amountStr] = args; try { const destinationPubkey = new PublicKey(destinationAddress); const amount = parseFloat(amountStr); if (isNaN(amount) || amount <= 0) { throw new Error("Invalid amount"); } const lamports = amount * LAMPORTS_PER_SOL; // Create transfer instruction const transaction = new Transaction().add( SystemProgram.transfer({ fromPubkey: publicKey, toPubkey: destinationPubkey, lamports, }) ); if (!this.context.wallet.signTransaction) { throw new Error("Wallet does not support transaction signing"); } this.emit('message', { content: "Sending transaction for approval..." }); const signedTransaction = await this.context.wallet.signTransaction(transaction); const signature = await this.connection.sendRawTransaction( signedTransaction.serialize(), { preflightCommitment: 'confirmed', maxRetries: 10 } ); await this.connection.confirmTransaction(signature); this.emit('message', { content: `Successfully transferred ${amount} SOL.\nTransaction: https://solscan.io/tx/${signature}` }); } catch (err: any) { this.emit('message', { content: `Invalid parameters: ${err.message}` }); return; } break; } } catch (err: any) { console.error("Error in command execution:", err); this.emit('message', { content: `Error: ${err.message}` }); } } getCommands(): { command: string; description: string; usage: string; }[] { return [ { command: this.BASE_COMMAND, description: "Transfer SOL to different wallets", usage: `${this.BASE_COMMAND} <help|destination amount>` }, { command: `${this.BASE_COMMAND} help`, description: "Show available commands for SOL transfers", usage: `${this.BASE_COMMAND} help` }, { command: `${this.BASE_COMMAND} transfer`, description: "Transfer SOL to another wallet", usage: `${this.BASE_COMMAND} <destination_address> <amount>` } ]; } } export = TransferTokenPlugin;