-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgemBot.js
More file actions
474 lines (456 loc) · 21.2 KB
/
gemBot.js
File metadata and controls
474 lines (456 loc) · 21.2 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
465
466
467
468
469
470
471
472
473
474
process.env["NTBA_FIX_319"] = 1;
var express = require('express');
var timeout = require('connect-timeout');
const axios = require('axios');
const fs = require('fs');
var answerCallbacks = {};
var port = process.env.PORT ? process.env.PORT : 4002;
// Turn the lights on
var app = express();
// Route for home
app.get("/", (req, res) => res.json("OK"));
// Load Telegram
var TelegramBot = require('node-telegram-bot-api');
// Tokenize
const token = process.env.SECRET;
// Establish bot
const bot = new TelegramBot(token, {polling: true});
// Establish time
var timeAgo = require('time-ago');
// Bot commands
var botCommands = {
start: "/start",
configure: "/configure",
compile: "/compile",
deploy: "/deploy"
}
if (botCommands) {
Object.assign(botCommands, botCommands);
}
// Bot helper text
var { exec } = require('child_process');
let description = "Hello Deployer, ";
let by = "Guardians of the Interchained";
let compileText = "Compiling Contracts";
let deployText = "Migrations Started";
var network = process.argv[3] ? process.argv[3] : '';
var action = process.argv[4] ? process.argv[4] : '';
var contractName = process.argv[5] ? process.argv[5] : '';
var contractAddress = process.argv[6] ? process.argv[6] : '';
var infura = process.argv[7] ? process.argv[7] : '';
var infuraURL = infura == 'ropsten' ? "wss://ropsten.infura.io/ws/v3/733a7efe57364ffd9210b582d7cd0cb3" : infura == 'mainnet' ? "wss://ropsten.infura.io/ws/v3/733a7efe57364ffd9210b582d7cd0cb3" : "wss://ropsten.infura.io/ws/v3/733a7efe57364ffd9210b582d7cd0cb3";
var networkURI = network == 'ropsten' ? "api-ropsten" : network == 'mainnet' ? 'api' : 'api';
var contractAddress = contractAddress ? contractAddress : '0x77124b96a355dab9562027103df8132c81650bef';
var etherscanApiKey = network == 'ropsten' ? 'API_KEY_ROPSTEN' : network == 'mainnet' ? 'API_KEY_MAINNET' : '45AQ1EIWA552SFI5KCM6W5IZHYJB1TEKQ3';
var contractsStorageDirectory = 'storage/';
var filePassword = 'lock&key';
var contractName = "";
async function importCA(msg,pastebin,contractName){
var contractsDirectory = 'contracts/';
var command = 'curl '+pastebin+' > '+contractsDirectory+contractName;
exec(command, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
let ttlerr = "Check details below and try again. "+'\n'+"There was an error reported from server:" + '\n' + '\n' + error;
return bot.sendMessage(msg.chat.id, ttlerr);
}
console.log(`stdout: ${stdout}`);
console.error(`stderr: ${stderr}`);
let ttlx = stderr + '\n' + stdout;
return bot.sendMessage(msg.chat.id, ttlx);
});
};
async function compile(msg, network){
var command = 'node ./node_modules/truffle/build/cli.bundled.js' + ' ' + 'compile';
if(!network || network === ''){
var options = {
reply_markup: JSON.stringify({
inline_keyboard: [
[{ text: 'Testnet (ropsten)', callback_data: '6' }],
[{ text: 'Mainnet (ethereum)', callback_data: '7' }]
]
})
};
return bot.sendMessage(msg.chat.id, "Select a Network", options);
}
// run
exec(command, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
let ttlerr = "Check details below and try again. "+'\n'+"There was an error reported from server:" + '\n' + '\n' + error;
return bot.sendMessage(msg.chat.id, ttlerr, options);
}
console.log(`stdout: ${stdout}`);
console.error(`stderr: ${stderr}`);
let ttlx = stderr + '\n' + stdout;
return bot.sendMessage(msg.chat.id, ttlx, options);
});
}
async function deploy(msg, contractName, network){
var command = 'node ./node_modules/truffle/build/cli.bundled.js --network' + ' ' + network + ' ' + 'migrate';
// run
exec(command, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
let ttlerr = "Check details below and try again. "+'\n'+"There was an error reported from server:" + '\n' + '\n' + error;
return bot.sendMessage(msg.chat.id, ttlerr);
}
console.log(`stdout: ${stdout}`);
console.error(`stderr: ${stderr}`);
let ttlx = stderr + '\n' + stdout;
return bot.sendMessage(msg.chat.id, ttlx);
});
}
async function verify(msg, network, contractName, contractAddress){
var command = 'node ./node_modules/truffle/build/cli.bundled.js --network' + ' ' + network + ' ' + 'verify' + ' ' + contractName + '@' + contractAddress;
// run
exec(command, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
let ttlerr = "Check details below and try again. "+'\n'+"There was an error reported from server:" + '\n' + '\n' + error;
return bot.sendMessage(msg.chat.id, ttlerr, options);
}
console.log(`stdout: ${stdout}`);
console.error(`stderr: ${stderr}`);
let ttlx = stderr + '\n' + stdout;
var options = {
reply_markup: JSON.stringify({
inline_keyboard: [
[{ text: 'Try again to Verify Contract', callback_data: '5' }]
]
})
};
return bot.sendMessage(msg.chat.id, ttlx, options);
});
}
async function run(msg, network, action, contractName, contractAddress, etherscanApiKey){
let compiled = await compile(msg, network, action);
let deployed = false;
if(compiled == true) {
deployed = await deploy(msg, network);
}
let verified = false;
if(deployed == true){
verified = await verify(msg, network, action, contractName);
}
}
// Grab telegram Object and handle it back
bot.onText(new RegExp('^'+botCommands['start'], 'i'), (msg, match) => {
if(msg.from.username==msg.chat.username){
var options = {
reply_markup: JSON.stringify({
inline_keyboard: [
[{ text: 'Configure', callback_data: '0' }],
[{ text: 'Import', callback_data: '1' }],
[{ text: 'Select', callback_data: '2' }],
[{ text: 'Compile', callback_data: '3' }],
[{ text: 'Deploy', callback_data: '4' }],
[{ text: 'Verify', callback_data: '5' }]
]
})
};
bot.sendMessage(msg.chat.id, description, options);
} else {
console.log("\n"+"chat message: ");
console.log(msg.chat);
console.log("\n"+"chat ID: ");
console.log(msg.chat.id);
console.log("\n"+"chat object: ");
console.log(msg);
console.log('Start request from @%s (%s)', [msg.from.username, msg.from.id]);
var response = 'Hello Deployer, ';
bot.sendMessage(msg.chat.id, response, { parse_mode: 'Markdown' });
}
});
// Grab telegram Object and handle it back
bot.onText(new RegExp('^'+botCommands['test'], 'i'), (msg, match) => {
if(msg.from.username==msg.chat.username){
run(network, action, contractName, contractAddress, etherscanApiKey);
bot.sendMessage(msg.chat.id, compileText);
} else {
console.log("\n"+"chat message: ")
console.log(msg.chat)
console.log("\n"+"chat ID: ")
console.log(msg.chat.id)
console.log("\n"+"chat object: ")
console.log(msg)
// log('info', logSystem, 'Pool statistics request from @%s (%s)', [msg.from.username, msg.from.id]);
console.log('Start request from @%s (%s)', [msg.from.username, msg.from.id]);
var response = 'Hi @' + msg.from.username + ',\n\n' + 'Here are some commands you can use, PM the bot for more intel:\n\n' + 'Help: ' + botCommands['help'] + '\n' + 'Thank you!';
bot.sendMessage(msg.chat.id, response, { parse_mode: 'Markdown' });
}
});
// Grab telegram Object and handle it back
bot.onText(new RegExp('^'+botCommands['deploy'], 'i'), (msg, match) => {
if(msg.from.username==msg.chat.username){
bot.sendMessage(msg.chat.id, deployText);
} else {
console.log("\n"+"chat message: ")
console.log(msg.chat)
console.log("\n"+"chat ID: ")
console.log(msg.chat.id)
console.log("\n"+"chat object: ")
console.log(msg)
// log('info', logSystem, 'Pool statistics request from @%s (%s)', [msg.from.username, msg.from.id]);
console.log('Start request from @%s (%s)', [msg.from.username, msg.from.id]);
var response = 'Hi @' + msg.from.username + ',\n\n' + 'Here are some commands you can use, PM the bot for more intel:\n\n' + 'Help: ' + botCommands['help'] + '\n' + 'Thank you!';
bot.sendMessage(msg.chat.id, response, { parse_mode: 'Markdown' });
}
});
bot.on('message', function (message) {
var callback = answerCallbacks[message.chat.id];
if (callback) {
delete answerCallbacks[message.chat.id];
return callback(message);
}
});
bot.on('callback_query', function onCallbackQuery(callbackQuery) {
const action = callbackQuery.data;
const msg = callbackQuery.message;
let message = msg;
console.log(msg);
const opts = {
chat_id: msg.chat.id,
message_id: msg.message_id,
};
let text;
if (action === '0') {
text = 'Configure a Contract';
// return configure(msg);
text2 = 'Check the contracts in storage...';
bot.sendMessage(msg.chat.id, text2);
const contractStorageDirectory = 'storage';
let contractsInStorage = [];
fs.readdirSync(contractStorageDirectory).forEach(contracts => {
console.log(contracts);
contractsInStorage.push(contracts);
bot.sendMessage(msg.chat.id, contracts);
});
if(!contractName || contractName === '' || contractName === null || !pastebin || pastebin === ''){
bot.sendMessage(message.chat.id, "Enter Contract Name").then(function () {
answerCallbacks[message.chat.id] = function (answer) {
var contractName = answer.text;
bot.sendMessage(message.chat.id, "Enter Token Decimals").then(function () {
answerCallbacks[message.chat.id] = function (answer) {
var tokenDecimals = answer.text;
bot.sendMessage(message.chat.id, "Enter Solidity Version").then(function () {
answerCallbacks[message.chat.id] = function (answer) {
var solidityVersion = answer.text;
bot.sendMessage(message.chat.id, "Enter Token Name").then(function () {
answerCallbacks[message.chat.id] = function (answer) {
var tokenName = answer.text;
bot.sendMessage(message.chat.id, "Enter Token Symbol").then(function () {
answerCallbacks[message.chat.id] = function (answer) {
var tokenSymbol = answer.text;
bot.sendMessage(message.chat.id, "Enter Total Supply").then(function () {
answerCallbacks[message.chat.id] = function (answer) {
var totalSupply = answer.text;
bot.sendMessage(message.chat.id, "Enter Mint Amount").then(function () {
answerCallbacks[message.chat.id] = function (answer) {
var mintAmount = answer.text;
bot.sendMessage(message.chat.id, "Enter Uniswap Router Address").then(function () {
answerCallbacks[message.chat.id] = function (answer) {
var uniswapV2 = answer.text;
bot.sendMessage(message.chat.id, "Enter Marketing Wallet").then(function () {
answerCallbacks[message.chat.id] = function (answer) {
var marketingWallet = answer.text;
bot.sendMessage(message.chat.id, "Enter Liquidity Wallet").then(function () {
answerCallbacks[message.chat.id] = function (answer) {
var liquidityWallet = answer.text;
bot.sendMessage(message.chat.id, "Enter Burn Wallet").then(function () {
answerCallbacks[message.chat.id] = function (answer) {
var burnWallet = answer.text;
let configData = {
args:[tokenName.toString(),tokenSymbol.toString(),totalSupply,mintAmount,marketingWallet.toString(),liquidityWallet.toString(),burnWallet.toString()],
solc:solidityVersion,
contractName:contractName,
tokenName:tokenName,
symbol:tokenSymbol,
supply:parseInt(totalSupply),
decimals:parseInt(tokenDecimals),
mintAmount:parseInt(mintAmount),
wallets:[marketingWallet.toString(),liquidityWallet.toString(),burnWallet.toString()],
uniswapRouter:uniswapV2.toString(),
marketingWallet:marketingWallet.toString(),
liquidityWallet:liquidityWallet.toString(),
burnWallet:burnWallet.toString()
}
answerCallbacks = {};
let configDir = 'configs/';
let contractConfig = configDir+contractName+'.json';
fs.writeFileSync(contractConfig, configData.toString());
let configFileWrote = "Wrote Config File for Contract "+contractName;
let doneMessage = "Solidity Version:" + solidityVersion + '\n' + "Contract Name: "+contractName + '\n' + "Token Name: "+tokenName + '\n'+ "Token Symbol: "+tokenSymbol + '\n'+ "Token Supply: "+totalSupply + '\n'+ "Token Decimals: "+tokenDecimals + '\n'+ "Mint Amount: "+mintAmount + '\n'+ "Uniswap Router: "+uniswapV2 + '\n'+ "Marketing Wallet: "+marketingWallet + '\n'+ "Liquidity Wallet: "+liquidityWallet + '\n'+ "Burn Wallet: "+burnWallet + '\n';
contractName = null;
bot.sendMessage(msg.chat.id, doneMessage.toString());
return bot.sendMessage(msg.chat.id, configFileWrote);
}
});
}
});
}
});
}
});
}
});
}
});
}
});
}
});
}
});
}
});
}
});
}
} if (action === '1') {
text = 'Are you there? Reply to get started...';
var options = {
reply_markup: JSON.stringify({
inline_keyboard: [
[{ text: 'Import', callback_data: '1' }]
]
})
};
if(!contractName || contractName === '' || contractName === null || !pastebin || pastebin === ''){
answerCallbacks[message.chat.id] = function (answer) {
var ca = answer.text;
console.log(ca);
bot.sendMessage(message.chat.id, "Enter Contract Name").then(function () {
answerCallbacks[message.chat.id] = function (answer) {
var contractName = answer.text;
bot.sendMessage(message.chat.id, "Enter Contract Pastebin URL").then(function () {
answerCallbacks[message.chat.id] = function (answer) {
var pastebinURL = answer.text;
bot.sendMessage(message.chat.id, "Enter Password (directory security)").then(function () {
answerCallbacks[message.chat.id] = function (answer) {
var passwordToMatch = answer.text;
if(passwordToMatch.toString() != filePassword.toString()){
return bot.sendMessage(msg.chat.id, "Wrong Password", options);
}
answerCallbacks = {};
return importCA(msg,pastebinURL,contractName);
}
});
}
});
}
});
};
}
} else if (action === '2') {
text = 'Check the contracts in storage...';
const contractStorageDirectory = 'storage';
let contractsInStorage = [];
fs.readdirSync(contractStorageDirectory).forEach(contracts => {
console.log(contracts);
contractsInStorage.push(contracts);
bot.sendMessage(msg.chat.id, contracts);
});
bot.sendMessage(message.chat.id, "Enter Contract Name").then(function () {
answerCallbacks[message.chat.id] = function (answer) {
contractName = answer.text;
let precommand = 'rm -rf contracts/*';
function removeContractFromPipeline(precommand,contractName){
exec(precommand, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
} else {
console.log(`stdout: ${stdout}`);
console.error(`stderr: ${stderr}`);
let ttlx = stderr + '\n' + stdout;
let command = 'cp storage/'+contractName.toString()+' '+'contracts/'+contractName.toString();
function moveContractFromStorage(command,contractName){
exec(command, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
}
console.log(`stdout: ${stdout}`);
console.error(`stderr: ${stderr}`);
let ttlx = stderr + '\n' + stdout;
return bot.sendMessage(msg.chat.id, ttlx);
});
}
return moveContractFromStorage(command,contractName);
}
});
}
removeContractFromPipeline(precommand,contractName);
var options = {
reply_markup: JSON.stringify({
inline_keyboard: [
[{ text: 'Testnet (ropsten)', callback_data: '9' }],
[{ text: 'Mainnet (ethereum)', callback_data: '10' }]
]
})
};
bot.sendMessage(message.chat.id, "Select a Network for Deployment", options);
}
});
} else if (action === '3') {
text = 'Compiling Contract';
return compile(msg,network);
} else if (action === '4') {
text = 'Deploying Contract';
network = 'ropsten';
infuraURL = 'ropsten';
networkURI = 'ropsten';
if(!contractName || contractName === '' || contractName === null ){
return bot.sendMessage(message.chat.id, "Error, no contract name.",)
}
return deploy(msg,contractName,network);
} else if (action === '5') {
text = 'Verifying Contract';
return verify(msg,network, contractName, contractAddress);
} else if (action === '6') {
text = 'Compiling Contract';
network = 'ropsten';
infuraURL = 'ropsten';
networkURI = 'ropsten';
return compile(msg,network);
} else if (action === '7') {
text = 'Verifying Contract';
network = 'mainnet';
infuraURL = 'mainnet';
networkURI = 'mainnet';
return compile(msg,network);
} else if (action === '8') {
network = 'ropsten';
infuraURL = 'ropsten';
networkURI = 'ropsten';
text = 'Deploying Contract to'+ " "+network;
if(!contractName || contractName === '' || contractName === null ){
return bot.sendMessage(message.chat.id, "Error, no contract name.",)
}
return deploy(msg,contractName,network);
} else if (action === '9') {
network = 'ropsten';
infuraURL = 'ropsten';
networkURI = 'ropsten';
text = 'Deploying Contract to'+ " "+network;
if(!contractName || contractName === '' || contractName === null ){
return bot.sendMessage(message.chat.id, "Error, no contract name.",)
}
return deploy(msg,contractName,network);
} else if (action === '10') {
network = 'mainnet';
infuraURL = 'mainnet';
networkURI = 'mainnet';
text = 'Deploying Contract to'+ " "+network;
if(!contractName || contractName === ''){
return bot.sendMessage(message.chat.id, "Error, no contract name.",)
}
return deploy(msg,contractName,network);
} else { return; }
bot.editMessageText(text, opts);
});
bot.on('polling_error', (error) => {
console.log(error);
});
var listener = app.listen(port, function() {
console.log('Express server listening on port', port)
});