Skip to content

Commit

Permalink
Remove unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
Lesigh-3100 committed Feb 28, 2024
1 parent 12865ab commit 60d008d
Showing 1 changed file with 0 additions and 162 deletions.
162 changes: 0 additions & 162 deletions fastlane_bot/helpers/routehandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,102 +438,6 @@ def add_wrap_or_unwrap_trades_to_route(self, trade_instructions: List[TradeInstr
source_amount=0)))
return new_route_struct

def add_wrap_or_unwrap_trades_to_route_v4(self, trade_instructions: List[TradeInstruction],
route_struct: List[Dict], flashloan_struct: List
) -> List[Dict]:
"""
Adds native token wrap and unwrap trades as necessary to the route struct.
:param trade_instructions: the processed trade instructions
:param route_struct: the processed route struct
:param flashloan_struct: the processed route struct
returns: List of RouteStruct
"""

flashloan_native_gas_token = False
flashloan_wrapped_gas_token = False

balance_tracker = {}
# Check if we are flashloaning wrapped or native gas tokens
for flash in flashloan_struct:
for idx, tkn in enumerate(flash["sourceTokens"]):
balance_tracker[tkn] = flash["sourceAmounts"][idx]
if tkn == self.ConfigObj.NATIVE_GAS_TOKEN_ADDRESS:
flashloan_native_gas_token = True
elif tkn == self.ConfigObj.WRAPPED_GAS_TOKEN_ADDRESS:
flashloan_wrapped_gas_token = True

# Ensure we didn't try to flashloan wrapped & native gas token
assert not flashloan_wrapped_gas_token or not flashloan_native_gas_token, f"Cannot flashloan both wrapped & native gas tokens! Flashloan struct = {flashloan_struct}"

segmented_routes = {}
segmented_routes_in_order = []
new_route_struct = []
deadline = route_struct[0]["deadline"]

def init_tkn_to_zero(_tkn):
balance_tracker[_tkn] = balance_tracker.get(_tkn, 0)

for idx, route in enumerate(route_struct):
real_tknin = trade_instructions[idx].get_real_tkn_in
real_tknout = trade_instructions[idx].get_real_tkn_out
exchange_name = trade_instructions[idx].exchange_name
init_tkn_to_zero(real_tknin)
init_tkn_to_zero(real_tknout)
pair = f"{real_tknin}/{real_tknout}"
if pair not in segmented_routes:
segmented_routes_in_order.append(pair)
segmented_routes[pair] = {}

segmented_routes[pair] = {
"amt_out": segmented_routes[pair].get("amt_out", 0) + trade_instructions[idx].amtout_wei,
"amt_in": segmented_routes[pair].get("amt_in", 0) + trade_instructions[idx].amtin_wei,
"token_in": real_tknin,
"token_out": real_tknout,
"trades": {**segmented_routes.get(pair, {}).get("trades", {}), idx: exchange_name} # Merge with existing trades
}

def add_subtract_wrapped_native(wrap: bool, amt):
__tkn_in = self.ConfigObj.NATIVE_GAS_TOKEN_ADDRESS if wrap else self.ConfigObj.WRAPPED_GAS_TOKEN_ADDRESS
__tkn_out = self.ConfigObj.NATIVE_GAS_TOKEN_ADDRESS if not wrap else self.ConfigObj.WRAPPED_GAS_TOKEN_ADDRESS
balance_tracker[__tkn_in] -= amt
balance_tracker[__tkn_out] += amt

for idx, segment in enumerate(segmented_routes_in_order):
_amt_in = segmented_routes[segment]["amt_in"]
_tkn_in = segmented_routes[segment]["token_in"]
_tkn_out = segmented_routes[segment]["token_out"]
if _amt_in > balance_tracker[_tkn_in] and _tkn_in in [self.ConfigObj.WRAPPED_GAS_TOKEN_ADDRESS, self.ConfigObj.NATIVE_GAS_TOKEN_ADDRESS]:
_wrap = _tkn_in == self.ConfigObj.WRAPPED_GAS_TOKEN_ADDRESS
new_route_struct.append(asdict(self.get_wrap_or_unwrap_native_gas_tkn_struct(deadline=deadline, wrap=_wrap, source_amount=_amt_in)))
add_subtract_wrapped_native(wrap=_wrap, amt=_amt_in)
wrap_unwrap = "wrap" if _wrap else "unwrap"
for trade_idx in segmented_routes[segment]["trades"].keys():
exchange_name = trade_instructions[int(trade_idx)].exchange_name
if exchange_name in self.ConfigObj.CARBON_V1_FORKS:
new_route_struct.append(route_struct[int(trade_idx)])
for trade_idx in segmented_routes[segment]["trades"].keys():
exchange_name = trade_instructions[int(trade_idx)].exchange_name
if exchange_name not in self.ConfigObj.CARBON_V1_FORKS:
new_route_struct.append(route_struct[int(trade_idx)])

balance_tracker[_tkn_in] -= segmented_routes[segmented_routes_in_order[idx]]["amt_in"]
balance_tracker[_tkn_out] += segmented_routes[segmented_routes_in_order[idx]]["amt_out"]
if idx == len(segmented_routes_in_order) - 1:

if flashloan_native_gas_token:
if balance_tracker.get(self.ConfigObj.WRAPPED_GAS_TOKEN_ADDRESS, 0) > 0:
new_route_struct.append(
asdict(self.get_wrap_or_unwrap_native_gas_tkn_struct(deadline=deadline,
wrap=False,
source_amount=0)))
elif flashloan_wrapped_gas_token:
if balance_tracker.get(self.ConfigObj.NATIVE_GAS_TOKEN_ADDRESS, 0) > 0:
new_route_struct.append(
asdict(self.get_wrap_or_unwrap_native_gas_tkn_struct(deadline=deadline,
wrap=True,
source_amount=0)))
return new_route_struct

def get_route_structs(
self, trade_instructions: List[TradeInstruction] = None, deadline: int = None
Expand Down Expand Up @@ -753,72 +657,6 @@ def _extract_flashloan_tokens(self, trade_instructions: List[TradeInstruction])

return flash_tokens

def split_carbon_trades(self, trade_instructions: List[TradeInstruction]) -> List[TradeInstruction]:
"""
Split Carbon trades in which some trades are native gas token & others are wrapped gas token, or trades are on different Carbon exchanges.
For example:
If a trade includes Carbon trades which include a mix of wrapped/native gas token, or different Carbon deployments, this function will split them all into distinct trades.
returns: List[TradeInstruction]
"""
new_trade_list = []
for idx, trade in enumerate(trade_instructions):
if trade.exchange_name not in trade.ConfigObj.CARBON_V1_FORKS:
new_trade_list.append(trade)
continue

if trade.tknin_address not in [trade.ConfigObj.NATIVE_GAS_TOKEN_ADDRESS, trade.ConfigObj.WRAPPED_GAS_TOKEN_ADDRESS] and trade.tknout_address not in [trade.ConfigObj.NATIVE_GAS_TOKEN_ADDRESS, trade.ConfigObj.WRAPPED_GAS_TOKEN_ADDRESS]:
new_trade_list.append(trade)
continue

carbon_exchanges = {}

for _tx in eval(trade.raw_txs):
cid = _tx["cid"]
curve = trade.db.get_pool(cid=str(cid).split('-')[0])

if curve.exchange_name not in carbon_exchanges:
carbon_exchanges[curve.exchange_name] = {"native_raw_txs": [], "wrapped_raw_txs": [], "native_amt_in": 0, "native_amt_out": 0, "native_amtin_wei": 0, "native_amtout_wei": 0, "wrapped_amt_in": 0, "wrapped_amt_out": 0, "wrapped_amtin_wei": 0, "wrapped_amtout_wei": 0}
native_or_wrapped = "native" if trade.ConfigObj.NATIVE_GAS_TOKEN_ADDRESS in curve.get_tokens else "wrapped"
real_tkn = trade.ConfigObj.NATIVE_GAS_TOKEN_ADDRESS if native_or_wrapped == "native" else trade.ConfigObj.WRAPPED_GAS_TOKEN_ADDRESS
tknin = real_tkn if trade.tknin in [trade.ConfigObj.NATIVE_GAS_TOKEN_ADDRESS, trade.ConfigObj.WRAPPED_GAS_TOKEN_ADDRESS] else trade.tknin
tknout = trade.tknout if trade.tknin in [trade.ConfigObj.NATIVE_GAS_TOKEN_ADDRESS, trade.ConfigObj.WRAPPED_GAS_TOKEN_ADDRESS] else real_tkn
amtin, amtout, amtin_wei, amtout_wei = _tx["amtin"], _tx["amtout"], _tx["_amtin_wei"], _tx["_amtout_wei"]
carbon_exchanges[curve.exchange_name][f"{native_or_wrapped}_amt_in"] += amtin
carbon_exchanges[curve.exchange_name][f"{native_or_wrapped}_amt_out"] += amtout
carbon_exchanges[curve.exchange_name][f"{native_or_wrapped}_amtin_wei"] += amtin_wei
carbon_exchanges[curve.exchange_name][f"{native_or_wrapped}_amtout_wei"] += amtout_wei
carbon_exchanges[curve.exchange_name][f"{native_or_wrapped}_tknin"] = tknin
carbon_exchanges[curve.exchange_name][f"{native_or_wrapped}_tknout"] = tknout
carbon_exchanges[curve.exchange_name][f"{native_or_wrapped}_cid"] = cid
carbon_exchanges[curve.exchange_name][f"{native_or_wrapped}_raw_txs"].append({
"cid": cid,
"tknin": tknin,
"amtin": amtin,
"_amtin_wei": amtin_wei,
"tknout": tknout,
"amtout": amtout,
"_amtout_wei": amtout_wei,
})

for exchange in carbon_exchanges.keys():
for _native_or_wrapped in ["native", "wrapped"]:
if len(carbon_exchanges[exchange][f"{_native_or_wrapped}_raw_txs"]) > 0:
new_trade_list.append(TradeInstruction(
cid=carbon_exchanges[exchange][f"{_native_or_wrapped}_cid"],
tknin=carbon_exchanges[exchange][f"{_native_or_wrapped}_tknin"],
amtin=carbon_exchanges[exchange][f"{_native_or_wrapped}_amt_in"],
tknout=carbon_exchanges[exchange][f"{_native_or_wrapped}_tknout"],
amtout=carbon_exchanges[exchange][f"{_native_or_wrapped}_amt_out"],
_amtin_wei=carbon_exchanges[exchange][f"{_native_or_wrapped}_amtin_wei"],
_amtout_wei=carbon_exchanges[exchange][f"{_native_or_wrapped}_amtout_wei"],
db=trade.db,
ConfigObj=trade.ConfigObj,
raw_txs=str(carbon_exchanges[exchange][f"{_native_or_wrapped}_raw_txs"])
))

return new_trade_list

def get_arb_contract_args(
self, trade_instructions: List[TradeInstruction], deadline: int
Expand Down

0 comments on commit 60d008d

Please sign in to comment.