-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbouncer.py
238 lines (200 loc) · 8.4 KB
/
bouncer.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import subprocess
import json
import time
import os
from flask import Flask, render_template, request, redirect, url_for, flash
app = Flask(__name__)
app.secret_key = 'your_secret_key'
# File paths for saving mnemonic, token IDs, and wallet initialization status
MNEMONIC_FILE = 'mnemonic.txt'
TOKEN_IDS_FILE = 'token_ids.txt'
WALLET_INITIALIZED_FILE = 'wallet_initialized.txt'
BOUNCE_ADDRESS_FILE = 'bounce_address.txt' # File to save bounce address
# Default bounce address
DEFAULT_BOUNCE_ADDRESS = '4MQyMKvMbnCJG3aJ'
# Function to run subprocess and return output
def run_subprocess(command):
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, error = process.communicate()
return output.decode(), error.decode()
# Function to save mnemonic to file and initialize wallet
def save_mnemonic(mnemonic):
with open(MNEMONIC_FILE, 'w') as file:
file.write(mnemonic)
initialize_wallet(mnemonic)
#check_and_bounce_tokens()
#start_token_checking_loop()
flash("Wallet mnemonic set successfully", "success")
# Function to save token IDs to file and start the loop for checking and sending tokens
def save_token_ids(token_ids):
with open(TOKEN_IDS_FILE, 'w') as file:
file.write('\n'.join(token_ids))
# Function to save bounce address to file
def save_bounce_address(bounce_address):
with open(BOUNCE_ADDRESS_FILE, 'w') as file:
file.write(bounce_address)
# Function to read bounce address from file
def get_saved_bounce_address():
try:
with open(BOUNCE_ADDRESS_FILE, 'r') as file:
return file.read().strip()
except FileNotFoundError:
return DEFAULT_BOUNCE_ADDRESS # Return default if file doesn't exist
# Function to check if wallet is initialized
def is_wallet_initialized():
return os.path.exists(WALLET_INITIALIZED_FILE)
# Function to initialize wallet with mnemonic
def initialize_wallet(mnemonic):
if not is_wallet_initialized():
command = ['ewc', 'new-wallet', '-n', 'test', '-p', '1234', '-m', mnemonic]
_, error = run_subprocess(command)
if error:
print("Error initializing wallet:", error)
else:
with open(WALLET_INITIALIZED_FILE, 'w'):
pass # Create empty file to mark wallet as initialized
# Function to get mnemonic from file
def get_mnemonic():
try:
with open(MNEMONIC_FILE, 'r') as file:
return file.read().strip()
except FileNotFoundError:
return None
# Function to get token IDs from file
def get_token_ids():
try:
with open(TOKEN_IDS_FILE, 'r') as file:
return [line.strip() for line in file.readlines()]
except FileNotFoundError:
return []
# Function to start the loop for checking and sending tokens
def start_token_checking_loop():
while True:
check_and_bounce_tokens()
time.sleep(5 * 60) # 5 minutes
# Function to start the loop for checking and sending tokens
def start_token_checking_loop_all():
while True:
check_and_bounce_all_tokens()
time.sleep(5 * 60) # 5 minutes
# Function to check wallet for tokens and bounce if found
def check_and_bounce_tokens():
if is_wallet_initialized():
mnemonic = get_mnemonic()
token_ids = get_token_ids()
if not mnemonic or not token_ids:
return
command = ['ewc', 'wallet-get', '-b', 'all', 'test', '1234']
output, error = run_subprocess(command)
if error:
print("Error:", error)
return
wallet_data = json.loads(output)
tokens = wallet_data.get('tokens', [])
bounce_address = get_saved_bounce_address() # Get the saved bounce address
for token_id in token_ids:
for token in tokens:
if token['tokenId'] == token_id:
# Bounce token
token_amount = token['amount'].replace(',', '') # Remove comma
process = subprocess.Popen(['ewc', 'wallet-send', '-e', '0.0001', '-t', token['tokenId'], '-u', token_amount, '-a', bounce_address, 'test', '1234', '--sign', '--send'], stdin=subprocess.PIPE)
process.communicate(input=b'y\n') # Send "y" to confirm the transaction
print("Token bounced:", token['tokenId'])
break # Stop searching for this token ID once it's found
# Function to check wallet for all tokens and bounce if found
def check_and_bounce_all_tokens():
if is_wallet_initialized():
mnemonic = get_mnemonic()
if not mnemonic:
return
command = ['ewc', 'wallet-get', '-b', 'all', 'test', '1234']
output, error = run_subprocess(command)
if error:
print("Error:", error)
return
wallet_data = json.loads(output)
tokens = wallet_data.get('tokens', [])
bounce_address = get_saved_bounce_address() # Get the saved bounce address
for token in tokens:
token_id = token['tokenId']
# Bounce token
token_amount = token['amount'].replace(',', '') # Remove comma
process = subprocess.Popen(['ewc', 'wallet-send', '-e', '0.0001', '-t', token_id, '-u', token_amount, '-a', bounce_address, 'test', '1234', '--sign', '--send'], stdin=subprocess.PIPE)
process.communicate(input=b'y\n') # Send "y" to confirm the transaction
print("Token bounced:", token_id)
# Route to start bouncing tokens
@app.route('/start_bouncing', methods=['POST'])
def start_bouncing():
mnemonic = get_mnemonic()
if mnemonic:
flash("Bouncing tokens initiated", "success")
start_token_checking_loop() # Start the bouncing loop
else:
flash("Please set the wallet mnemonic first", "error")
return redirect(url_for('home'))
# Route to start bouncing all tokens
@app.route('/start_bouncing_all', methods=['POST'])
def start_bouncing_all():
mnemonic = get_mnemonic()
if mnemonic:
flash("Bouncing tokens initiated", "success")
start_token_checking_loop_all()
#start_token_checking_loop() # Start the bouncing loop
else:
flash("Please set the wallet mnemonic first", "error")
return redirect(url_for('home'))
# Route to set new bounce address
@app.route('/set_bounce_address', methods=['POST'])
def set_bounce_address():
new_bounce_address = request.form['new_bounce_address']
# Save the new bounce address
save_bounce_address(new_bounce_address)
# Flash success message
flash("Bounce address updated successfully", "success")
# Redirect to home page`
return redirect(url_for('home'))
# Route to set mnemonic
@app.route('/set_mnemonic', methods=['POST'])
def set_mnemonic():
mnemonic = request.form['mnemonic']
save_mnemonic(mnemonic)
return redirect(url_for('home'))
# Route for home page
@app.route('/')
def home():
mnemonic = get_mnemonic()
token_ids = get_token_ids()
bounce_address = get_saved_bounce_address() # Get the saved bounce address
return render_template('index.html', mnemonic=mnemonic, token_ids=token_ids, bounce_address=bounce_address)
@app.route('/add_token_id', methods=['POST'])
def add_token_id():
token_id = request.json.get('token_id', '')
if token_id:
token_ids = get_token_ids()
token_ids.append(token_id)
save_token_ids(token_ids)
flash(f'Token ID "{token_id}" added successfully.', 'success')
else:
flash('Failed to add token ID. Please provide a valid token ID.', 'error')
return redirect(url_for('home'))
@app.route('/remove_token_id', methods=['POST'])
def remove_token_id():
token_id = request.json.get('token_id', '')
if token_id:
token_ids = get_token_ids()
if token_id in token_ids:
token_ids.remove(token_id)
save_token_ids(token_ids)
flash(f'Token ID "{token_id}" removed successfully.', 'success')
else:
flash(f'Token ID "{token_id}" not found.', 'error')
else:
flash('Failed to remove token ID. Please provide a valid token ID.', 'error')
return redirect(url_for('home'))
# Route to reset mnemonic (not used right now)
@app.route('/reset_mnemonic', methods=['POST'])
def reset_mnemonic():
subprocess.run(['rm', 'ewc/build/wallets/test.wallet'])
return redirect(url_for('home'))
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)