-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
223 lines (192 loc) · 6.55 KB
/
app.py
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
import asyncio
import sys
import threading
import webbrowser
import logging
from flask import Flask
from flask_socketio import SocketIO, emit
import requests.exceptions
from web3 import Web3
from bsc_blockchain import fire_and_forget
from connection import Connection
# Flask application setup
app = Flask(__name__, static_folder='static', static_url_path='/')
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app, async_mode="threading", cors_allowed_origins="*")
app.test_client()
connection = Connection(app)
# Configure logging
log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)
@app.after_request
def after_request(response):
"""
Set response headers to prevent caching and allow cross-origin requests.
"""
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
response.headers["Pragma"] = "no-cache"
response.headers["Expires"] = "0"
response.headers['Cache-Control'] = 'public, max-age=0'
response.headers['Access-Control-Allow-Origin'] = '*'
return response
@app.route('/')
def index():
"""
Serve the main page.
"""
return app.send_static_file('index.html')
@socketio.event
def test_event(message):
emit('test_response', {'data': message['data']})
@socketio.event
def login(message):
connection.login(message)
@socketio.event
def check_login(message):
print("YurtTraders Client checked login status...")
connection.check_login()
@socketio.event
def log_out(message):
connection.log_out()
@socketio.event
def set_mnemonic(message):
connection.set_mnemonic(message)
@socketio.event
def refresh_wallet(message):
try:
connection.refresh_wallet()
except requests.exceptions.ConnectionError:
pass
@socketio.event
def distribute(message):
print(message)
fire_and_forget(connection.blockchain.distribute_balance(float(message['amount'])))
@socketio.event
def collect(message):
print("Collecting to main wallet...")
fire_and_forget(connection.blockchain.collect_to_main_wallet())
@socketio.event
def transfer(message):
print("Transferring")
fire_and_forget(
connection.blockchain.transfer_bnb(
connection.blockchain.wallets[0],
message['receiver'],
float(message['amount'])
)
)
@socketio.event
def single_buy(message):
fire_and_forget(
connection.blockchain.buy(
connection.blockchain.wallets[0],
message['amount'],
message['contract']
)
)
@socketio.event
def approve(message):
loops = []
wallet_counter = 0
print(message)
if message['all']:
for _ in range(len(connection.blockchain.wallets)):
loop = asyncio.new_event_loop()
threading.Thread(target=loop.run_forever, daemon=True).start()
loops.append(loop)
for wallet in connection.blockchain.wallets:
loops[wallet_counter].call_soon_threadsafe(
asyncio.create_task,
connection.blockchain.approve(
wallet,
message['contract'],
message['check_balance']
)
)
wallet_counter += 1
else:
loop = asyncio.new_event_loop()
loop.run_until_complete(
connection.blockchain.approve(
connection.blockchain.wallets[int(message['wallet'])],
message['contract'],
message['check_balance']
)
)
@socketio.event
def buy(message):
print(message)
loops = []
loop_counter = 0
wallet_counter = 0
for _ in range(int(message['repeat']) * len(connection.blockchain.wallets)):
loop = asyncio.new_event_loop()
threading.Thread(target=loop.run_forever, daemon=True).start()
loops.append(loop)
for wallet in connection.blockchain.wallets:
i = wallet.nonce
if message['wallets'] is not False and wallet_counter == message['wallets']:
break
for _ in range(int(message['repeat'])):
loops[loop_counter].call_soon_threadsafe(
asyncio.create_task,
connection.blockchain.buy(wallet, message['amount'], message['contract'], i)
)
i += 1
loop_counter += 1
wallet_counter += 1
# Telegram related socket events
@socketio.event
def telegram_connect():
connection.telegram_connect()
@socketio.event
def telegram_disconnect():
connection.disconnect_telegram()
@socketio.event
def telegram_dialogues():
print("Telegram dialogue renewal request")
fire_and_forget(connection.telegram.get_dialogues())
@socketio.event
def sell_gas(message):
print("Sell gas calculated.")
fire_and_forget(connection.blockchain.calculate_sell_gas(message['contract']))
@socketio.event
def sell(message):
if connection.blockchain is not None:
for wallet in range(message['count']):
fire_and_forget(connection.blockchain.sell(connection.blockchain.wallets[wallet], message['contract']))
else:
emit('test_response', {
'data': '<span style="color:red">Error: blockchain connection has not established. '
'Either you did not wait for the node response or did not set your mnemonic yet.</span>'
})
@socketio.event
def save_settings(message):
user_data: dict = connection.user.config
print(user_data)
update_wallet = False
if int(user_data['buy']['wallets']) != int(message['buy']['wallets']) or user_data['active'] != message['active']:
update_wallet = True
update_telegram = False
if (user_data['telegram']['whitelist'] != message['telegram']['whitelist'] or
user_data['telegram']['blacklist'] != message['telegram']['blacklist'] or
user_data['telegram']['listen'] != message['telegram']['listen']):
update_telegram = True
user_data.update(message)
connection.user.config = user_data
connection.user.save()
if update_wallet:
connection.blockchain = None
connection.init_modules()
if update_telegram and connection.telegram:
connection.telegram_connect()
@app.before_request
def initialize():
print("Application reloaded.")
if __name__ == '__main__':
port = sys.argv[1] if len(sys.argv) > 1 else 3059
print("\n##########################################################"
"\n##################### YURTTRADERS V3 #####################"
"\n################# http://localhost:" + str(port) + " ###################\n")
print("Work location: ", __file__)
socketio.run(app, debug=False, host='0.0.0.0', port=port)