-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
329 lines (292 loc) Β· 8.84 KB
/
server.js
File metadata and controls
329 lines (292 loc) Β· 8.84 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
327
328
329
const express = require('express');
const fs = require('fs');
const cors = require('cors');
const path = require('path');
const crypto = require('crypto');
const app = express();
const PORT = process.env.PORT || 3000;
app.use(cors());
app.use(express.json());
// Simple in-memory storage (would use DB in production)
const apiKeys = new Map();
const requestCounts = new Map();
const donations = [];
// Load existing data
const dataFile = path.join(__dirname, 'data.json');
let storedData = { apiKeys: {}, donations: [] };
try {
storedData = JSON.parse(fs.readFileSync(dataFile, 'utf8'));
Object.entries(storedData.apiKeys || {}).forEach(([key, value]) => {
apiKeys.set(key, value);
});
donations.push(...(storedData.donations || []));
} catch (e) {
console.log('No existing data file, starting fresh');
}
// Save data periodically
function saveData() {
const data = {
apiKeys: Object.fromEntries(apiKeys),
donations
};
fs.writeFileSync(dataFile, JSON.stringify(data, null, 2));
}
// Request logging
const logFile = path.join(__dirname, 'requests.log');
app.use((req, res, next) => {
const log = `${new Date().toISOString()} ${req.method} ${req.path} ${req.ip}
`;
fs.appendFileSync(logFile, log);
next();
});
// Rate limiting middleware
function rateLimit(req, res, next) {
const apiKey = req.headers['x-api-key'];
const ip = req.ip;
const key = apiKey || ip;
const now = Date.now();
const windowMs = 3600000; // 1 hour
if (!requestCounts.has(key)) {
requestCounts.set(key, { count: 0, resetAt: now + windowMs });
}
const record = requestCounts.get(key);
if (now > record.resetAt) {
record.count = 0;
record.resetAt = now + windowMs;
}
// Check limits
const userInfo = apiKey ? apiKeys.get(apiKey) : null;
const limit = userInfo?.tier === 'premium' ? 10000 : 100;
if (record.count >= limit) {
return res.status(429).json({
error: 'Rate limit exceeded',
retryAfter: Math.ceil((record.resetAt - now) / 1000),
upgrade: 'Get premium at /api/premium for 10,000 requests/hour'
});
}
record.count++;
req.rateLimit = { remaining: limit - record.count, limit, resetAt: record.resetAt };
next();
}
app.use(rateLimit);
// Serve static files
app.use(express.static(path.join(__dirname)));
// Airdrop data
const airdropData = {
lastUpdated: new Date().toISOString(),
protocols: [
{
name: "Seamless Protocol",
category: "Lending",
tvl: 100000000,
hasToken: false,
priority: "HIGH",
action: "Deposit USDC to earn yield + airdrop points",
url: "https://app.seamlessprotocol.com",
chain: "Base"
},
{
name: "Extra Finance",
category: "Yield Farming",
tvl: 9000000,
hasToken: false,
priority: "HIGH",
action: "Provide liquidity or farm",
url: "https://app.extrafi.io",
chain: "Base"
},
{
name: "Avantis",
category: "Perpetuals",
tvl: 91000000,
hasToken: false,
priority: "MEDIUM",
action: "Trade perps to show activity",
url: "https://www.avantisfi.com",
chain: "Base"
},
{
name: "Aerodrome",
category: "DEX",
tvl: 213000000,
hasToken: true,
tokenSymbol: "AERO",
priority: "LOW",
action: "Trade/LP for potential future airdrops",
url: "https://aerodrome.finance",
chain: "Base"
}
]
};
// Health check
app.get('/health', (req, res) => {
res.json({
status: 'ok',
uptime: process.uptime(),
timestamp: new Date().toISOString(),
version: '2.0.0-monetized'
});
});
// Generate API key
app.post('/api/key', (req, res) => {
const { email, tier = 'free' } = req.body;
if (!email) {
return res.status(400).json({ error: 'Email required' });
}
const apiKey = 'xmoney_' + crypto.randomBytes(16).toString('hex');
const keyData = {
email,
tier,
createdAt: new Date().toISOString(),
requests: 0
};
apiKeys.set(apiKey, keyData);
saveData();
res.json({
apiKey,
tier,
limits: tier === 'premium' ? '10,000 requests/hour' : '100 requests/hour',
message: 'Include this key in X-API-Key header'
});
});
// Premium upgrade info
app.get('/api/premium', (req, res) => {
res.json({
tier: 'premium',
price: '0.01 USDC per day or 0.25 USDC per month',
benefits: [
'10,000 requests/hour (vs 100 free)',
'Priority support',
'Early access to new protocols',
'Custom webhook alerts',
'Historical data access'
],
payment: {
method: 'Send USDC on Base to: 0x6d15eE39fB46Eb439d7B19ACed5d36A4A327eAa2',
note: 'Include your API key in transaction memo or email proof to upgrade'
},
donate: {
message: 'Support free tier by donating any amount',
wallet: '0x6d15eE39fB46Eb439d7B19ACed5d36A4A327eAa2',
chain: 'Base'
}
});
});
// Record donation
app.post('/api/donate', (req, res) => {
const { txHash, amount, apiKey } = req.body;
if (!txHash || !amount) {
return res.status(400).json({ error: 'txHash and amount required' });
}
const donation = {
txHash,
amount: parseFloat(amount),
apiKey: apiKey || 'anonymous',
timestamp: new Date().toISOString()
};
donations.push(donation);
saveData();
res.json({
success: true,
message: 'Thank you for your donation!',
totalDonations: donations.reduce((sum, d) => sum + d.amount, 0)
});
});
// Get all airdrop opportunities
app.get('/api/airdrops', (req, res) => {
res.set('X-RateLimit-Remaining', req.rateLimit.remaining);
res.set('X-RateLimit-Limit', req.rateLimit.limit);
res.json(airdropData);
});
// Get no-token protocols only
app.get('/api/airdrops/no-token', (req, res) => {
res.set('X-RateLimit-Remaining', req.rateLimit.remaining);
const noToken = airdropData.protocols.filter(p => !p.hasToken);
res.json({
lastUpdated: airdropData.lastUpdated,
count: noToken.length,
protocols: noToken
});
});
// Get high priority only
app.get('/api/airdrops/high-priority', (req, res) => {
res.set('X-RateLimit-Remaining', req.rateLimit.remaining);
const high = airdropData.protocols.filter(p => p.priority === 'HIGH');
res.json({ count: high.length, protocols: high });
});
// Get single protocol
app.get('/api/airdrops/:name', (req, res) => {
res.set('X-RateLimit-Remaining', req.rateLimit.remaining);
const protocol = airdropData.protocols.find(
p => p.name.toLowerCase().replace(/\s+/g, '-') === req.params.name.toLowerCase().replace(/\s+/g, '-')
);
if (!protocol) {
return res.status(404).json({ error: 'Protocol not found' });
}
res.json(protocol);
});
// Check wallet eligibility
app.get('/api/check/:address', (req, res) => {
res.set('X-RateLimit-Remaining', req.rateLimit.remaining);
const { address } = req.params;
if (!address || !address.startsWith('0x')) {
return res.status(400).json({ error: 'Invalid address' });
}
res.json({
address,
eligible: true,
protocols: airdropData.protocols.filter(p => !p.hasToken).map(p => ({
name: p.name,
priority: p.priority,
action: p.action,
url: p.url
})),
disclaimer: "This is simulated data. Check on-chain for real eligibility."
});
});
// API info
app.get('/api', (req, res) => {
res.json({
name: "Base Airdrop API",
version: "2.0.0-monetized",
description: "Free & Premium API for Base network airdrop opportunities",
endpoints: {
"POST /api/key": "Generate API key (free)",
"GET /api/premium": "Premium tier info",
"POST /api/donate": "Record a donation",
"GET /api/airdrops": "Get all airdrop opportunities",
"GET /api/airdrops/no-token": "Get protocols without tokens (best candidates)",
"GET /api/airdrops/high-priority": "Get high priority opportunities only",
"GET /api/airdrops/:name": "Get specific protocol info",
"GET /api/check/:address": "Check wallet eligibility (simulated)",
"GET /health": "Health check"
},
rateLimit: {
free: "100 requests/hour",
premium: "10,000 requests/hour"
},
monetization: {
premium: "0.01 USDC/day or 0.25 USDC/month",
donations: "Any amount appreciated"
},
contact: "X-Money Autonomous Agent",
wallet: "0x6d15eE39fB46Eb439d7B19ACed5d36A4A327eAa2",
stats: {
totalKeys: apiKeys.size,
totalDonations: donations.reduce((sum, d) => sum + d.amount, 0),
donationCount: donations.length
},
landing: "/"
});
});
// Serve landing page
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'landing.html'));
});
app.listen(PORT, () => {
console.log(`π Base Airdrop API v2.0 (Monetized) running on port ${PORT}`);
console.log(`π API docs: http://localhost:${PORT}/api`);
console.log(`π° Premium info: http://localhost:${PORT}/api/premium`);
console.log(`π Landing page: http://localhost:${PORT}/`);
console.log(`π Active API keys: ${apiKeys.size}`);
});