@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
227 lines (205 loc) ⢠6.86 kB
text/typescript
import {
Action,
ActionResult,
HandlerCallback,
IAgentRuntime,
Memory,
State,
logger,
} from '@elizaos/core';
import { BagsService } from '../services/BagsService';
/**
* Action for claiming fees from Bags protocol
* Triggers on phrases like "claim fees", "collect earnings", etc.
*/
export const claimFeesAction: Action = {
name: 'CLAIM_BAGS_FEES',
similes: ['CLAIM_FEES', 'COLLECT_EARNINGS', 'CLAIM_REWARDS', 'GET_FEES'],
description: 'Claims available fees from Bags protocol tokens',
validate: async (
runtime: IAgentRuntime,
message: Memory,
state?: State
): Promise<boolean> => {
const text = message.content.text?.toLowerCase();
if (!text) return false;
// Check for fee claiming triggers
const triggers = [
'claim fees',
'claim all fees',
'collect fees',
'collect earnings',
'claim rewards',
'get my fees',
'withdraw fees',
'claim from 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?.toLowerCase() || '';
let results;
// Check if claiming specific token or all tokens
if (text.includes('all') || !text.match(/[A-Za-z0-9]{32,}/)) {
// Claim all available fees
await callback?.({
text: 'š Checking for claimable fees across all tokens...',
action: 'CLAIM_BAGS_FEES',
});
results = await bagsService.claimAllFees();
} else {
// Try to extract token mint from message
const tokenMintMatch = text.match(/([A-Za-z0-9]{32,})/);
if (tokenMintMatch) {
const tokenMint = tokenMintMatch[1];
await callback?.({
text: `š Claiming fees for token: ${tokenMint.slice(0, 8)}...${tokenMint.slice(-8)}`,
action: 'CLAIM_BAGS_FEES',
});
const result = await bagsService.claimFeesForToken(tokenMint);
results = result ? [result] : [];
} else {
results = await bagsService.claimAllFees();
}
}
if (results.length === 0) {
await callback?.({
text: 'š No claimable fees found at this time. Your tokens might not have generated fees yet, or fees may have already been claimed.',
action: 'CLAIM_BAGS_FEES',
});
return {
success: true,
text: 'No fees to claim',
values: {
claimedCount: 0,
totalClaimed: 0,
},
data: {
actionName: 'CLAIM_BAGS_FEES',
results: [],
},
};
}
// Calculate totals
const totalClaimed = results.reduce((sum, result) => sum + result.claimedAmount, 0);
const successfulClaims = results.filter(r => r.success).length;
// Format response message
let responseText = `ā
Fee claiming completed!\n\n`;
responseText += `š° Total claimed: ${totalClaimed.toFixed(6)} SOL\n`;
responseText += `š Successful claims: ${successfulClaims}/${results.length}\n\n`;
if (results.length <= 5) {
// Show details for each claim if not too many
responseText += `Details:\n`;
for (const result of results) {
const tokenShort = `${result.tokenMint.slice(0, 8)}...${result.tokenMint.slice(-8)}`;
responseText += `⢠${tokenShort}: ${result.claimedAmount.toFixed(6)} SOL\n`;
}
} else {
responseText += `View full details in the analytics dashboard.`;
}
await callback?.({
text: responseText,
action: 'CLAIM_BAGS_FEES',
});
return {
success: true,
text: `Successfully claimed ${totalClaimed.toFixed(6)} SOL from ${successfulClaims} tokens`,
values: {
claimedCount: successfulClaims,
totalClaimed: totalClaimed,
results: results,
},
data: {
actionName: 'CLAIM_BAGS_FEES',
totalClaimed,
successfulClaims,
results,
},
};
} catch (error) {
logger.error(`Error in claim fees action: ${error instanceof Error ? error.message : String(error)}`);
await callback?.({
text: 'ā An error occurred while claiming fees. Please try again later.',
error: true,
});
return {
success: false,
text: 'Failed to claim fees',
error: error instanceof Error ? error : new Error(String(error)),
data: {
actionName: 'CLAIM_BAGS_FEES',
errorMessage: error instanceof Error ? error.message : String(error),
},
};
}
},
examples: [
[
{
name: '{{userName}}',
content: {
text: 'claim all my fees from bags',
actions: [],
},
},
{
name: '{{agentName}}',
content: {
text: 'š Checking for claimable fees across all tokens...\n\nā
Fee claiming completed!\n\nš° Total claimed: 0.125000 SOL\nš Successful claims: 3/3\n\nDetails:\n⢠4k3Dyjlb...8xMp1a2K: 0.045000 SOL\n⢠7wB5nCx9...2nFp4k7L: 0.032000 SOL\n⢠9mK8pLq2...5vGh3n8M: 0.048000 SOL',
actions: ['CLAIM_BAGS_FEES'],
},
},
],
[
{
name: '{{userName}}',
content: {
text: 'collect my earnings',
actions: [],
},
},
{
name: '{{agentName}}',
content: {
text: 'š Checking for claimable fees across all tokens...\n\nš No claimable fees found at this time. Your tokens might not have generated fees yet, or fees may have already been claimed.',
actions: ['CLAIM_BAGS_FEES'],
},
},
],
[
{
name: '{{userName}}',
content: {
text: 'claim fees for token 4k3Dyjlb8xMp1a2KcNvFp9L7wB5nCx92nFp4k7L5vGh3n8M',
actions: [],
},
},
{
name: '{{agentName}}',
content: {
text: 'š Claiming fees for token: 4k3Dyjlb...5vGh3n8M\n\nā
Fee claiming completed!\n\nš° Total claimed: 0.045000 SOL\nš Successful claims: 1/1\n\nDetails:\n⢠4k3Dyjlb...5vGh3n8M: 0.045000 SOL',
actions: ['CLAIM_BAGS_FEES'],
},
},
],
],
};