@gala-chain/launchpad-mcp-server 1.7.2 → 1.7.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +40 -4
- package/docs/AI-AGENT-PATTERNS.md +555 -0
- package/docs/CONSTRAINTS-REFERENCE.md +454 -0
- package/docs/PROMPT-TOOL-MAPPING.md +352 -0
- package/docs/examples/default-values-pattern.md +240 -0
- package/docs/examples/tool-factory-pattern.md +217 -0
- package/jest.config.js +94 -0
- package/package.json +2 -2
- package/src/__tests__/integration/poolTools.integration.test.ts +185 -0
- package/src/constants/mcpToolNames.ts +141 -0
- package/src/index.ts +19 -0
- package/src/prompts/__tests__/promptStructure.test.ts +114 -0
- package/src/prompts/__tests__/registry.test.ts +143 -0
- package/src/prompts/analysis.ts +429 -0
- package/src/prompts/index.ts +123 -0
- package/src/prompts/portfolio.ts +242 -0
- package/src/prompts/trading.ts +191 -0
- package/src/prompts/utils/workflowTemplates.ts +344 -0
- package/src/schemas/common-schemas.ts +392 -0
- package/src/scripts/test-all-prompts.ts +184 -0
- package/src/server.ts +241 -0
- package/src/tools/balance/index.ts +174 -0
- package/src/tools/creation/index.ts +182 -0
- package/src/tools/index.ts +80 -0
- package/src/tools/pools/fetchAllPools.ts +47 -0
- package/src/tools/pools/fetchPoolDetails.ts +27 -0
- package/src/tools/pools/fetchPoolDetailsForCalculation.ts +22 -0
- package/src/tools/pools/fetchPools.ts +47 -0
- package/src/tools/pools/index.ts +240 -0
- package/src/tools/social/index.ts +64 -0
- package/src/tools/trading/index.ts +605 -0
- package/src/tools/transfers/index.ts +75 -0
- package/src/tools/utils/clearCache.ts +36 -0
- package/src/tools/utils/createWallet.ts +19 -0
- package/src/tools/utils/explainSdkUsage.ts +765 -0
- package/src/tools/utils/getAddress.ts +12 -0
- package/src/tools/utils/getCacheInfo.ts +14 -0
- package/src/tools/utils/getConfig.ts +11 -0
- package/src/tools/utils/getEthereumAddress.ts +12 -0
- package/src/tools/utils/getUrlByTokenName.ts +12 -0
- package/src/tools/utils/index.ts +25 -0
- package/src/tools/utils/isTokenGraduated.ts +16 -0
- package/src/types/mcp.ts +72 -0
- package/src/utils/__tests__/validation.test.ts +147 -0
- package/src/utils/constraints.ts +146 -0
- package/src/utils/default-values.ts +208 -0
- package/src/utils/error-handler.ts +69 -0
- package/src/utils/error-templates.ts +273 -0
- package/src/utils/response-formatter.ts +51 -0
- package/src/utils/tool-factory.ts +257 -0
- package/src/utils/tool-registry.ts +296 -0
- package/src/utils/validation.ts +336 -0
- package/tsconfig.json +23 -0
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tool Registry System
|
|
3
|
+
*
|
|
4
|
+
* Enhanced tool registration with auto-validation, metadata, and search capabilities.
|
|
5
|
+
* Provides runtime validation and developer-friendly tool discovery.
|
|
6
|
+
*
|
|
7
|
+
* @see Phase 3.1 of refactoring plan
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { MCPTool } from '../types/mcp.js';
|
|
11
|
+
|
|
12
|
+
// =============================================================================
|
|
13
|
+
// Type Definitions
|
|
14
|
+
// =============================================================================
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Tool category metadata
|
|
18
|
+
*/
|
|
19
|
+
export interface ToolCategory {
|
|
20
|
+
name: string;
|
|
21
|
+
description: string;
|
|
22
|
+
tools: MCPTool[];
|
|
23
|
+
count: number;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Tool registry with metadata and search capabilities
|
|
28
|
+
*/
|
|
29
|
+
export interface ToolRegistry {
|
|
30
|
+
tools: MCPTool[];
|
|
31
|
+
categories: Map<string, ToolCategory>;
|
|
32
|
+
totalCount: number;
|
|
33
|
+
expectedCount: number;
|
|
34
|
+
isValid: boolean;
|
|
35
|
+
errors: string[];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Tool validation result
|
|
40
|
+
*/
|
|
41
|
+
export interface ToolValidationResult {
|
|
42
|
+
isValid: boolean;
|
|
43
|
+
errors: string[];
|
|
44
|
+
warnings: string[];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// =============================================================================
|
|
48
|
+
// Validation Functions
|
|
49
|
+
// =============================================================================
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Validates a single tool definition
|
|
53
|
+
*/
|
|
54
|
+
export function validateTool(tool: MCPTool): ToolValidationResult {
|
|
55
|
+
const errors: string[] = [];
|
|
56
|
+
const warnings: string[] = [];
|
|
57
|
+
|
|
58
|
+
// Required fields
|
|
59
|
+
if (!tool.name) {
|
|
60
|
+
errors.push('Tool name is required');
|
|
61
|
+
} else if (typeof tool.name !== 'string') {
|
|
62
|
+
errors.push('Tool name must be a string');
|
|
63
|
+
} else if (!tool.name.startsWith('gala_launchpad_')) {
|
|
64
|
+
warnings.push(`Tool name "${tool.name}" should start with "gala_launchpad_"`);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (!tool.description) {
|
|
68
|
+
errors.push('Tool description is required');
|
|
69
|
+
} else if (typeof tool.description !== 'string') {
|
|
70
|
+
errors.push('Tool description must be a string');
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (!tool.inputSchema) {
|
|
74
|
+
errors.push('Tool inputSchema is required');
|
|
75
|
+
} else {
|
|
76
|
+
// Validate input schema structure
|
|
77
|
+
if (tool.inputSchema.type !== 'object') {
|
|
78
|
+
errors.push('inputSchema.type must be "object"');
|
|
79
|
+
}
|
|
80
|
+
if (!tool.inputSchema.properties) {
|
|
81
|
+
warnings.push('inputSchema.properties is empty');
|
|
82
|
+
}
|
|
83
|
+
if (tool.inputSchema.required && !Array.isArray(tool.inputSchema.required)) {
|
|
84
|
+
errors.push('inputSchema.required must be an array');
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (!tool.handler) {
|
|
89
|
+
errors.push('Tool handler is required');
|
|
90
|
+
} else if (typeof tool.handler !== 'function') {
|
|
91
|
+
errors.push('Tool handler must be a function');
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return {
|
|
95
|
+
isValid: errors.length === 0,
|
|
96
|
+
errors,
|
|
97
|
+
warnings,
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Validates all tools in a registry
|
|
103
|
+
*/
|
|
104
|
+
export function validateTools(tools: MCPTool[]): ToolValidationResult {
|
|
105
|
+
const allErrors: string[] = [];
|
|
106
|
+
const allWarnings: string[] = [];
|
|
107
|
+
const toolNames = new Set<string>();
|
|
108
|
+
|
|
109
|
+
tools.forEach((tool, index) => {
|
|
110
|
+
const result = validateTool(tool);
|
|
111
|
+
|
|
112
|
+
// Collect errors with tool context
|
|
113
|
+
result.errors.forEach((error) => {
|
|
114
|
+
allErrors.push(`Tool #${index + 1} (${tool.name || 'unknown'}): ${error}`);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
result.warnings.forEach((warning) => {
|
|
118
|
+
allWarnings.push(`Tool #${index + 1} (${tool.name || 'unknown'}): ${warning}`);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
// Check for duplicate names
|
|
122
|
+
if (tool.name) {
|
|
123
|
+
if (toolNames.has(tool.name)) {
|
|
124
|
+
allErrors.push(`Duplicate tool name: ${tool.name}`);
|
|
125
|
+
}
|
|
126
|
+
toolNames.add(tool.name);
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
return {
|
|
131
|
+
isValid: allErrors.length === 0,
|
|
132
|
+
errors: allErrors,
|
|
133
|
+
warnings: allWarnings,
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// =============================================================================
|
|
138
|
+
// Registry Functions
|
|
139
|
+
// =============================================================================
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Creates a tool registry with metadata and validation
|
|
143
|
+
*/
|
|
144
|
+
export function createToolRegistry(
|
|
145
|
+
categories: Array<{ name: string; description: string; tools: MCPTool[] }>,
|
|
146
|
+
expectedCount?: number
|
|
147
|
+
): ToolRegistry {
|
|
148
|
+
const allTools: MCPTool[] = [];
|
|
149
|
+
const categoryMap = new Map<string, ToolCategory>();
|
|
150
|
+
const registryErrors: string[] = [];
|
|
151
|
+
|
|
152
|
+
// Process each category
|
|
153
|
+
categories.forEach((category) => {
|
|
154
|
+
const toolCategory: ToolCategory = {
|
|
155
|
+
name: category.name,
|
|
156
|
+
description: category.description,
|
|
157
|
+
tools: category.tools,
|
|
158
|
+
count: category.tools.length,
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
categoryMap.set(category.name, toolCategory);
|
|
162
|
+
allTools.push(...category.tools);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
// Validate all tools
|
|
166
|
+
const validation = validateTools(allTools);
|
|
167
|
+
registryErrors.push(...validation.errors);
|
|
168
|
+
|
|
169
|
+
// Check expected count
|
|
170
|
+
const actualCount = allTools.length;
|
|
171
|
+
const hasExpectedCount = expectedCount !== undefined;
|
|
172
|
+
const countMatches = hasExpectedCount ? actualCount === expectedCount : true;
|
|
173
|
+
|
|
174
|
+
if (hasExpectedCount && !countMatches) {
|
|
175
|
+
registryErrors.push(
|
|
176
|
+
`Tool count mismatch: expected ${expectedCount}, got ${actualCount}`
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
return {
|
|
181
|
+
tools: allTools,
|
|
182
|
+
categories: categoryMap,
|
|
183
|
+
totalCount: actualCount,
|
|
184
|
+
expectedCount: expectedCount ?? actualCount,
|
|
185
|
+
isValid: registryErrors.length === 0,
|
|
186
|
+
errors: registryErrors,
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Finds a tool by name
|
|
192
|
+
*/
|
|
193
|
+
export function findTool(registry: ToolRegistry, name: string): MCPTool | undefined {
|
|
194
|
+
return registry.tools.find((tool) => tool.name === name);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Finds tools by category
|
|
199
|
+
*/
|
|
200
|
+
export function findToolsByCategory(
|
|
201
|
+
registry: ToolRegistry,
|
|
202
|
+
categoryName: string
|
|
203
|
+
): MCPTool[] {
|
|
204
|
+
const category = registry.categories.get(categoryName);
|
|
205
|
+
return category ? category.tools : [];
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Searches tools by name pattern
|
|
210
|
+
*/
|
|
211
|
+
export function searchTools(registry: ToolRegistry, pattern: string): MCPTool[] {
|
|
212
|
+
const regex = new RegExp(pattern, 'i');
|
|
213
|
+
return registry.tools.filter((tool) => regex.test(tool.name));
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Gets tool statistics by category
|
|
218
|
+
*/
|
|
219
|
+
export function getToolStatistics(registry: ToolRegistry): {
|
|
220
|
+
total: number;
|
|
221
|
+
byCategory: Record<string, number>;
|
|
222
|
+
} {
|
|
223
|
+
const byCategory: Record<string, number> = {};
|
|
224
|
+
|
|
225
|
+
registry.categories.forEach((category, name) => {
|
|
226
|
+
byCategory[name] = category.count;
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
return {
|
|
230
|
+
total: registry.totalCount,
|
|
231
|
+
byCategory,
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// =============================================================================
|
|
236
|
+
// Logging and Reporting
|
|
237
|
+
// =============================================================================
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Logs tool registry statistics
|
|
241
|
+
*/
|
|
242
|
+
export function logToolRegistry(registry: ToolRegistry): void {
|
|
243
|
+
console.log('\n📦 Tool Registry Statistics:');
|
|
244
|
+
console.log(` Total Tools: ${registry.totalCount}/${registry.expectedCount}`);
|
|
245
|
+
console.log(` Valid: ${registry.isValid ? '✅' : '❌'}`);
|
|
246
|
+
|
|
247
|
+
if (!registry.isValid && registry.errors.length > 0) {
|
|
248
|
+
console.log('\n❌ Validation Errors:');
|
|
249
|
+
registry.errors.forEach((error) => console.log(` - ${error}`));
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
console.log('\n📂 Categories:');
|
|
253
|
+
registry.categories.forEach((category) => {
|
|
254
|
+
console.log(` ${category.name}: ${category.count} tools`);
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
console.log('');
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Generates markdown documentation for tool registry
|
|
262
|
+
*/
|
|
263
|
+
export function generateToolDocumentation(registry: ToolRegistry): string {
|
|
264
|
+
let doc = '# MCP Tool Registry\n\n';
|
|
265
|
+
doc += `**Total Tools**: ${registry.totalCount}\n\n`;
|
|
266
|
+
|
|
267
|
+
doc += '## Categories\n\n';
|
|
268
|
+
registry.categories.forEach((category) => {
|
|
269
|
+
doc += `### ${category.name} (${category.count} tools)\n\n`;
|
|
270
|
+
doc += `${category.description}\n\n`;
|
|
271
|
+
|
|
272
|
+
category.tools.forEach((tool) => {
|
|
273
|
+
doc += `- **${tool.name}**: ${tool.description}\n`;
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
doc += '\n';
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
return doc;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// =============================================================================
|
|
283
|
+
// Export All
|
|
284
|
+
// =============================================================================
|
|
285
|
+
|
|
286
|
+
export const toolRegistry = {
|
|
287
|
+
createToolRegistry,
|
|
288
|
+
validateTool,
|
|
289
|
+
validateTools,
|
|
290
|
+
findTool,
|
|
291
|
+
findToolsByCategory,
|
|
292
|
+
searchTools,
|
|
293
|
+
getToolStatistics,
|
|
294
|
+
logToolRegistry,
|
|
295
|
+
generateToolDocumentation,
|
|
296
|
+
};
|
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validation Utilities for MCP Prompts
|
|
3
|
+
*
|
|
4
|
+
* Input validation functions for slash command arguments.
|
|
5
|
+
* Prevents malformed inputs and provides clear error messages.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Validation error class for better error handling
|
|
10
|
+
*/
|
|
11
|
+
export class ValidationError extends Error {
|
|
12
|
+
constructor(
|
|
13
|
+
public field: string,
|
|
14
|
+
message: string
|
|
15
|
+
) {
|
|
16
|
+
super(`Validation error for '${field}': ${message}`);
|
|
17
|
+
this.name = 'ValidationError';
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Validate token name format
|
|
23
|
+
*
|
|
24
|
+
* Token names must be 2-20 characters, alphanumeric with hyphens and underscores.
|
|
25
|
+
*
|
|
26
|
+
* @param name - Token name to validate
|
|
27
|
+
* @param fieldName - Field name for error messages (default: 'tokenName')
|
|
28
|
+
* @throws {ValidationError} If token name is invalid
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```typescript
|
|
32
|
+
* validateTokenName('anime'); // ✅ Valid
|
|
33
|
+
* validateTokenName('test-123'); // ✅ Valid
|
|
34
|
+
* validateTokenName('my_token'); // ✅ Valid
|
|
35
|
+
* validateTokenName('a'); // ❌ Throws: too short
|
|
36
|
+
* validateTokenName('token@#$'); // ❌ Throws: invalid characters
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export function validateTokenName(name: string, fieldName = 'tokenName'): void {
|
|
40
|
+
if (!name || typeof name !== 'string') {
|
|
41
|
+
throw new ValidationError(fieldName, 'Token name is required');
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (name.length < 2 || name.length > 20) {
|
|
45
|
+
throw new ValidationError(
|
|
46
|
+
fieldName,
|
|
47
|
+
`Token name must be 2-20 characters (got ${name.length})`
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const tokenNameRegex = /^[a-zA-Z0-9-_]+$/;
|
|
52
|
+
if (!tokenNameRegex.test(name)) {
|
|
53
|
+
throw new ValidationError(
|
|
54
|
+
fieldName,
|
|
55
|
+
'Token name can only contain letters, numbers, hyphens, and underscores'
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Validate numeric amount
|
|
62
|
+
*
|
|
63
|
+
* Amounts must be positive numbers (integers or decimals).
|
|
64
|
+
*
|
|
65
|
+
* @param amount - Amount to validate (string or number)
|
|
66
|
+
* @param fieldName - Field name for error messages
|
|
67
|
+
* @throws {ValidationError} If amount is invalid
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```typescript
|
|
71
|
+
* validateNumericAmount('100', 'galaAmount'); // ✅ Valid
|
|
72
|
+
* validateNumericAmount('99.5', 'tokenAmount'); // ✅ Valid
|
|
73
|
+
* validateNumericAmount('-5', 'amount'); // ❌ Throws: negative
|
|
74
|
+
* validateNumericAmount('abc', 'amount'); // ❌ Throws: not a number
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
export function validateNumericAmount(
|
|
78
|
+
amount: string | number,
|
|
79
|
+
fieldName: string
|
|
80
|
+
): void {
|
|
81
|
+
if (amount === null || amount === undefined || amount === '') {
|
|
82
|
+
throw new ValidationError(fieldName, 'Amount is required');
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const numAmount = typeof amount === 'string' ? parseFloat(amount) : amount;
|
|
86
|
+
|
|
87
|
+
if (isNaN(numAmount)) {
|
|
88
|
+
throw new ValidationError(fieldName, `Amount must be a valid number (got: ${amount})`);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (numAmount <= 0) {
|
|
92
|
+
throw new ValidationError(
|
|
93
|
+
fieldName,
|
|
94
|
+
`Amount must be positive (got: ${numAmount})`
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (!isFinite(numAmount)) {
|
|
99
|
+
throw new ValidationError(fieldName, 'Amount must be a finite number');
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Validate slippage tolerance percentage
|
|
105
|
+
*
|
|
106
|
+
* Slippage must be between 0.01% and 100%.
|
|
107
|
+
*
|
|
108
|
+
* @param slippage - Slippage percentage to validate
|
|
109
|
+
* @param fieldName - Field name for error messages (default: 'slippage')
|
|
110
|
+
* @throws {ValidationError} If slippage is invalid
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* ```typescript
|
|
114
|
+
* validateSlippage('1'); // ✅ Valid (1%)
|
|
115
|
+
* validateSlippage('0.5'); // ✅ Valid (0.5%)
|
|
116
|
+
* validateSlippage('50'); // ✅ Valid (50%)
|
|
117
|
+
* validateSlippage('0'); // ❌ Throws: too low
|
|
118
|
+
* validateSlippage('150'); // ❌ Throws: too high
|
|
119
|
+
* ```
|
|
120
|
+
*/
|
|
121
|
+
export function validateSlippage(
|
|
122
|
+
slippage: string | number,
|
|
123
|
+
fieldName = 'slippage'
|
|
124
|
+
): void {
|
|
125
|
+
if (slippage === null || slippage === undefined || slippage === '') {
|
|
126
|
+
throw new ValidationError(fieldName, 'Slippage is required');
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const numSlippage = typeof slippage === 'string' ? parseFloat(slippage) : slippage;
|
|
130
|
+
|
|
131
|
+
if (isNaN(numSlippage)) {
|
|
132
|
+
throw new ValidationError(
|
|
133
|
+
fieldName,
|
|
134
|
+
`Slippage must be a valid number (got: ${slippage})`
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (numSlippage < 0.01 || numSlippage > 100) {
|
|
139
|
+
throw new ValidationError(
|
|
140
|
+
fieldName,
|
|
141
|
+
`Slippage must be between 0.01% and 100% (got: ${numSlippage}%)`
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Validate wallet address format
|
|
148
|
+
*
|
|
149
|
+
* Supports both GalaChain format (eth|0x...) and standard Ethereum format (0x...).
|
|
150
|
+
*
|
|
151
|
+
* @param address - Wallet address to validate
|
|
152
|
+
* @param fieldName - Field name for error messages (default: 'address')
|
|
153
|
+
* @throws {ValidationError} If address is invalid
|
|
154
|
+
*
|
|
155
|
+
* @example
|
|
156
|
+
* ```typescript
|
|
157
|
+
* validateAddress('eth|0x1234567890abcdef1234567890abcdef12345678'); // ✅ Valid
|
|
158
|
+
* validateAddress('0x1234567890abcdef1234567890abcdef12345678'); // ✅ Valid
|
|
159
|
+
* validateAddress('invalid'); // ❌ Throws: invalid format
|
|
160
|
+
* ```
|
|
161
|
+
*/
|
|
162
|
+
export function validateAddress(address: string, fieldName = 'address'): void {
|
|
163
|
+
if (!address || typeof address !== 'string') {
|
|
164
|
+
throw new ValidationError(fieldName, 'Address is required');
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// GalaChain format: eth|0x{40 hex chars}
|
|
168
|
+
const galachainRegex = /^eth\|0x[a-fA-F0-9]{40}$/;
|
|
169
|
+
// Standard Ethereum format: 0x{40 hex chars}
|
|
170
|
+
const ethereumRegex = /^0x[a-fA-F0-9]{40}$/;
|
|
171
|
+
|
|
172
|
+
if (!galachainRegex.test(address) && !ethereumRegex.test(address)) {
|
|
173
|
+
throw new ValidationError(
|
|
174
|
+
fieldName,
|
|
175
|
+
'Address must be in GalaChain format (eth|0x...) or Ethereum format (0x...)'
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Validate token symbol format
|
|
182
|
+
*
|
|
183
|
+
* Symbols must be 1-8 uppercase letters.
|
|
184
|
+
*
|
|
185
|
+
* @param symbol - Token symbol to validate
|
|
186
|
+
* @param fieldName - Field name for error messages (default: 'symbol')
|
|
187
|
+
* @throws {ValidationError} If symbol is invalid
|
|
188
|
+
*
|
|
189
|
+
* @example
|
|
190
|
+
* ```typescript
|
|
191
|
+
* validateTokenSymbol('GALA'); // ✅ Valid
|
|
192
|
+
* validateTokenSymbol('TEST'); // ✅ Valid
|
|
193
|
+
* validateTokenSymbol('test'); // ❌ Throws: must be uppercase
|
|
194
|
+
* validateTokenSymbol('TOOLONG123'); // ❌ Throws: too long
|
|
195
|
+
* ```
|
|
196
|
+
*/
|
|
197
|
+
export function validateTokenSymbol(symbol: string, fieldName = 'symbol'): void {
|
|
198
|
+
if (!symbol || typeof symbol !== 'string') {
|
|
199
|
+
throw new ValidationError(fieldName, 'Symbol is required');
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
if (symbol.length < 1 || symbol.length > 8) {
|
|
203
|
+
throw new ValidationError(
|
|
204
|
+
fieldName,
|
|
205
|
+
`Symbol must be 1-8 characters (got ${symbol.length})`
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
const symbolRegex = /^[A-Z]+$/;
|
|
210
|
+
if (!symbolRegex.test(symbol)) {
|
|
211
|
+
throw new ValidationError(
|
|
212
|
+
fieldName,
|
|
213
|
+
'Symbol must be uppercase letters only (A-Z)'
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Validate pagination limit
|
|
220
|
+
*
|
|
221
|
+
* Limits must be positive integers within reasonable range (1-100).
|
|
222
|
+
*
|
|
223
|
+
* @param limit - Pagination limit to validate
|
|
224
|
+
* @param max - Maximum allowed limit (default: 100)
|
|
225
|
+
* @param fieldName - Field name for error messages (default: 'limit')
|
|
226
|
+
* @throws {ValidationError} If limit is invalid
|
|
227
|
+
*
|
|
228
|
+
* @example
|
|
229
|
+
* ```typescript
|
|
230
|
+
* validatePaginationLimit('20'); // ✅ Valid
|
|
231
|
+
* validatePaginationLimit('1'); // ✅ Valid
|
|
232
|
+
* validatePaginationLimit('0'); // ❌ Throws: too low
|
|
233
|
+
* validatePaginationLimit('150'); // ❌ Throws: too high
|
|
234
|
+
* ```
|
|
235
|
+
*/
|
|
236
|
+
export function validatePaginationLimit(
|
|
237
|
+
limit: string | number,
|
|
238
|
+
max = 100,
|
|
239
|
+
fieldName = 'limit'
|
|
240
|
+
): void {
|
|
241
|
+
if (limit === null || limit === undefined || limit === '') {
|
|
242
|
+
throw new ValidationError(fieldName, 'Limit is required');
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const numLimit = typeof limit === 'string' ? parseInt(limit, 10) : limit;
|
|
246
|
+
|
|
247
|
+
if (isNaN(numLimit) || !Number.isInteger(numLimit)) {
|
|
248
|
+
throw new ValidationError(
|
|
249
|
+
fieldName,
|
|
250
|
+
`Limit must be a valid integer (got: ${limit})`
|
|
251
|
+
);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
if (numLimit < 1 || numLimit > max) {
|
|
255
|
+
throw new ValidationError(
|
|
256
|
+
fieldName,
|
|
257
|
+
`Limit must be between 1 and ${max} (got: ${numLimit})`
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Validate comma-separated token list
|
|
264
|
+
*
|
|
265
|
+
* For batch operations that accept multiple token names.
|
|
266
|
+
*
|
|
267
|
+
* @param tokens - Comma-separated token names
|
|
268
|
+
* @param fieldName - Field name for error messages (default: 'tokens')
|
|
269
|
+
* @throws {ValidationError} If token list is invalid
|
|
270
|
+
*
|
|
271
|
+
* @example
|
|
272
|
+
* ```typescript
|
|
273
|
+
* validateTokenList('anime,test,dragon'); // ✅ Valid
|
|
274
|
+
* validateTokenList('token1'); // ✅ Valid (single token)
|
|
275
|
+
* validateTokenList(''); // ❌ Throws: empty list
|
|
276
|
+
* validateTokenList('token1,invalid@#$'); // ❌ Throws: invalid token name
|
|
277
|
+
* ```
|
|
278
|
+
*/
|
|
279
|
+
export function validateTokenList(tokens: string, fieldName = 'tokens'): string[] {
|
|
280
|
+
if (!tokens || typeof tokens !== 'string') {
|
|
281
|
+
throw new ValidationError(fieldName, 'Token list is required');
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
const tokenArray = tokens.split(',').map((t) => t.trim());
|
|
285
|
+
|
|
286
|
+
if (tokenArray.length === 0 || tokenArray.some((t) => !t)) {
|
|
287
|
+
throw new ValidationError(fieldName, 'Token list cannot be empty');
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
// Validate each token name
|
|
291
|
+
tokenArray.forEach((token, index) => {
|
|
292
|
+
try {
|
|
293
|
+
validateTokenName(token, `${fieldName}[${index}]`);
|
|
294
|
+
} catch (error) {
|
|
295
|
+
if (error instanceof ValidationError) {
|
|
296
|
+
throw new ValidationError(
|
|
297
|
+
fieldName,
|
|
298
|
+
`Invalid token at position ${index + 1}: ${token}`
|
|
299
|
+
);
|
|
300
|
+
}
|
|
301
|
+
throw error;
|
|
302
|
+
}
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
return tokenArray;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Safe validation wrapper that doesn't throw
|
|
310
|
+
*
|
|
311
|
+
* Useful for non-critical validation where you want to continue with a warning.
|
|
312
|
+
*
|
|
313
|
+
* @param validationFn - Validation function to execute
|
|
314
|
+
* @returns Object with success flag and optional error
|
|
315
|
+
*
|
|
316
|
+
* @example
|
|
317
|
+
* ```typescript
|
|
318
|
+
* const result = safeValidate(() => validateTokenName('anime'));
|
|
319
|
+
* if (!result.success) {
|
|
320
|
+
* console.warn('Validation warning:', result.error);
|
|
321
|
+
* }
|
|
322
|
+
* ```
|
|
323
|
+
*/
|
|
324
|
+
export function safeValidate(
|
|
325
|
+
validationFn: () => void
|
|
326
|
+
): { success: boolean; error?: string } {
|
|
327
|
+
try {
|
|
328
|
+
validationFn();
|
|
329
|
+
return { success: true };
|
|
330
|
+
} catch (error) {
|
|
331
|
+
if (error instanceof ValidationError) {
|
|
332
|
+
return { success: false, error: error.message };
|
|
333
|
+
}
|
|
334
|
+
return { success: false, error: String(error) };
|
|
335
|
+
}
|
|
336
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "CommonJS",
|
|
5
|
+
"moduleResolution": "node",
|
|
6
|
+
"lib": ["ES2022"],
|
|
7
|
+
"outDir": "./dist",
|
|
8
|
+
"rootDir": "./src",
|
|
9
|
+
"declaration": true,
|
|
10
|
+
"declarationMap": true,
|
|
11
|
+
"sourceMap": true,
|
|
12
|
+
"strict": true,
|
|
13
|
+
"esModuleInterop": true,
|
|
14
|
+
"skipLibCheck": true,
|
|
15
|
+
"noEmit": false,
|
|
16
|
+
"forceConsistentCasingInFileNames": true,
|
|
17
|
+
"resolveJsonModule": true,
|
|
18
|
+
"allowSyntheticDefaultImports": true,
|
|
19
|
+
"types": ["node", "jest"]
|
|
20
|
+
},
|
|
21
|
+
"include": ["src/**/*"],
|
|
22
|
+
"exclude": ["node_modules", "dist", "tests", "src/scripts", "src/**/__tests__"]
|
|
23
|
+
}
|