-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
223 lines (180 loc) · 8.02 KB
/
main.py
File metadata and controls
223 lines (180 loc) · 8.02 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
#!/usr/bin/env python3
"""
Multi-Pool Arbitrage Bot
Detects and executes arbitrage opportunities across multiple AMM pools
on the Strato blockchain by comparing pool prices with external market prices.
"""
import time
import yaml
import logging
from decimal import Decimal
from core.strato_client import strato_client
from core.constants import WEI_SCALE, BLOCKAPPS_ORACLE_TOKENS
from onchain.pool import Pool
from market.oracle import PriceOracle, get_external_symbol
from engine.arb_executor import ArbitrageExecutor
from engine.liquidation_executor import LiquidationExecutor
from engine.helpers import ensure_pool_approvals
from core.health import start_health_server
from core.notifier import notify_error
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s",
handlers=[logging.StreamHandler()]
)
log = logging.getLogger("arb_bot")
class ArbitrageBot:
def __init__(self, cfg_path="config.yaml"):
with open(cfg_path) as f:
cfg = yaml.safe_load(f)
self.cfg = cfg
self.dry_run = True # Default to dry run for safety
self.interval = cfg.get("execution", {}).get("execution_interval", 10)
self.running = False
self.executors = [] # List of executors, one per pool
self.liquidation_executor = None
def init_components(self):
c = strato_client()
if not c.is_connected():
raise RuntimeError("cannot connect to blockchain")
pools = self.cfg.get("pools", [])
if not pools:
log.info("no pools configured in config.yaml")
else:
# Validate all pool configs
for i, pool_config in enumerate(pools):
pool_addr = pool_config.get("address")
if not pool_addr:
raise RuntimeError(f"Pool {i+1}: address is required in config.yaml")
fee_bps = self.cfg["trading"]["fee_bps"]
oracle_cfg = self.cfg["oracle"]
self.oracle = PriceOracle(
timeout=oracle_cfg["timeout"],
blockapps_price_oracle=oracle_cfg.get("blockapps_price_oracle", "")
)
trade_cfg = self.cfg["trading"]
min_profit = Decimal(str(trade_cfg["min_profit"]))
min_profit_wei = int(min_profit * WEI_SCALE)
slippage_factor_amm = float(trade_cfg.get("slippage_factor_amm", 0.96))
slippage_factor_stable = float(trade_cfg.get("slippage_factor_stable", 0.92))
exec_cfg = self.cfg.get("execution", {})
self.vault_addr = exec_cfg.get("vault_addr", "")
# Initialize executor for each pool
for pool_config in pools:
pool_addr = pool_config.get("address")
pool_fee_bps = int(pool_config.get("fee_bps", fee_bps))
pool_slippage_amm = float(pool_config.get("slippage_factor_amm", slippage_factor_amm))
pool_slippage_stable = float(pool_config.get("slippage_factor_stable", slippage_factor_stable))
pool = Pool(
pool_addr,
fee_bps=pool_fee_bps,
slippage_factor_amm=pool_slippage_amm,
slippage_factor_stable=pool_slippage_stable,
)
pool.fetch_pool_data()
# Auto-register BlockApps tokens based on token names
# These use the on-chain BlockApps PriceOracle instead of Alchemy
for token in pool.coins:
external_symbol = get_external_symbol(token.symbol)
if external_symbol in BLOCKAPPS_ORACLE_TOKENS:
self.oracle.register_blockapps_token(external_symbol, token.address)
executor = ArbitrageExecutor(
token_a=pool.token_a,
token_b=pool.token_b,
pool=pool,
oracle=self.oracle,
fee_bps=pool_fee_bps,
min_profit_usd=min_profit_wei,
)
# Ensure pool approvals (token_a/token_b + any extra coins in n>2 pools)
ensure_pool_approvals(pool.token_a, pool.token_b, pool, self.vault_addr)
for extra in pool.coins[2:]:
ensure_pool_approvals(extra, extra, pool, self.vault_addr)
self.executors.append(executor)
log.info(
f"initialized {pool.token_a.symbol}-{pool.token_b.symbol} pool at {pool_addr} "
f"(isStable={pool.is_stable_pool()}, source=on-chain BlockApps-Pool.isStable)"
)
# Pre-fetch prices for all tokens in all pools
all_token_symbols = set()
for executor in self.executors:
for coin in executor.pool.coins:
all_token_symbols.add(get_external_symbol(coin.symbol))
if all_token_symbols:
self.oracle.fetch_all_prices(list(all_token_symbols), force_refresh=True)
# Initialize liquidation executor if enabled
liq_cfg = self.cfg.get("liquidation", {})
if liq_cfg.get("enabled", False):
self.liquidation_executor = LiquidationExecutor()
log.info("CDP liquidation scanning enabled")
if self.dry_run:
log.info("dry-run mode enabled")
def scan_once(self):
"""Scan all pools for arbitrage and CDP positions for liquidation"""
any_executed = False
# --- Arbitrage scan (all pools every cycle) ---
for i, executor in enumerate(self.executors):
opp = executor.scan_for_opportunity()
if i < len(self.executors) - 1:
print()
if not opp:
continue
pool_label = f"{executor.pool.token_a.symbol}-{executor.pool.token_b.symbol}"
if self.dry_run:
log.info(f"dry-run: would execute trade on {pool_label} pool")
any_executed = True
continue
res = executor.execute_opportunity(opp)
log.info(f"exec result on {pool_label}: {res.success}")
if res.success:
any_executed = True
# --- Liquidation scan ---
if self.liquidation_executor:
print()
opportunities = self.liquidation_executor.scan_for_opportunities()
for opp in opportunities:
if self.dry_run:
log.info(
f"dry-run: would liquidate borrower={opp.borrower[:16]}… "
f"collateral={opp.collateral_asset[:16]}… "
f"debt={opp.debt_to_cover}"
)
continue
res = self.liquidation_executor.execute_liquidation(opp)
if res.success:
log.info(f"liquidation succeeded for borrower={opp.borrower[:16]}…")
else:
msg = f"liquidation failed for borrower={opp.borrower[:16]}…: {res.error_message}"
log.warning(msg)
notify_error(msg)
return any_executed
def run(self):
self.running = True
log.info(f"starting loop interval={self.interval}s")
while self.running:
try:
self.scan_once()
print("\n\n\n") # 3 newlines after every scan
time.sleep(self.interval)
except KeyboardInterrupt:
log.info("stopping...")
self.running = False
except Exception as e:
log.error(f"loop error: {e}")
notify_error(f"loop error: {e}")
print("\n\n\n") # 3 newlines even on error
time.sleep(self.interval)
def main():
import argparse
p = argparse.ArgumentParser()
p.add_argument("-c", "--config", default="config.yaml")
p.add_argument("--live", action="store_true")
a = p.parse_args()
bot = ArbitrageBot(a.config)
bot.dry_run = not a.live
bot.init_components()
health_port = bot.cfg.get("health_port", 8080)
start_health_server(bot, port=health_port)
bot.run()
if __name__ == "__main__":
main()