Skip to content

Commit

Permalink
Make leg formatting generic.
Browse files Browse the repository at this point in the history
  • Loading branch information
dowlandaiello committed May 16, 2024
1 parent 5d5e5fc commit bcf4be0
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 23 deletions.
1 change: 1 addition & 0 deletions src/contracts/auction.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def __init__(
self.chain_id = contract_info.clients[0].query_chain_id()
self.chain_prefix = "neutron"
self.chain_fee_denom = "untrn"
self.kind = "auction"

def exchange_rate(self) -> int:
"""
Expand Down
1 change: 1 addition & 0 deletions src/contracts/pool/astroport.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ def __init__(
self.chain_id = contract_info.clients[0].query_chain_id()
self.chain_prefix = "neutron"
self.chain_fee_denom = "untrn"
self.kind = "astroport"

def __exchange_rate(
self, asset_a: Token | NativeToken, asset_b: Token | NativeToken, amount: int
Expand Down
1 change: 1 addition & 0 deletions src/contracts/pool/osmosis.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def __init__(
self.chain_id = "osmosis-1"
self.chain_prefix = "osmo"
self.chain_fee_denom = "uosmo"
self.kind = "osmosis"

self.endpoints = endpoints["http"]
self.grpc_endpoints = endpoints["grpc"]
Expand Down
55 changes: 32 additions & 23 deletions src/strategies/naive.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
of hops using all available providers.
"""

import multiprocessing
import threading
from queue import Queue
from decimal import Decimal
Expand Down Expand Up @@ -293,7 +292,7 @@ def profit_arb(i: int, route: List[Union[PoolProvider, AuctionProvider]]) -> Non
# Calculate profitability of all routes, and execute
# profitable ones
for i, route in enumerate(ctx.state.routes):
workers.append(multiprocessing.Process(target=profit_arb, args=[i, route]))
workers.append(threading.Thread(target=profit_arb, args=[i, route]))
workers[-1].start()

while True:
Expand Down Expand Up @@ -323,7 +322,7 @@ def fmt_route_leg(leg: Union[PoolProvider, AuctionProvider]) -> str:
if isinstance(leg, AuctionProvider):
return "valence"

return "unknown pool"
return leg.kind


def exec_arb(
Expand Down Expand Up @@ -641,17 +640,17 @@ def get_routes_with_depth_limit_dfs(
auctions: dict[str, dict[str, AuctionProvider]],
) -> List[List[Union[PoolProvider, AuctionProvider]]]:
denom_cache: dict[str, dict[str, str]] = {}
routes = []

def next_legs(
path: list[Union[PoolProvider, AuctionProvider]]
) -> list[list[Union[PoolProvider, AuctionProvider]]]:
def next_legs(path: list[Union[PoolProvider, AuctionProvider]]) -> None:
nonlocal denom_cache
nonlocal limit
nonlocal routes

# Only find `limit` pools
# with a depth less than `depth
if limit < 0 or len(path) > depth:
return []
if len(routes) == limit or len(path) > depth:
return

# We must be finding the next leg
# in an already existing path
Expand All @@ -664,12 +663,16 @@ def next_legs(
# of the denoms match the starting denom, we are
# finished, and the circuit is closed
if len(path) > 1 and src in {path[-1].asset_a(), path[-1].asset_b()}:
if len(required_leg_types - set((fmt_route_leg(leg) for leg in path))) > 0:
return []
if (
len(required_leg_types - set((fmt_route_leg(leg) for leg in path))) > 0
or len(path) < depth
):
return

limit -= 1
if len(routes) < limit and path not in routes:
routes.append(path)

return [path]
return

# Find all pools that start where this leg ends
# the "ending" point of this leg is defined
Expand Down Expand Up @@ -740,7 +743,7 @@ def next_legs(
denoms_prev[0] in denoms_prev_prev
and denoms_prev[1] in denoms_prev_prev
):
return []
return

# And that denom is the end
end = (
Expand Down Expand Up @@ -801,19 +804,25 @@ def next_legs(
),
]

return [
for pool in next_pools:
if (
pool in path
or len(
{pool.asset_a(), pool.asset_b()}
^ {prev_pool.asset_a(), prev_pool.asset_b()}
)
<= 0
):
continue

next_legs(path + [pool])
for pool in next_pools
if pool not in path
and len(
{pool.asset_a(), pool.asset_b()}
^ {prev_pool.asset_a(), prev_pool.asset_b()}
)
> 0
]

start_pools = [
*auctions.get(src, {}).values(),
*(pool for pool_set in pools.get(src, {}).values() for pool in pool_set),
]
return [next_legs([pool]) for pool in start_pools]

for pool in start_pools:
next_legs([pool])

return routes[:limit]

0 comments on commit bcf4be0

Please sign in to comment.