-
Notifications
You must be signed in to change notification settings - Fork 360
Expand file tree
/
Copy pathconfig.py
More file actions
75 lines (62 loc) · 2.32 KB
/
config.py
File metadata and controls
75 lines (62 loc) · 2.32 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
# -*- coding: utf-8 -*-
"""
BeraChainTools configuration management.
Provides typed access to config.json fields with validation and defaults.
"""
import json
from pathlib import Path
from dataclasses import dataclass, field, asdict
from typing import Optional
CONFIG_PATH = Path(__file__).parent / "config.json"
SUPPORTED_SOLVERS = ("yescaptcha", "2captcha")
DEFAULT_RPC = "https://rpc.ankr.com/berachain_testnet"
KNOWN_TOKEN_ADDRESSES = {
"BERA": "0x0000000000000000000000000000000000000000",
"WBERA": "0x5806E416dA447b267cEA759358cF22Cc41DAE3F0",
"USDC": "0xd6D83aF58a19Cd14eF3CF6fe848C9A4d21e5727c",
"WETH": "0xE28AfD8c634946833E89ee3F122C06d7C537E8A8",
"HONEY": "0x0E4aaF1351de4c0264C5c7056Ef3777b41BD8e03",
}
@dataclass
class BeraConfig:
rpc_url: str = DEFAULT_RPC
client_key: str = ""
solver_provider: str = "yescaptcha"
private_key: str = ""
use_proxy: bool = False
proxy_http: str = "http://127.0.0.1:8888"
proxy_https: str = "http://127.0.0.1:8888"
def validate(self) -> list:
errors = []
if self.solver_provider not in SUPPORTED_SOLVERS:
errors.append(f"solver_provider must be one of {SUPPORTED_SOLVERS}")
if self.use_proxy and not self.proxy_http:
errors.append("proxy_http required when use_proxy is True")
if self.private_key and not self.private_key.startswith("0x"):
errors.append("private_key should start with 0x")
return errors
@property
def proxies(self) -> Optional[dict]:
if not self.use_proxy:
return None
return {
"http": self.proxy_http,
"https": self.proxy_https,
}
def load_config() -> BeraConfig:
defaults = BeraConfig()
try:
if CONFIG_PATH.exists():
with open(CONFIG_PATH, "r", encoding="utf-8") as f:
data = json.load(f)
return BeraConfig(**{k: data[k] for k in data if k in defaults.__dict__})
except (json.JSONDecodeError, TypeError, KeyError):
pass
return defaults
def save_config(cfg: BeraConfig) -> bool:
try:
with open(CONFIG_PATH, "w", encoding="utf-8") as f:
json.dump(asdict(cfg), f, indent=2)
return True
except OSError:
return False