-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscheduler.py
459 lines (367 loc) · 13.7 KB
/
scheduler.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
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
"""
Implements a strategy runner with an arbitrary provider set in an event-loop style.
"""
import logging
from datetime import datetime
import json
from typing import Callable, List, Self, Optional, Awaitable, Any, TypeVar, Generic
from dataclasses import dataclass
from cosmpy.aerial.client import LedgerClient
from cosmpy.crypto.address import Address
from cosmpy.aerial.wallet import LocalWallet
from src.contracts.auction import AuctionDirectory, AuctionProvider
from src.contracts.route import Route, load_route, LegRepr, Status, Leg
from src.contracts.pool.provider import PoolProvider
from src.util import (
try_multiple_clients,
DenomRouteLeg,
DenomRouteQuery,
ChainInfo,
DenomChainInfo,
)
import aiohttp
import grpc
logger = logging.getLogger(__name__)
MAX_ROUTE_HISTORY_LEN = 200000
# Length to truncate denoms in balance logs to
DENOM_BALANCE_PREFIX_MAX_DENOM_LEN = 12
TState = TypeVar("TState")
@dataclass
class Ctx(Generic[TState]):
"""
Information about the scheduling environment including:
- User configuration via flags
- User state
"""
clients: dict[str, list[LedgerClient]]
endpoints: dict[str, dict[str, list[str]]]
wallet: LocalWallet
cli_args: dict[str, Any]
state: Optional[TState]
terminated: bool
http_session: aiohttp.ClientSession
order_history: list[Route]
deployments: dict[str, Any]
denom_map: dict[str, list[DenomChainInfo]]
denom_routes: dict[DenomRouteQuery, list[DenomRouteLeg]]
chain_info: dict[str, ChainInfo]
def with_state(self, state: Any) -> Self:
"""
Constructs a new context with the given state,
modifying the current context.
"""
self.state = state
return self
def commit_history(self) -> Self:
"""
Commits the order history to disk.
"""
with open(self.cli_args["history_file"], "w", encoding="utf-8") as f:
f.seek(0)
json.dump([order.dumps() for order in self.order_history], f)
return self
def recover_history(self) -> Self:
"""
Retrieves the order history from disk
"""
with open(self.cli_args["history_file"], "r", encoding="utf-8") as f:
f.seek(0)
self.order_history = [
load_route(json_route) for json_route in json.load(f)
][:-MAX_ROUTE_HISTORY_LEN]
return self
def cancel(self) -> Self:
"""
Marks the event loop for termination.
"""
self.terminated = True
return self
def queue_route(
self,
route: list[Leg],
theoretical_profit: int,
expected_profit: int,
quantities: list[int],
) -> Route:
"""
Creates a new identified route, inserting it into the order history,
and returning it for later updating.
"""
r = Route(
len(self.order_history),
[
LegRepr(leg.in_asset(), leg.out_asset(), leg.backend.kind, False)
for leg in route
],
route,
theoretical_profit,
expected_profit,
None,
quantities,
Status.QUEUED,
datetime.now().strftime("%Y-%m-%d @ %H:%M:%S"),
[],
)
self.order_history.append(r)
return r
def update_route(self, route: Route) -> None:
"""
Updates the route in the scheduler.
"""
if route.uid >= len(self.order_history) or route.uid < 0:
return
self.order_history[route.uid] = route
def log_route(
self, route: Route, log_level: str, fmt_string: str, args: list[Any]
) -> None:
"""
Writes a log to the standard logger and to the log file of a route.
"""
def asset_balance_prefix(leg: Leg, asset: str) -> Optional[str]:
balance_resp_asset = try_multiple_clients(
self.clients[leg.backend.chain_id],
lambda client: client.query_bank_balance(
Address(
self.wallet.public_key(),
prefix=leg.backend.chain_prefix,
),
asset,
),
)
if not balance_resp_asset:
return None
return f"balance[{leg.backend.chain_id}]({asset[:DENOM_BALANCE_PREFIX_MAX_DENOM_LEN]}): {balance_resp_asset}"
def leg_balance_prefixes(leg: Leg) -> list[str]:
assets = [leg.in_asset(), leg.out_asset()]
return [
x for x in (asset_balance_prefix(leg, asset) for asset in assets) if x
]
prefix = " ".join(
{
prefix
for leg_prefixes in [leg_balance_prefixes(leg) for leg in route.legs]
for prefix in leg_prefixes
}
)
route.logs.append(f"{log_level.upper()} {prefix} {fmt_string % tuple(args)}")
if route.uid >= len(self.order_history) or route.uid < 0:
return
self.order_history[route.uid] = route
fmt_string = f"{prefix} %s- {fmt_string}"
if log_level == "info":
logger.info(fmt_string, str(route), *args)
return
if log_level == "error":
logger.error(fmt_string, str(route), *args)
return
if log_level == "debug":
logger.debug(fmt_string, str(route), *args)
async def query_denom_route(
self, query: DenomRouteQuery
) -> Optional[list[DenomRouteLeg]]:
if self.denom_routes and query in self.denom_routes:
return self.denom_routes[query]
head = {"accept": "application/json", "content-type": "application/json"}
async with self.http_session.post(
"https://api.skip.money/v2/fungible/route",
headers=head,
json={
"amount_in": "1",
"source_asset_denom": query.src_denom,
"source_asset_chain_id": query.src_chain,
"dest_asset_denom": query.dest_denom,
"dest_asset_chain_id": query.dest_chain,
"allow_multi_tx": True,
"allow_unsafe": False,
"bridges": ["IBC"],
},
) as resp:
if resp.status != 200:
return None
ops = (await resp.json())["operations"]
# The transfer includes a swap or some other operation
# we can't handle
if any(("transfer" not in op for op in ops)):
return None
transfer_info = ops[0]["transfer"]
from_chain_info = await self.query_chain_info(
transfer_info["from_chain_id"]
)
to_chain_info = await self.query_chain_info(transfer_info["to_chain_id"])
if not from_chain_info or not to_chain_info:
return None
route = [
DenomRouteLeg(
src_chain=query.src_chain,
dest_chain=query.dest_chain,
src_denom=query.src_denom,
dest_denom=query.dest_denom,
from_chain=from_chain_info,
to_chain=to_chain_info,
denom_in=transfer_info["denom_in"],
denom_out=transfer_info["denom_out"],
port=transfer_info["port"],
channel=transfer_info["channel"],
)
for op in ops
]
self.denom_routes[query] = route
return route
async def query_chain_info(
self,
chain_id: str,
) -> Optional[ChainInfo]:
"""
Gets basic information about a cosmos chain.
"""
if chain_id in self.chain_info:
return self.chain_info[chain_id]
head = {"accept": "application/json", "content-type": "application/json"}
async with self.http_session.get(
f"https://api.skip.money/v2/info/chains?chain_ids={chain_id}",
headers=head,
) as resp:
if resp.status != 200:
return None
chains = (await resp.json())["chains"]
if len(chains) == 0:
return None
chain = chains[0]
chain_info = ChainInfo(
chain_name=chain["chain_name"],
chain_id=chain["chain_id"],
pfm_enabled=chain["pfm_enabled"],
supports_memo=chain["supports_memo"],
bech32_prefix=chain["bech32_prefix"],
fee_asset=chain["fee_assets"][0]["denom"],
chain_type=chain["chain_type"],
pretty_name=chain["pretty_name"],
)
self.chain_info[chain_id] = chain_info
return chain_info
async def query_denom_info_on_chain(
self,
src_chain: str,
src_denom: str,
dest_chain: str,
) -> Optional[DenomChainInfo]:
"""
Gets a neutron denom's denom and channel on/to another chain.
"""
infos = await self.query_denom_info(src_chain, src_denom)
return next((info for info in infos if info.dest_chain_id == dest_chain))
async def query_denom_info(
self,
src_chain: str,
src_denom: str,
) -> list[DenomChainInfo]:
"""
Gets a denom's denom and channel on/to other chains.
"""
if src_denom in self.denom_map:
return self.denom_map[src_denom]
head = {"accept": "application/json", "content-type": "application/json"}
async with self.http_session.post(
"https://api.skip.money/v1/fungible/assets_from_source",
headers=head,
json={
"allow_multi_tx": False,
"include_cw20_assets": True,
"source_asset_denom": src_denom,
"source_asset_chain_id": src_chain,
"client_id": "timewave-arb-bot",
},
) as resp:
if resp.status != 200:
return []
dests = (await resp.json())["dest_assets"]
def chain_info(chain_id: str, info: dict[str, Any]) -> DenomChainInfo:
info = info["assets"][0]
return DenomChainInfo(
src_chain_id=src_chain, denom=info["denom"], dest_chain_id=chain_id
)
return [chain_info(chain_id, info) for chain_id, info in dests.items()]
class Scheduler(Generic[TState]):
"""
A registry of pricing providers for different assets,
which can be polled alongside a strategy function which
may interact with registered providers.
"""
ctx: Ctx[TState]
strategy: Callable[
[
Ctx[TState],
dict[str, dict[str, List[PoolProvider]]],
dict[str, dict[str, AuctionProvider]],
],
Awaitable[Ctx[TState]],
]
providers: dict[str, dict[str, List[PoolProvider]]]
auction_manager: AuctionDirectory
auctions: dict[str, dict[str, AuctionProvider]]
def __init__(
self,
ctx: Ctx[TState],
strategy: Callable[
[
Ctx[TState],
dict[str, dict[str, List[PoolProvider]]],
dict[str, dict[str, AuctionProvider]],
],
Awaitable[Ctx[TState]],
],
) -> None:
"""
Initializes the scheduler with some initial context,
a strategy function to poll,
and no providers except available auctions.
"""
self.ctx = ctx
self.strategy = strategy
self.providers: dict[str, dict[str, List[PoolProvider]]] = {}
self.auction_manager = AuctionDirectory(
self.ctx.deployments,
ctx.http_session,
[
(
grpc.aio.secure_channel(
endpoint.split("grpc+https://")[1],
grpc.ssl_channel_credentials(),
)
if "https" in endpoint
else grpc.aio.insecure_channel(
endpoint.split("grpc+http://")[1],
)
)
for endpoint in ctx.endpoints[
list(self.ctx.deployments["auctions"].keys())[0]
]["grpc"]
],
endpoints=ctx.endpoints[list(self.ctx.deployments["auctions"].keys())[0]],
poolfile_path=ctx.cli_args["pool_file"],
)
self.auctions = {}
async def register_auctions(self) -> None:
"""
Registers all auctions into the pool provider.
"""
self.auctions = await self.auction_manager.auctions()
def register_provider(self, provider: PoolProvider) -> None:
"""
Registers a pool provider, enqueing it to future strategy function polls.
"""
if provider.asset_a() not in self.providers:
self.providers[provider.asset_a()] = {}
if provider.asset_b() not in self.providers:
self.providers[provider.asset_b()] = {}
if provider.asset_b() not in self.providers[provider.asset_a()]:
self.providers[provider.asset_a()][provider.asset_b()] = []
if provider.asset_a() not in self.providers[provider.asset_b()]:
self.providers[provider.asset_b()][provider.asset_a()] = []
self.providers[provider.asset_a()][provider.asset_b()].append(provider)
self.providers[provider.asset_b()][provider.asset_a()].append(provider)
async def poll(self) -> None:
"""
Polls the strategy function with all registered providers.
"""
self.ctx = await self.strategy(self.ctx, self.providers, self.auctions)