Skip to content

Commit

Permalink
Fix price construction
Browse files Browse the repository at this point in the history
  • Loading branch information
barak manos committed May 28, 2024
1 parent 5fecd1e commit 7c58f59
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
12 changes: 6 additions & 6 deletions fastlane_bot/modes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ def __init__(self, flashloan_tokens, CCm, ConfigObj):
["bancor_v2", "bancor_v3"]
+ ConfigObj.UNI_V2_FORKS
+ ConfigObj.UNI_V3_FORKS
+ ConfigObj.CARBON_V1_FORKS
)
}

Expand Down Expand Up @@ -58,9 +57,9 @@ def get_profit(self, src_token: str, optimization, trade_instructions_df):

def calculate_profit(self, src_token: str, src_profit: float) -> Decimal:
if src_token not in [self.ConfigObj.NATIVE_GAS_TOKEN_ADDRESS, self.ConfigObj.WRAPPED_GAS_TOKEN_ADDRESS]:
items = get_items(self.CCm, self.sort_order, self.ConfigObj.WRAPPED_GAS_TOKEN_ADDRESS, src_token)
assert len(items) > 0, f"Failed to get conversion rate for {src_token} and {self.ConfigObj.WRAPPED_GAS_TOKEN_ADDRESS}"
return Decimal(str(src_profit)) / Decimal(str(items[0]["price"]))
price = get_reliable_price(self.CCm, self.sort_order, self.ConfigObj.WRAPPED_GAS_TOKEN_ADDRESS, src_token)
assert price is not None, f"Failed to get conversion rate for {src_token} and {self.ConfigObj.WRAPPED_GAS_TOKEN_ADDRESS}"
return Decimal(str(src_profit)) / Decimal(str(price))
return Decimal(str(src_profit))

def is_net_change_small(trade_instructions_df) -> bool:
Expand All @@ -69,7 +68,8 @@ def is_net_change_small(trade_instructions_df) -> bool:
except Exception:
return False

def get_items(CCm, sort_order, dst_token, src_token):
def get_reliable_price(CCm, sort_order, dst_token, src_token):
list1 = [{"exchange": curve.params.exchange, "price": curve.p / 1} for curve in CCm.bytknx(dst_token).bytkny(src_token).curves]
list2 = [{"exchange": curve.params.exchange, "price": 1 / curve.p} for curve in CCm.bytknx(src_token).bytkny(dst_token).curves]
return sorted([item for item in list1 + list2 if item["exchange"] in sort_order], key=lambda item: sort_order[item["exchange"]])
items = sorted(list1 + list2, key = lambda item: sort_order.get(item["exchange"], float('inf')))
return items[0]["price"] if len(items) > 0 else None
17 changes: 9 additions & 8 deletions fastlane_bot/modes/base_triangle.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,15 @@ def find_arbitrage(self) -> Dict[List[Any], List[Any]]:

def get_params(container, src_token):
pstart = {src_token: 1}
for dst_token in [token for token in container.tokens() if token != src_token]:
curves = container.bytknx(dst_token).bytkny(src_token).curves
if len(curves) > 0:
pstart[dst_token] = curves[0].p
else:
curves = container.bytknx(src_token).bytkny(dst_token).curves
for dst_token in container.tokens():
if dst_token != src_token:
curves = container.bytknx(dst_token).bytkny(src_token).curves
if len(curves) > 0:
pstart[dst_token] = 1 / curves[0].p
pstart[dst_token] = curves[0].p
else:
return None
curves = container.bytknx(src_token).bytkny(dst_token).curves
if len(curves) > 0:
pstart[dst_token] = 1 / curves[0].p
else:
return None
return {"pstart": pstart}

0 comments on commit 7c58f59

Please sign in to comment.