-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
326 lines (298 loc) · 9.38 KB
/
index.js
File metadata and controls
326 lines (298 loc) · 9.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// SMILE Backend - Testnet Discovery & API
const express = require('express');
const cors = require('cors');
const app = express();
// Middleware
app.use(cors());
app.use(express.json({ limit: '10mb' }));
// Health check
app.get('/', (req, res) => {
res.json({
status: 'online',
name: 'SMILE Backend',
version: '2.0.0',
timestamp: new Date().toISOString()
});
});
// Mock testnet data (in production, scrape from real sources)
const TESTNETS = [
{
id: 'scroll-sepolia',
name: 'Scroll Sepolia',
chain: 'scroll',
score: 95,
automation: 40,
profitability: 28,
timing: 18,
popularity: 9,
tasks: [
{ type: 'claim_faucet', description: 'Claim testnet ETH', url: 'https://scroll.io/faucet' },
{ type: 'swap_tokens', description: 'Swap 0.1 ETH to USDC', amountMin: 0.08, amountMax: 0.12 },
{ type: 'bridge', description: 'Bridge to Ethereum', amountMin: 0.05, amountMax: 0.1, fromChain: 'scroll', toChain: 'ethereum' }
],
status: 'active',
deadline: Date.now() + 30 * 24 * 60 * 60 * 1000,
cost: 0,
requirements: []
},
{
id: 'linea-goerli',
name: 'Linea Goerli',
chain: 'linea',
score: 92,
automation: 38,
profitability: 26,
timing: 17,
popularity: 11,
tasks: [
{ type: 'claim_faucet', description: 'Claim testnet tokens' },
{ type: 'swap_tokens', description: 'Swap tokens', amountMin: 0.05, amountMax: 0.15 },
{ type: 'mint_nft', description: 'Mint test NFT', minCount: 1, maxCount: 3 }
],
status: 'active',
deadline: Date.now() + 45 * 24 * 60 * 60 * 1000,
cost: 0,
requirements: []
},
{
id: 'base-sepolia',
name: 'Base Sepolia',
chain: 'base',
score: 90,
automation: 36,
profitability: 25,
timing: 19,
popularity: 10,
tasks: [
{ type: 'claim_faucet', description: 'Claim Base ETH' },
{ type: 'swap_tokens', description: 'Swap on Uniswap', amountMin: 0.01, amountMax: 0.05 }
],
status: 'active',
deadline: Date.now() + 60 * 24 * 60 * 60 * 1000,
cost: 0,
requirements: []
},
{
id: 'optimism-sepolia',
name: 'Optimism Sepolia',
chain: 'optimism',
score: 88,
automation: 35,
profitability: 24,
timing: 18,
popularity: 11,
tasks: [
{ type: 'claim_faucet', description: 'Claim OP tokens' },
{ type: 'provide_liquidity', description: 'Add liquidity', amountMin: 0.1, amountMax: 0.3 }
],
status: 'active',
deadline: Date.now() + 50 * 24 * 60 * 60 * 1000,
cost: 0,
requirements: []
},
{
id: 'arbitrum-sepolia',
name: 'Arbitrum Sepolia',
chain: 'arbitrum',
score: 85,
automation: 34,
profitability: 23,
timing: 17,
popularity: 11,
tasks: [
{ type: 'claim_faucet', description: 'Claim ARB tokens' },
{ type: 'stake', description: 'Stake tokens', amountMin: 0.1, amountMax: 0.5 }
],
status: 'active',
deadline: Date.now() + 40 * 24 * 60 * 60 * 1000,
cost: 0,
requirements: []
}
];
// Get all testnets
app.get('/api/testnets', (req, res) => {
try {
const activeTestnets = TESTNETS.filter(t => t.cost === 0 && t.status === 'active');
activeTestnets.sort((a, b) => b.score - a.score);
res.json(activeTestnets);
} catch (error) {
res.status(500).json({ error: 'Failed to fetch testnets', message: error.message });
}
});
// Get new testnets (excluding completed ones)
app.get('/api/testnets/new', (req, res) => {
try {
const completed = req.query.completed || [];
const completedIds = Array.isArray(completed) ? completed : [completed];
const newTestnets = TESTNETS.filter(t =>
!completedIds.includes(t.id) &&
t.cost === 0 &&
t.status === 'active'
);
newTestnets.sort((a, b) => b.score - a.score);
res.json(newTestnets);
} catch (error) {
res.status(500).json({ error: 'Failed to fetch new testnets', message: error.message });
}
});
// Check eligibility for claims
app.post('/api/check-eligibility', (req, res) => {
try {
const { addresses } = req.body;
if (!addresses || !Array.isArray(addresses)) {
return res.status(400).json({ error: 'Addresses array is required' });
}
// Simulate eligibility check (30% of wallets are eligible)
const eligible = addresses
.filter(() => Math.random() < 0.3)
.map(addr => ({
address: addr,
testnet: 'scroll-sepolia',
eligible: true,
amount: (Math.random() * 1000 + 100).toFixed(2),
claimable: true,
claimUrl: 'https://claim.scroll.io'
}));
res.json(eligible);
} catch (error) {
res.status(500).json({ error: 'Failed to check eligibility', message: error.message });
}
});
// Parse custom task using simple NLP
app.post('/api/parse-task', (req, res) => {
try {
const { text } = req.body;
if (!text) {
return res.status(400).json({ error: 'Text is required' });
}
const steps = [];
const lower = text.toLowerCase();
const lines = text.split('\n').filter(l => l.trim());
// Parse each line
for (const line of lines) {
const lineLower = line.toLowerCase();
if (lineLower.includes('claim') || lineLower.includes('faucet')) {
steps.push({
action: 'claim_faucet',
description: line.trim(),
canAutomate: true,
reason: 'Can claim automatically'
});
}
if (lineLower.includes('swap') || lineLower.includes('exchange')) {
const amounts = extractAmounts(line);
steps.push({
action: 'swap_tokens',
description: line.trim(),
amountMin: amounts.min,
amountMax: amounts.max,
canAutomate: true,
reason: 'Can swap automatically'
});
}
if (lineLower.includes('bridge')) {
const amounts = extractAmounts(line);
steps.push({
action: 'bridge',
description: line.trim(),
amountMin: amounts.min,
amountMax: amounts.max,
canAutomate: true,
reason: 'Can bridge automatically'
});
}
if (lineLower.includes('mint') && lineLower.includes('nft')) {
steps.push({
action: 'mint_nft',
description: line.trim(),
minCount: 1,
maxCount: 3,
canAutomate: true,
reason: 'Can mint automatically'
});
}
if (lineLower.includes('stake')) {
const amounts = extractAmounts(line);
steps.push({
action: 'stake',
description: line.trim(),
amountMin: amounts.min,
amountMax: amounts.max,
canAutomate: true,
reason: 'Can stake automatically'
});
}
}
// If no steps found, try to parse entire text
if (steps.length === 0) {
if (lower.includes('swap')) {
const amounts = extractAmounts(text);
steps.push({
action: 'swap_tokens',
description: text.trim(),
amountMin: amounts.min,
amountMax: amounts.max,
canAutomate: true
});
}
}
const result = {
testnet: detectTestnet(text),
network: detectNetwork(text),
steps,
complexity: steps.length > 3 ? 'hard' : steps.length > 1 ? 'medium' : 'simple',
canAutomate: steps.every(s => s.canAutomate)
};
res.json(result);
} catch (error) {
res.status(500).json({ error: 'Failed to parse task', message: error.message });
}
});
// Helper functions
function extractAmounts(text) {
const matches = text.match(/(\d+\.?\d*)/g);
if (matches && matches.length >= 1) {
const amount = parseFloat(matches[0]);
return { min: amount * 0.9, max: amount * 1.1 };
}
return { min: 0.01, max: 0.1 };
}
function detectTestnet(text) {
const lower = text.toLowerCase();
if (lower.includes('scroll')) return 'Scroll Sepolia';
if (lower.includes('linea')) return 'Linea Goerli';
if (lower.includes('base')) return 'Base Sepolia';
if (lower.includes('optimism') || lower.includes('op ')) return 'Optimism Sepolia';
if (lower.includes('arbitrum') || lower.includes('arb')) return 'Arbitrum Sepolia';
return 'Custom Testnet';
}
function detectNetwork(text) {
const lower = text.toLowerCase();
if (lower.includes('scroll')) return 'scroll';
if (lower.includes('linea')) return 'linea';
if (lower.includes('base')) return 'base';
if (lower.includes('optimism')) return 'optimism';
if (lower.includes('arbitrum')) return 'arbitrum';
return 'ethereum';
}
// Start server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`
╔══════════════════════════════════════╗
║ SMILE BACKEND API ║
║ ║
║ Status: ONLINE ✓ ║
║ Port: ${PORT} ║
║ Version: 2.0.0 ║
║ ║
║ Endpoints: ║
║ GET /api/testnets ║
║ GET /api/testnets/new ║
║ POST /api/check-eligibility ║
║ POST /api/parse-task ║
║ ║
╚══════════════════════════════════════╝
`);
});
module.exports = app;