Skip to content

Commit

Permalink
Extend the test for function _carbon_to_cpc_dict
Browse files Browse the repository at this point in the history
  • Loading branch information
barak manos committed Jun 3, 2024
1 parent 51d9064 commit 8d071bb
Show file tree
Hide file tree
Showing 3 changed files with 2,158 additions and 1,086 deletions.
39 changes: 23 additions & 16 deletions fastlane_bot/helpers/poolandtokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,11 +420,11 @@ def _carbon_to_cpc(self) -> ConstantProductCurve:
"""

cpc_list = []
for typed_args in self._carbon_to_cpc_dict()["strategy_typed_args"]:
for order in self._carbon_to_cpc_dict()["strategy_orders"]:
try:
cpc_list.append(ConstantProductCurve.from_carbon(**self._convert_to_float(typed_args)))
cpc_list.append(ConstantProductCurve.from_carbon(**self._convert_to_float(order)))
except Exception as e:
self.ConfigObj.logger.debug(f"[_carbon_to_cpc] curve {typed_args} error {e}")
self.ConfigObj.logger.debug(f"[_carbon_to_cpc] curve {order} error {e}")
return cpc_list

def _carbon_to_cpc_dict(self) -> Tuple[Dict, bool]:
Expand Down Expand Up @@ -459,7 +459,7 @@ def _carbon_to_cpc_dict(self) -> Tuple[Dict, bool]:
pair = self.pair_name.replace(self.ConfigObj.NATIVE_GAS_TOKEN_ADDRESS, self.ConfigObj.WRAPPED_GAS_TOKEN_ADDRESS)
tokens = pair.split("/")

strategy_typed_args = [
strategy_orders = [
{
"cid": f"{self.cid}-{i}" if is_carbon else self.cid,
"yint": Decimal(encoded_orders[i]["z"]) / decimals[i],
Expand All @@ -475,40 +475,47 @@ def _carbon_to_cpc_dict(self) -> Tuple[Dict, bool]:
for i in [0, 1] if encoded_orders[i]["y"] > 0 and encoded_orders[i]["B"] > 0
]

pm_within_range = False
no_limit_orders = False
prices_overlap = False

# Only overlapping strategies are selected for modification
if len(strategy_typed_args) == 2:
if len(strategy_orders) == 2:
p_marg_0 = (decoded_orders[0]["marginalRate"] * converters[0]) ** -1
p_marg_1 = (decoded_orders[1]["marginalRate"] * converters[1]) ** +1

# check if the marginal prices are within a 1% threshold (may included stable/stable pairs)
percent_component_met = abs(p_marg_0 - p_marg_1) <= max(p_marg_0, p_marg_1) / 100
pm_within_range = abs(p_marg_0 - p_marg_1) <= max(p_marg_0, p_marg_1) / 100

# verify that the strategy does not consist of any limit orders
no_limit_orders = not any(encoded_order["A"] == 0 for encoded_order in encoded_orders)

# check if the price boundaries pa/pb overlap at one end
min0, max0 = sorted([strategy_typed_args[0]["pa"] ** +1, strategy_typed_args[0]["pb"] ** +1])
min1, max1 = sorted([strategy_typed_args[1]["pa"] ** -1, strategy_typed_args[1]["pb"] ** -1])
min0, max0 = sorted([strategy_orders[0]["pa"] ** +1, strategy_orders[0]["pb"] ** +1])
min1, max1 = sorted([strategy_orders[1]["pa"] ** -1, strategy_orders[1]["pb"] ** -1])
prices_overlap = max(min0, min1) < min(max0, max1)

# if all conditions are met then this is likely an overlapping strategy
if percent_component_met and no_limit_orders and prices_overlap:
if pm_within_range and no_limit_orders and prices_overlap:
# calculate the geometric mean
pm = 1 / (p_marg_0 * p_marg_1).sqrt()
# modify the y_int based on the new geomean to the limit of y
for typed_args in strategy_typed_args:
for order in strategy_orders:
yint = encodeOrder({
"liquidity": typed_args["y"],
"lowestRate": typed_args["pb"],
"highestRate": typed_args["pa"],
"liquidity": order["y"],
"lowestRate": order["pb"],
"highestRate": order["pa"],
"marginalRate": pm,
})["z"]
if typed_args["yint"] < yint:
typed_args["yint"] = yint
if order["yint"] < yint:
order["yint"] = yint

return {"strategy_typed_args": strategy_typed_args, "prices_overlap": prices_overlap}
return {
"strategy_orders": strategy_orders,
"pm_within_range": pm_within_range,
"no_limit_orders": no_limit_orders,
"prices_overlap": prices_overlap,
}

def _univ3_to_cpc(self) -> List[Any]:
"""
Expand Down
Loading

0 comments on commit 8d071bb

Please sign in to comment.