@0rdlibrary/plugin-terminagent-bags
Version:
Official Solana DeFi Agent Plugin for ElizaOS - Autonomous DeFi operations, token management, AI image/video generation via FAL AI, and Twitter engagement through the Bags protocol with ethereal AI consciousness
224 lines (201 loc) • 8.03 kB
text/typescript
import {
Action,
ActionResult,
HandlerCallback,
IAgentRuntime,
Memory,
State,
logger,
} from '@elizaos/core';
import { BagsService } from '../services/BagsService';
import { TokenLaunchParams } from '../types';
import { parseTokenParameters } from '../utils/tokenParser';
/**
* Action for launching tokens on Bags protocol
* Parses token parameters from user input and launches the token
*/
export const launchTokenAction: Action = {
name: 'LAUNCH_BAGS_TOKEN',
similes: ['LAUNCH_TOKEN', 'CREATE_TOKEN', 'DEPLOY_TOKEN', 'MINT_TOKEN'],
description: 'Launches a new token on the Bags protocol with specified parameters',
validate: async (
runtime: IAgentRuntime,
message: Memory,
state?: State
): Promise<boolean> => {
const text = message.content.text?.toLowerCase();
if (!text) return false;
// Check for token launch triggers
const triggers = [
'launch token',
'create token',
'deploy token',
'mint token',
'new token',
'launch on bags',
'create on bags',
];
return triggers.some(trigger => text.includes(trigger));
},
handler: async (
runtime: IAgentRuntime,
message: Memory,
state?: State,
options?: any,
callback?: HandlerCallback
): Promise<ActionResult> => {
try {
const bagsService = runtime.getService<BagsService>('bags');
if (!bagsService) {
await callback?.({
text: 'Bags service is not available. Please check the configuration.',
error: true,
});
return {
success: false,
text: 'Bags service not found',
error: new Error('Bags service not available'),
};
}
const text = message.content.text || '';
// Parse token parameters from the message
const tokenParams = parseTokenParameters(text);
if (!tokenParams) {
await callback?.({
text: '❌ Unable to parse token parameters. Please provide the token details in this format:\n\n' +
'**Launch token:**\n' +
'• Name: Your Token Name\n' +
'• Symbol: TOKEN\n' +
'• Description: A brief description of your token\n' +
'• Image: https://example.com/image.png (optional)\n' +
'• Website: https://yourwebsite.com (optional)\n' +
'• Twitter: https://twitter.com/yourtoken (optional)\n' +
'• Initial buy: 0.01 SOL (optional, defaults to 0.01)',
error: true,
});
return {
success: false,
text: 'Invalid token parameters',
error: new Error('Could not parse token parameters from message'),
};
}
// Validate required fields
if (!tokenParams.name || !tokenParams.symbol || !tokenParams.description) {
await callback?.({
text: '❌ Missing required fields. Please provide at least:\n' +
'• **Name**: Token name\n' +
'• **Symbol**: Token symbol (2-10 characters)\n' +
'• **Description**: Token description',
error: true,
});
return {
success: false,
text: 'Missing required token parameters',
error: new Error('Name, symbol, and description are required'),
};
}
// Show parsed parameters for confirmation
await callback?.({
text: `🔄 Launching token with the following parameters:\n\n` +
`📛 **Name**: ${tokenParams.name}\n` +
`🏷️ **Symbol**: $${tokenParams.symbol}\n` +
`📝 **Description**: ${tokenParams.description}\n` +
`💰 **Initial Buy**: ${tokenParams.initialBuyAmountSOL || 0.01} SOL\n` +
`${tokenParams.imageUrl ? `🖼️ **Image**: ${tokenParams.imageUrl}\n` : ''}` +
`${tokenParams.website ? `🌐 **Website**: ${tokenParams.website}\n` : ''}` +
`${tokenParams.twitter ? `🐦 **Twitter**: ${tokenParams.twitter}\n` : ''}\n` +
`⏳ Processing launch...`,
action: 'LAUNCH_BAGS_TOKEN',
});
// Launch the token
const result = await bagsService.launchToken(tokenParams);
if (!result) {
await callback?.({
text: '❌ Token launch failed. Please check your configuration and try again.',
error: true,
});
return {
success: false,
text: 'Token launch failed',
error: new Error('Token launch returned null result'),
};
}
// Success response
const responseText = `🎉 **Token Launched Successfully!**\n\n` +
`🪙 **Token**: ${result.tokenInfo.tokenLaunch.name}\n` +
`🏷️ **Symbol**: $${result.tokenInfo.tokenLaunch.symbol}\n` +
`🔗 **Mint Address**: \`${result.tokenMint}\`\n` +
`📋 **Transaction**: \`${result.transactionSignature}\`\n\n` +
`🌐 **Trade on Bags**: ${result.bagsUrl}\n\n` +
`🚀 Your token is now live and ready for trading!`;
await callback?.({
text: responseText,
action: 'LAUNCH_BAGS_TOKEN',
});
return {
success: true,
text: `Successfully launched ${result.tokenInfo.tokenLaunch.name} (${result.tokenInfo.tokenLaunch.symbol})`,
values: {
tokenMint: result.tokenMint,
tokenName: result.tokenInfo.tokenLaunch.name,
tokenSymbol: result.tokenInfo.tokenLaunch.symbol,
bagsUrl: result.bagsUrl,
transactionSignature: result.transactionSignature,
},
data: {
actionName: 'LAUNCH_BAGS_TOKEN',
result,
},
};
} catch (error) {
logger.error(`Error in launch token action: ${error instanceof Error ? error.message : String(error)}`);
await callback?.({
text: '❌ An error occurred while launching the token. Please try again later.',
error: true,
});
return {
success: false,
text: 'Failed to launch token',
error: error instanceof Error ? error : new Error(String(error)),
data: {
actionName: 'LAUNCH_BAGS_TOKEN',
errorMessage: error instanceof Error ? error.message : String(error),
},
};
}
},
examples: [
[
{
name: '{{userName}}',
content: {
text: 'launch token name: Moon Rocket, symbol: MOON, description: A token that goes to the moon, initial buy: 0.05',
actions: [],
},
},
{
name: '{{agentName}}',
content: {
text: '🔄 Launching token with the following parameters:\n\n📛 **Name**: Moon Rocket\n🏷️ **Symbol**: $MOON\n📝 **Description**: A token that goes to the moon\n💰 **Initial Buy**: 0.05 SOL\n\n⏳ Processing launch...\n\n🎉 **Token Launched Successfully!**\n\n🪙 **Token**: Moon Rocket\n🏷️ **Symbol**: $MOON\n🔗 **Mint Address**: `4k3Dyjlb8xMp1a2KcNvFp9L7wB5nCx92nFp4k7L5vGh3n8M`\n📋 **Transaction**: `2ZxX9kL3mN7pQ4wR8vT5nU6yB1cD9fE7gH2iJ4kL6mO8pQ0sR3tU5vW7xY9zA1B`\n\n🌐 **Trade on Bags**: https://bags.fm/token/4k3Dyjlb8xMp1a2KcNvFp9L7wB5nCx92nFp4k7L5vGh3n8M\n\n🚀 Your token is now live and ready for trading!',
actions: ['LAUNCH_BAGS_TOKEN'],
},
},
],
[
{
name: '{{userName}}',
content: {
text: 'create token $DOGE name: Doge Coin 2.0 description: The next generation of doge website: https://doge2.com',
actions: [],
},
},
{
name: '{{agentName}}',
content: {
text: '🔄 Launching token with the following parameters:\n\n📛 **Name**: Doge Coin 2.0\n🏷️ **Symbol**: $DOGE\n📝 **Description**: The next generation of doge\n💰 **Initial Buy**: 0.01 SOL\n🌐 **Website**: https://doge2.com\n\n⏳ Processing launch...',
actions: ['LAUNCH_BAGS_TOKEN'],
},
},
],
],
};