-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.js
215 lines (181 loc) · 5.29 KB
/
main.js
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
// Modules to control application life and create native browser window
const chalk = require('chalk')
const { app, BrowserWindow, ipcMain, Menu } = require('electron')
const path = require('path')
const { CheckScam } = require('./src/helpers/checkScam')
const { checkProfits } = require('./src/helpers/profits')
const store = new (require('node-storage'))(path.join(__dirname, 'user/config.json'))
const sleep = (timeMs) => new Promise(resolve => setTimeout(resolve, timeMs))
const { Init, Stop, Buy, Sell } = require('./src/sniper')
const { getTokenIndexByAddress } = require('./src/utils/token')
var isBotRunning = false
process.on('uncaughtException', function (error) {
console.log('-----RATE LIMIT/TIMEOUT-----')
})
process.on('UnhandledPromiseRejectionWarning', function (error) {
console.log('-----RETRY-----')
})
function createWindow() {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 1100,
height: 720,
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
})
// and load the index.html of the app.
mainWindow.loadFile('index.html')
Menu.setApplicationMenu(null)
if (process.argv[2] === 'dev')
mainWindow.webContents.openDevTools()
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
createWindow()
app.on('activate', function () {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})
/// EVENTS
ipcMain.handle('save-config', async (event, data) => {
return _saveConfig(data)
})
ipcMain.handle('get-config', async (event) => {
return await _getConfig()
})
ipcMain.handle('exit-app', async (event, data) => {
await _stopBot()
await sleep(2000)
app.exit(0)
})
ipcMain.handle('restart-bot', async (event, data) => {
await _stopBot()
await sleep(2000)
return await _initBot()
})
ipcMain.handle('init-bot', async (event, data) => {
return await _initBot()
})
ipcMain.handle('get-tokens', async (event) => {
return _getTokens()
})
ipcMain.handle('save-tokens', async (event, data) => {
return _saveTokens(data)
})
ipcMain.handle('buy-token', async (event, data) => {
return await _buyToken(data)
})
ipcMain.handle('sell-token', async (event, data) => {
return await _sellToken(data)
})
/// FUNCTIONS
async function _saveConfig(data) {
Object.keys(data).forEach((key) => {
store.put(`config.${key}`, data[key])
})
await _stopBot()
await _initBot()
return {
msg: 'success'
}
}
function _saveTokens(data) {
store.put('tokens', data)
}
async function _getConfig() {
const config = store.get('config') || undefined
if (config) {
config['botRunning'] = isBotRunning
store.put('config', config)
}
return {
msg: config ? 'success' : 'error',
config
}
}
function _getTokens() {
var tokens = store.get('tokens') || undefined
return {
msg: tokens ? 'success' : 'error',
tokens
}
}
async function _initBot() {
await Init(store)
isBotRunning = true
broadcastTokens()
return await _getConfig()
}
async function _buyToken(token) {
try {
await Buy(token, store)
await sleep(1000)
return {
msg: 'success'
}
} catch (err) {
return {
msg: 'error'
}
}
}
async function _sellToken(token) {
try {
await Sell(token, store)
const tokenIndex = getTokenIndexByAddress(token.address, store.get('tokens'))
if (tokenIndex)
store.get('tokens').slice(tokenIndex, 1)
await sleep(1000)
return {
msg: 'success'
}
} catch (err) {
return {
msg: 'error'
}
}
}
async function broadcastTokens() {
while (true) {
const tokens = store.get('tokens')
const config = store.get('config')
if (tokens && tokens.length > 0)
for (var i = 0; i < tokens.length; i++) {
const token = tokens[i]
if (config.checkScam && !token.isScam) {
try {
console.log(chalk.red('Checking SCAM...'))
const isScam = await CheckScam(token, config)
token['isScam'] = isScam
} catch (err) { }
}
if (token.status === 'bought') {
try {
console.log(chalk.blue('Checking Profits...'))
const response = await checkProfits(token.address)
token['profit'] = response.profitPercent < 0 ? `-${response.profit.toString().replace('-', '').replace('+', '')}` : `+${response.profit.toString().replace('+', '').replace('-', '')}`
token['profitText'] = response.msg
token['profitPercent'] = response.profitPercent < 0 ? `-${response.profitPercent.toString().replace('-', '')}` : `+${response.profitPercent.toString().replace('+', '')}`
} catch (err) { }
}
}
store.put('tokens', tokens)
await sleep(6000)
}
}
async function _stopBot() {
Stop()
isBotRunning = false
return await _getConfig()
}