-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
464 lines (396 loc) · 18.1 KB
/
Copy pathserver.js
File metadata and controls
464 lines (396 loc) · 18.1 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
const express = require('express');
const puppeteer = require('puppeteer-extra');
const path = require('path');
const fs = require('fs');
const cors = require('cors');
const axios = require('axios');
const cron = require('node-cron');
const FormData = require('form-data');
const StealthPlugin = require('puppeteer-extra-plugin-stealth');
// Use Puppeteer Stealth plugin
puppeteer.use(StealthPlugin());
const authToken = ''; // constant auth token
const app = express();
// Replace with the path to your Phantom Wallet extension
const phantomExtensionPath = '';
const walletName = 'MyWallet';
const privateKey = '';
const walletPassword = 'password';
const allowedOrigins = [
'http://pump.fun//create',
'chrome-extension://emcjgmgdfakfaddjloelmaloiohdddpe', // Add your Chrome extension ID here (find it in details under chrome extensions)
'http://pump.fun/api/ipfs'
];
app.use(cors({
origin: function (origin, callback) {
if (!origin) return callback(null, true);
if (allowedOrigins.indexOf(origin) === -1) {
const msg = 'The CORS policy for this site does not allow access from the specified Origin.';
return callback(new Error(msg), false);
}
return callback(null, true);
},
methods: ['GET', 'POST', 'PUT', 'DELETE'],
allowedHeaders: ['Content-Type', 'Authorization']
}));
app.use(express.json({ limit: '50mb' }));
async function createCoin(tweetImageData, ticker, name, description, devBuyPrompt) {
const imagePath = path.join(__dirname, 'tweet.png');
fs.writeFileSync(imagePath, Buffer.from(tweetImageData, 'base64'));
let notificationPage;
try {
const browser = await puppeteer.launch({
headless: false,
args: [
`--disable-extensions-except=${phantomExtensionPath}`,
`--load-extension=${phantomExtensionPath}`
],
});
// Add delay to ensure all tabs are fully loaded
await new Promise(resolve => setTimeout(resolve, 1000));
const pages = await browser.pages();
const phantomPage = pages[1];
// Interact with the Phantom Wallet tab
await phantomPage.bringToFront();
try {
await phantomPage.goto('chrome-extension://bfnaelmomeimhlpmgjnjophhpkkoljpa/onboarding.html', { waitUntil: 'networkidle2', timeout: 60000 });
console.log('Navigated to Phantom Wallet onboarding');
} catch (error) {
console.error('Error navigating to Phantom Wallet onboarding:', error);
throw error;
}
// Click "Import an existing wallet"
try {
await phantomPage.waitForSelector('button', { timeout: 60000 });
const buttons = await phantomPage.$$('button');
await buttons[1].click(); // Assuming the second button is "Import an existing wallet"
console.log('Clicked "Import an existing wallet"');
} catch (error) {
console.error('Error clicking "Import an existing wallet":', error);
throw error;
}
// Click "Import Private Key"
try {
await phantomPage.waitForSelector('div[data-testid="import-private-key-button"]', { timeout: 60000 });
await phantomPage.click('div[data-testid="import-private-key-button"]');
console.log('Clicked "Import Private Key"');
} catch (error) {
console.error('Error clicking "Import Private Key":', error);
throw error;
}
// Fill out the import form
try {
await phantomPage.waitForSelector('input[name="name"]', { timeout: 60000 });
await phantomPage.type('input[name="name"]', walletName);
await phantomPage.type('textarea', privateKey); // Use class selector for textarea
console.log('Filled out wallet name and private key');
} catch (error) {
console.error('Error filling out wallet form:', error);
throw error;
}
// Click "Import"
try {
await phantomPage.waitForSelector('button[data-testid="onboarding-form-submit-button"]', { timeout: 60000 });
await phantomPage.click('button[data-testid="onboarding-form-submit-button"]');
console.log('Clicked "Import"');
} catch (error) {
console.error('Error clicking "Import":', error);
throw error;
}
// Fill out the password form
try {
await phantomPage.waitForSelector('input[data-testid="onboarding-form-password-input"]', { timeout: 60000 });
await phantomPage.type('input[data-testid="onboarding-form-password-input"]', walletPassword);
await phantomPage.type('input[data-testid="onboarding-form-confirm-password-input"]', walletPassword);
await phantomPage.click('input[type="checkbox"]');
console.log('Filled out password and accepted terms');
} catch (error) {
console.error('Error filling out password form:', error);
throw error;
}
// Click "Continue"
try {
await phantomPage.waitForSelector('button[data-testid="onboarding-form-submit-button"]', { timeout: 60000 });
await phantomPage.click('button[data-testid="onboarding-form-submit-button"]');
console.log('Clicked "Continue"');
} catch (error) {
console.error('Error clicking "Continue":', error);
throw error;
}
// Open the pump.fun tab explicitly
const pumpPage = await browser.newPage();
await pumpPage.goto('http://pump.fun/create', { waitUntil: 'networkidle2' });
// Handle the initial popup
try {
await pumpPage.waitForSelector('button', { text: "I'm ready to pump", timeout: 60000 });
await pumpPage.click('button', { text: "I'm ready to pump" });
console.log('Clicked the "I\'m ready to pump" button');
} catch (error) {
console.error('Error clicking the "I\'m ready to pump" button:', error);
throw error;
}
// Wait for the necessary input fields to be available
try {
await pumpPage.waitForSelector('#name', { timeout: 60000 });
await pumpPage.waitForSelector('#ticker', { timeout: 60000 });
await pumpPage.waitForSelector('#text', { timeout: 60000 });
await pumpPage.waitForSelector('input[type="file"]', { timeout: 60000 });
} catch (error) {
console.error('Error waiting for input fields:', error);
throw error;
}
// Fill out the form
try {
await pumpPage.type('#name', name);
await pumpPage.type('#ticker', ticker);
await pumpPage.type('#text', description);
const input = await pumpPage.$('input[type="file"]');
await input.uploadFile(imagePath);
console.log('Uploaded tweet image');
} catch (error) {
console.error('Error filling out form:', error);
throw error;
}
// Click "Connect Wallet"
try {
await pumpPage.waitForSelector('button.text-sm.text-slate-50', { timeout: 60000 });
await pumpPage.evaluate(() => {
const buttons = [...document.querySelectorAll('button.text-sm.text-slate-50')];
buttons.find(btn => btn.textContent.includes('[connect wallet]')).click();
});
console.log('Clicked "Connect Wallet"');
} catch (error) {
console.error('Error clicking "Connect Wallet":', error);
throw error;
}
// Click "Phantom"
try {
await pumpPage.waitForSelector('button', { text: 'Phantom' });
await pumpPage.evaluate(() => {
const buttons = [...document.querySelectorAll('button')];
buttons.find(btn => btn.textContent.includes('Phantom')).click();
});
console.log('Clicked "Phantom"');
} catch (error) {
console.error('Error clicking "Phantom":', error);
throw error;
}
// Handle Phantom Wallet connection and confirmation
let notificationPage;
// Click "Connect" in Phantom Wallet notification
try {
notificationPage = await browser.waitForTarget(target => target.url().includes('notification.html')).then(target => target.page());
await notificationPage.bringToFront();
await notificationPage.waitForSelector('button[data-testid="primary-button"]', { timeout: 60000 });
await notificationPage.click('button[data-testid="primary-button"]');
console.log('Clicked "Connect" in Phantom Wallet');
} catch (error) {
console.error('Error clicking "Connect" in Phantom Wallet:', error);
throw error;
}
// Wait for new notification page and click "Confirm"
try {
// Wait for the new notification page to appear
await new Promise(resolve => setTimeout(resolve, 1000));
notificationPage = await browser.waitForTarget(target => target.url().includes('notification.html')).then(target => target.page());
await notificationPage.bringToFront();
await notificationPage.waitForSelector('button[data-testid="primary-button"]', { timeout: 60000 });
await notificationPage.click('button[data-testid="primary-button"]');
console.log('Clicked "Confirm" in Phantom Wallet');
} catch (error) {
console.error('Error clicking "Confirm":', error);
throw error;
}
// Switch focus back to pump.fun page
await pumpPage.bringToFront();
const closeSelector = 'div.text-slate-50.hover\\:font-bold.hover\\:text-slate-50.cursor-pointer.w-fit.justify-self-center';
// Click the 1st "Close" button back on pump.fun
try {
await pumpPage.waitForFunction(
(selector) => {
const elements = Array.from(document.querySelectorAll(selector));
return elements.some(element => element.textContent.trim() === '[close]');
},
{ timeout: 60000 },
closeSelector
);
const closeButtonElements = await pumpPage.evaluate((selector) => {
return Array.from(document.querySelectorAll(selector))
.filter(element => element.textContent.trim() === '[close]')
.map(element => element.outerHTML); // Return outerHTML for debugging
}, closeSelector);
console.log('Close button elements found:', closeButtonElements);
if (closeButtonElements.length > 0) {
await pumpPage.evaluate((selector) => {
const element = Array.from(document.querySelectorAll(selector))
.find(element => element.textContent.trim() === '[close]');
if (element) element.click();
}, closeSelector);
console.log('Clicked 1st "Close" on pump.fun');
}
} catch (error) {
console.error('Error clicking first "Close" on pump.fun:', error);
throw error;
}
// Click the 2nd "Close" button back on pump.fun
try {
await pumpPage.waitForFunction(
(selector) => {
const elements = Array.from(document.querySelectorAll(selector));
return elements.some(element => element.textContent.trim() === '[close]');
},
{ timeout: 60000 },
closeSelector
);
const closeButtonElements = await pumpPage.evaluate((selector) => {
return Array.from(document.querySelectorAll(selector))
.filter(element => element.textContent.trim() === '[close]')
.map(element => element.outerHTML); // Return outerHTML for debugging
}, closeSelector);
console.log('Close button elements found:', closeButtonElements);
if (closeButtonElements.length > 1) {
await pumpPage.evaluate((selector) => {
const elements = Array.from(document.querySelectorAll(selector))
.filter(element => element.textContent.trim() === '[close]');
if (elements.length > 1) elements[1].click();
}, closeSelector);
console.log('Clicked 2nd "Close" on pump.fun');
}
} catch (error) {
console.error('Error clicking second "Close" on pump.fun:', error);
throw error;
}
const rejectAllSelector = '#btn-reject-all';
// Click the "Reject All" button for cookie settings
try {
await pumpPage.waitForSelector(rejectAllSelector, { timeout: 60000 });
await pumpPage.evaluate((selector) => {
const button = document.querySelector(selector);
if (button) {
button.dispatchEvent(new MouseEvent('click', { bubbles: true, cancelable: true, view: window }));
}
}, rejectAllSelector);
console.log('Clicked "Reject All" button for cookie settings');
} catch (error) {
console.error('Error clicking "Reject All" button for cookie settings:', error);
throw error;
}
// Click the "Create coin" button
try {
await pumpPage.waitForSelector('button', { timeout: 60000 });
await pumpPage.evaluate(() => {
const buttons = Array.from(document.querySelectorAll('button'));
buttons.find(btn => btn.textContent.trim() === 'Create coin').click();
});
console.log('Clicked the "Create coin" button');
} catch (error) {
console.error('Error clicking the "Create coin" button:', error);
throw error;
}
// Fill out the "devBuy" input field
try {
await pumpPage.waitForSelector('#amount', { timeout: 60000 });
// Extract numeric value from devBuyPrompt
const devBuyNumber = devBuyPrompt.match(/\d+(\.\d+)?/)[0];
console.log(devBuyNumber);
console.log(devBuyPrompt);
await pumpPage.type('#amount', devBuyNumber);
console.log('Filled out the devBuy input field');
} catch (error) {
console.error('Error filling out the devBuy input field:', error);
throw error;
}
// Click the "Create coin" button
try {
// Added a delay to ensure the correct button is rendered
await new Promise(resolve => setTimeout(resolve, 500)); // Adjust delay as needed
await pumpPage.waitForSelector('button', { timeout: 60000 });
const clicked = await pumpPage.evaluate(() => {
const buttons = Array.from(document.querySelectorAll('button'));
const createCoinButtons = buttons.filter(btn => btn.textContent.trim() === 'Create coin');
if (createCoinButtons.length > 1) {
// Log the found buttons for debugging
console.log('Found "Create coin" buttons:', createCoinButtons);
// Assuming the correct button is the second one
const createCoinButton = createCoinButtons[1];
createCoinButton.dispatchEvent(new MouseEvent('click', { bubbles: true, cancelable: true, view: window }));
return true;
} else if (createCoinButtons.length === 1) {
const createCoinButton = createCoinButtons[0];
createCoinButton.dispatchEvent(new MouseEvent('click', { bubbles: true, cancelable: true, view: window }));
return true;
} else {
return false;
}
});
if (clicked) {
console.log('Clicked the "Create coin" button');
} else {
console.error('Could not find the "Create coin" button');
throw new Error('Could not find the "Create coin" button');
}
} catch (error) {
console.error('Error clicking the "Create coin" button:', error);
throw error;
}
// Wait for new notification page and click "Confirm"
try {
// Wait for the new notification page to appear
await new Promise(resolve => setTimeout(resolve, 300));
notificationPage = await browser.waitForTarget(target => target.url().includes('notification.html')).then(target => target.page());
await notificationPage.bringToFront();
await notificationPage.waitForSelector('button[data-testid="primary-button"]', { timeout: 60000 });
await notificationPage.click('button[data-testid="primary-button"]');
console.log('Clicked "Confirm" in Phantom Wallet');
} catch (error) {
console.error('Error clicking "Confirm":', error);
throw error;
}
// Send the request to the API with the auth token
const formData = new FormData();
formData.append('tweetImageData', tweetImageData);
formData.append('ticker', ticker);
formData.append('name', name);
formData.append('description', description);
formData.append('devBuyPrompt', devBuyPrompt);
try {
const response = await axios.post('https://pump.fun/api/ipfs', formData, {
headers: {
'Authorization': `Bearer ${authToken}`,
...formData.getHeaders(),
}
});
} catch (error) {
throw error;
}
console.log('Coin created successfully!');
} catch (error) {
console.error('Error creating coin:', error);
} finally {
fs.unlinkSync(imagePath);
}
}
// Define a POST route
app.post('/api/ipfs', (req, res) => {
// Your route logic here
res.send('IPFS endpoint');
});
app.post('/create', (req, res) => {
let { tweetImageData, ticker, name, description, devBuyPrompt } = req.body;
// Remove the data URL scheme part if it exists
if (tweetImageData.startsWith('data:image/png;base64,')) {
tweetImageData = tweetImageData.replace('data:image/png;base64,', '');
}
createCoin(tweetImageData, ticker, name, description, devBuyPrompt)
.then(() => res.json({ message: 'Coin created successfully!' }))
.catch((error) => {
console.error('Error during coin creation:', error); // Improved logging
res.status(500).json({ message: 'Error creating coin', error: error.message });
});
});
// Define the port
const port = 3000;
// Start the server on the defined port
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});