-
Notifications
You must be signed in to change notification settings - Fork 360
Expand file tree
/
Copy pathmain.py
More file actions
514 lines (431 loc) · 20 KB
/
main.py
File metadata and controls
514 lines (431 loc) · 20 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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
BeraChainTools - Interactive Terminal Interface
Toolkit for BeraChain ecosystem: faucet, BEX, Honey, Bend, BeraName and more.
"""
import os
import sys
import subprocess
import json
from pathlib import Path
from rich.console import Console
from rich.panel import Panel
from rich.table import Table
from rich.prompt import Prompt, IntPrompt, Confirm
from rich.progress import Progress, SpinnerColumn, TextColumn
from rich import box
from rich.text import Text
from utils import ensure_env
PROJECT_ROOT = Path(__file__).parent
os.chdir(PROJECT_ROOT)
console = Console()
# Custom BeraChainTools Logo
BERACHAIN_LOGO = r"""
____ _____ _ _ _______ _
| _ \ / ____| | | (_) |__ __| | |
| |_) | ___ _ __ __ _ | | | |__ __ _ _ _ __ | | ___ ___ | |___
| _ < / _ \ '__/ _` | | | | '_ \ / _` | | | | '_ \ | |/ _ \ / _ \| / __|
| |_) | __/ | | (_| | | |____ | | | | | (_| | | | | | | | | | (_) | (_) | \__ \
|____/ \___|_| \__,_| \_____| |_| |_| \__,_| |_| |_| |_| |_|\___/ \___/|_|___/
"""
def load_config():
"""Load configuration from config.json"""
config_path = PROJECT_ROOT / "config.json"
default_config = {
"rpc_url": "https://rpc.ankr.com/berachain_testnet",
"client_key": "",
"solver_provider": "yescaptcha",
"private_key": "",
"use_proxy": False,
"proxy_http": "http://127.0.0.1:8888",
"proxy_https": "http://127.0.0.1:8888",
}
try:
if config_path.exists():
with open(config_path, "r", encoding="utf-8") as f:
return {**default_config, **json.load(f)}
except Exception as e:
console.print(f"[red]Error loading config: {e}[/red]")
return default_config
def save_config(config):
"""Save configuration to config.json"""
config_path = PROJECT_ROOT / "config.json"
try:
with open(config_path, "w", encoding="utf-8") as f:
json.dump(config, f, indent=2)
return True
except Exception as e:
console.print(f"[red]Error saving config: {e}[/red]")
return False
def show_header():
"""Display the main header with logo and info"""
console.clear()
console.print(Panel(Text(BERACHAIN_LOGO, style="bold green"),
border_style="green", box=box.DOUBLE, padding=(0, 1)))
info_table = Table(show_header=False, box=box.ROUNDED, border_style="dim")
info_table.add_column("", style="green")
info_table.add_column("", style="white")
info_table.add_row("Project", "BeraChainTools - BeraChain Ecosystem Toolkit")
info_table.add_row("Network", "BeraChain Artio Testnet")
info_table.add_row("Features", "Faucet, BEX, Honey, Bend, BeraName, HoneyJar")
info_table.add_row("Captcha", "YesCaptcha, 2Captcha (Cloudflare Turnstile)")
console.print(Panel(info_table, title="[bold]Project Info[/bold]", border_style="blue", padding=(0, 1)))
console.print()
def install_dependencies():
"""Install all required Python packages"""
show_header()
console.print("[bold]Install Dependencies[/bold]\n", style="green")
deps_table = Table(title="Packages to Install", box=box.ROUNDED, border_style="green")
deps_table.add_column("#", style="dim")
deps_table.add_column("Package", style="cyan")
deps_table.add_column("Purpose", style="white")
deps_table.add_row("1", "rich", "Terminal UI")
deps_table.add_row("2", "web3", "Blockchain interaction")
deps_table.add_row("3", "eth-account", "Wallet management")
deps_table.add_row("4", "requests", "HTTP requests")
deps_table.add_row("5", "loguru", "Logging")
deps_table.add_row("6", "Faker", "User agent generation")
deps_table.add_row("7", "py-solc-x", "Solidity compiler")
console.print(deps_table)
console.print()
req_path = PROJECT_ROOT / "requirements.txt"
if not req_path.exists():
console.print("[red]requirements.txt not found![/red]")
Prompt.ask("\nPress Enter to return to menu")
return
if Confirm.ask("Proceed with installation?", default=True):
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
console=console,
) as progress:
task = progress.add_task("Installing packages...", total=None)
try:
subprocess.run(
[sys.executable, "-m", "pip", "install", "-r", str(req_path), "-q"],
check=True,
capture_output=True,
cwd=PROJECT_ROOT,
)
progress.update(task, description="[green]Packages installed, cloning BeraChainTools...")
# Clone BeraChainTools repo if not present
bera_repo = PROJECT_ROOT / "bera_tools_repo"
if not bera_repo.exists():
try:
subprocess.run(
["git", "clone", "https://github.com/ymmmmmmmm/BeraChainTools.git", str(bera_repo)],
check=True,
capture_output=True,
cwd=PROJECT_ROOT,
)
subprocess.run(
[sys.executable, "-m", "pip", "install", "-r", str(bera_repo / "requirements.txt"), "-q"],
check=True,
capture_output=True,
)
except (subprocess.CalledProcessError, FileNotFoundError):
console.print("[yellow]Could not clone BeraChainTools (git required). Clone manually:[/yellow]")
console.print(" git clone https://github.com/ymmmmmmmm/BeraChainTools.git bera_tools_repo")
progress.update(task, description="[green]Installation complete!")
console.print("\n[bold green]All dependencies installed successfully![/bold green]")
except subprocess.CalledProcessError as e:
progress.update(task, description="[red]Installation failed!")
console.print(f"\n[bold red]Error: {e}[/bold red]")
else:
console.print("[yellow]Installation cancelled.[/yellow]")
Prompt.ask("\nPress Enter to return to menu")
def settings_menu():
"""Settings configuration menu"""
show_header()
console.print("[bold]Settings[/bold]\n", style="green")
config = load_config()
settings_table = Table(title="Current Configuration", box=box.ROUNDED, border_style="yellow")
settings_table.add_column("Setting", style="cyan")
settings_table.add_column("Value", style="white")
settings_table.add_column("Description", style="dim")
key_display = (config.get("client_key", "")[:15] + "...") if config.get("client_key") else "(not set)"
settings_table.add_row("Captcha API Key", key_display, "YesCaptcha or 2Captcha key")
settings_table.add_row("Solver Provider", config.get("solver_provider", "yescaptcha"), "yescaptcha or 2captcha")
settings_table.add_row("RPC URL", config.get("rpc_url", "")[:40] + "...", "BeraChain RPC endpoint")
pk_display = (config.get("private_key", "")[:10] + "...") if config.get("private_key") else "(not set)"
settings_table.add_row("Private Key", pk_display, "Wallet private key (0x...)")
settings_table.add_row("Use Proxy", str(config.get("use_proxy", False)), "Enable HTTP proxy")
console.print(settings_table)
console.print()
console.print("[bold]Edit settings:[/bold]")
new_key = Prompt.ask("Captcha API Key (YesCaptcha/2Captcha) [Enter to keep]", default=config.get("client_key", ""))
if new_key:
config["client_key"] = new_key
provider = Prompt.ask("Solver provider (yescaptcha/2captcha)", default=config.get("solver_provider", "yescaptcha"))
if provider in ("yescaptcha", "2captcha"):
config["solver_provider"] = provider
new_rpc = Prompt.ask("RPC URL", default=config.get("rpc_url", "https://rpc.ankr.com/berachain_testnet"))
if new_rpc:
config["rpc_url"] = new_rpc
new_pk = Prompt.ask("Private Key (0x...) [Enter to keep]", default=config.get("private_key", ""))
if new_pk:
config["private_key"] = new_pk
use_proxy = Confirm.ask("Use proxy?", default=config.get("use_proxy", False))
config["use_proxy"] = use_proxy
if use_proxy:
config["proxy_http"] = Prompt.ask("Proxy HTTP", default=config.get("proxy_http", "http://127.0.0.1:8888"))
config["proxy_https"] = Prompt.ask("Proxy HTTPS", default=config.get("proxy_https", "http://127.0.0.1:8888"))
if save_config(config):
console.print("\n[green]Settings saved successfully![/green]")
else:
console.print("\n[red]Failed to save settings.[/red]")
Prompt.ask("\nPress Enter to return to menu")
def about_menu():
"""About BeraChainTools and the project"""
show_header()
console.print("[bold]About BeraChainTools[/bold]\n", style="green")
about_table = Table(title="BeraChain Overview", box=box.ROUNDED, border_style="cyan")
about_table.add_column("Property", style="cyan")
about_table.add_column("Description", style="white")
about_table.add_row("Type", "EVM-compatible blockchain (Berachain)")
about_table.add_row("Testnet", "Artio - for testing and development")
about_table.add_row("Faucet", "https://artio.faucet.berachain.com/")
about_table.add_row("BEX", "DEX - swaps and liquidity")
about_table.add_row("Honey", "Stablecoin mint/redeem")
about_table.add_row("Bend", "Lending protocol")
about_table.add_row("BeraName", "Domain registration")
console.print(about_table)
console.print()
features_table = Table(title="Toolkit Features", box=box.ROUNDED, border_style="green")
features_table.add_column("Feature", style="cyan")
features_table.add_column("Status", style="white")
features_table.add_row("BeraChain Faucet (Claim)", "Completed")
features_table.add_row("BEX (Swap, Add Liquidity)", "Completed")
features_table.add_row("Honey (Mint, Redeem)", "Completed")
features_table.add_row("Bend (Deposit, Borrow, Repay)", "Completed")
features_table.add_row("0xHoneyJar Mint", "Completed")
features_table.add_row("BeraName Registration", "Completed")
features_table.add_row("Contract Deployment", "Completed")
features_table.add_row("Berps", "In Progress")
features_table.add_row("Station", "Pending")
console.print(features_table)
console.print()
links = Panel(
"Faucet: https://artio.faucet.berachain.com/\n"
"BEX: https://artio.bex.berachain.com/swap\n"
"Honey: https://artio.honey.berachain.com\n"
"Bend: https://artio.bend.berachain.com/\n"
"BeraNames: https://www.beranames.com\n"
"Berps: https://artio.berps.berachain.com/\n"
"Station: https://artio.station.berachain.com/",
title="Links",
border_style="blue"
)
console.print(links)
# Load hashtags from about folder
hashtags_path = PROJECT_ROOT / "about" / "hashtags.txt"
if hashtags_path.exists():
console.print()
console.print(Panel(hashtags_path.read_text(encoding="utf-8"), title="Hashtags", border_style="magenta"))
Prompt.ask("\nPress Enter to return to menu")
def run_bera_faucet():
"""Run BeraChain faucet claim"""
show_header()
console.print("[bold]BeraChain Faucet - Claim Test Tokens[/bold]\n", style="green")
config = load_config()
if not config.get("client_key"):
console.print("[red]Error: Captcha API key not configured. Go to Settings first.[/red]")
Prompt.ask("\nPress Enter to return to menu")
return
try:
sys.path.insert(0, str(PROJECT_ROOT))
from tools.faucet import run_claim
run_claim(config)
except ImportError as e:
console.print(f"[red]Error: {e}[/red]")
console.print("[yellow]Ensure BeraChainTools is properly installed. Run Install Dependencies.[/yellow]")
except Exception as e:
console.print(f"[red]Error: {e}[/red]")
Prompt.ask("\nPress Enter to return to menu")
def run_bex_menu():
"""BEX interaction submenu"""
show_header()
console.print("[bold]BEX - DEX Swaps & Liquidity[/bold]\n", style="green")
config = load_config()
if not config.get("private_key"):
console.print("[red]Error: Private key not configured. Go to Settings first.[/red]")
Prompt.ask("\nPress Enter to return to menu")
return
table = Table(title="BEX Options", box=box.ROUNDED, border_style="cyan")
table.add_column("Key", style="bold")
table.add_column("Action")
table.add_row("1", "Swap tokens (BERA/USDC/WETH)")
table.add_row("2", "Add liquidity")
table.add_row("0", "Back to main menu")
console.print(table)
choice = Prompt.ask("Select option", default="0")
if choice == "0":
return
try:
from tools.bex_tools import run_bex
run_bex(config, choice)
except ImportError:
console.print("[red]BEX tools module not available.[/red]")
Prompt.ask("\nPress Enter to return to menu")
def run_honey_menu():
"""Honey interaction submenu"""
show_header()
console.print("[bold]Honey - Mint & Redeem[/bold]\n", style="green")
config = load_config()
if not config.get("private_key"):
console.print("[red]Error: Private key not configured. Go to Settings first.[/red]")
Prompt.ask("\nPress Enter to return to menu")
return
table = Table(title="Honey Options", box=box.ROUNDED, border_style="yellow")
table.add_column("Key", style="bold")
table.add_column("Action")
table.add_row("1", "Mint Honey (USDC -> Honey)")
table.add_row("2", "Redeem Honey (Honey -> USDC)")
table.add_row("0", "Back to main menu")
console.print(table)
choice = Prompt.ask("Select option", default="0")
if choice == "0":
return
try:
from tools.honey_tools import run_honey
run_honey(config, choice)
except ImportError:
console.print("[red]Honey tools module not available.[/red]")
Prompt.ask("\nPress Enter to return to menu")
def run_bend_menu():
"""Bend interaction submenu"""
show_header()
console.print("[bold]Bend - Lending Protocol[/bold]\n", style="green")
config = load_config()
if not config.get("private_key"):
console.print("[red]Error: Private key not configured. Go to Settings first.[/red]")
Prompt.ask("\nPress Enter to return to menu")
return
table = Table(title="Bend Options", box=box.ROUNDED, border_style="magenta")
table.add_column("Key", style="bold")
table.add_column("Action")
table.add_row("1", "Deposit (WETH)")
table.add_row("2", "Borrow (Honey)")
table.add_row("3", "Repay (Honey)")
table.add_row("0", "Back to main menu")
console.print(table)
choice = Prompt.ask("Select option", default="0")
if choice == "0":
return
try:
from tools.bend_tools import run_bend
run_bend(config, choice)
except ImportError:
console.print("[red]Bend tools module not available.[/red]")
Prompt.ask("\nPress Enter to return to menu")
def run_honey_jar():
"""0xHoneyJar mint"""
show_header()
console.print("[bold]0xHoneyJar - Mint (4.2 Honey)[/bold]\n", style="green")
config = load_config()
if not config.get("private_key"):
console.print("[red]Error: Private key not configured. Go to Settings first.[/red]")
Prompt.ask("\nPress Enter to return to menu")
return
try:
from tools.honey_jar import run_mint
run_mint(config)
except ImportError:
console.print("[red]Honey Jar module not available.[/red]")
Prompt.ask("\nPress Enter to return to menu")
def run_bera_name():
"""BeraName domain registration"""
show_header()
console.print("[bold]BeraName - Domain Registration[/bold]\n", style="green")
config = load_config()
if not config.get("private_key"):
console.print("[red]Error: Private key not configured. Go to Settings first.[/red]")
Prompt.ask("\nPress Enter to return to menu")
return
try:
from tools.bera_name import run_register
run_register(config)
except ImportError:
console.print("[red]BeraName module not available.[/red]")
Prompt.ask("\nPress Enter to return to menu")
def open_links_menu():
"""Open BeraChain links in browser"""
show_header()
console.print("[bold]Quick Links[/bold]\n", style="green")
links_table = Table(title="BeraChain Links", box=box.ROUNDED, border_style="blue")
links_table.add_column("#", style="dim")
links_table.add_column("Service", style="cyan")
links_table.add_column("URL", style="white")
links_table.add_row("1", "Faucet", "https://artio.faucet.berachain.com/")
links_table.add_row("2", "BEX", "https://artio.bex.berachain.com/swap")
links_table.add_row("3", "Honey", "https://artio.honey.berachain.com")
links_table.add_row("4", "Bend", "https://artio.bend.berachain.com/")
links_table.add_row("5", "BeraNames", "https://www.beranames.com")
links_table.add_row("6", "Berps", "https://artio.berps.berachain.com/")
links_table.add_row("7", "Station", "https://artio.station.berachain.com/")
links_table.add_row("0", "Back", "-")
console.print(links_table)
choice = Prompt.ask("Open link (1-7) or 0 to back", default="0")
urls = {
"1": "https://artio.faucet.berachain.com/",
"2": "https://artio.bex.berachain.com/swap",
"3": "https://artio.honey.berachain.com",
"4": "https://artio.bend.berachain.com/",
"5": "https://www.beranames.com",
"6": "https://artio.berps.berachain.com/",
"7": "https://artio.station.berachain.com/",
}
if choice in urls:
import webbrowser
webbrowser.open(urls[choice])
console.print(f"[green]Opened {urls[choice]}[/green]")
Prompt.ask("\nPress Enter to return to menu")
@ensure_env
def main_menu():
"""Display main menu and handle selection"""
while True:
show_header()
menu_table = Table(show_header=False, box=box.DOUBLE_EDGE, border_style="bright_green", title="[bold]Main Menu[/bold]")
menu_table.add_column("Key", style="bold green", width=4)
menu_table.add_column("Action", style="white")
menu_table.add_column("Description", style="dim")
menu_table.add_row("1", "Install Dependencies", "Install required Python packages")
menu_table.add_row("2", "Settings", "Configure RPC, API keys, private key")
menu_table.add_row("3", "About", "About BeraChainTools and links")
menu_table.add_row("4", "BeraChain Faucet", "Claim test tokens (captcha required)")
menu_table.add_row("5", "BEX", "DEX swaps and add liquidity")
menu_table.add_row("6", "Honey", "Mint and redeem Honey")
menu_table.add_row("7", "Bend", "Deposit, borrow, repay")
menu_table.add_row("8", "0xHoneyJar", "Mint (4.2 Honey)")
menu_table.add_row("9", "BeraName", "Register domain")
menu_table.add_row("L", "Links", "Open BeraChain services in browser")
menu_table.add_row("0", "Exit", "Exit the application")
console.print(menu_table)
console.print()
choice = Prompt.ask("Select option", default="1").strip().upper()
actions = {
"1": install_dependencies,
"2": settings_menu,
"3": about_menu,
"4": run_bera_faucet,
"5": run_bex_menu,
"6": run_honey_menu,
"7": run_bend_menu,
"8": run_honey_jar,
"9": run_bera_name,
"L": open_links_menu,
}
if choice == "0":
console.print("\n[green]Goodbye! Thank you for using BeraChainTools![/green]")
break
if choice in actions:
actions[choice]()
else:
console.print("[red]Invalid option.[/red]")
Prompt.ask("Press Enter to continue")
if __name__ == "__main__":
try:
main_menu()
except KeyboardInterrupt:
console.print("\n[green]Exiting...[/green]")