Skip to content

Commit

Permalink
Merge branch '178-bugfix-for-missing-pool-data' of https://github.com…
Browse files Browse the repository at this point in the history
…/bancorprotocol/fastlane-bot into 178-bugfix-for-missing-pool-data
  • Loading branch information
mikewcasale committed Oct 31, 2023
2 parents 54d5eb4 + 4d8ca98 commit 9ee7acb
Show file tree
Hide file tree
Showing 14 changed files with 235 additions and 225 deletions.
19 changes: 17 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,26 @@

## [Unreleased](https://github.com/bancorprotocol/fastlane-bot/tree/HEAD)

[Full Changelog](https://github.com/bancorprotocol/fastlane-bot/compare/v2.7.51...HEAD)
[Full Changelog](https://github.com/bancorprotocol/fastlane-bot/compare/v2.7.52...HEAD)

- Several bugs - static pool csv and validator [\#171](https://github.com/bancorprotocol/fastlane-bot/issues/171)
- zero liquidity pool filters Carbon Orders where y0 is 0 but y1 isn't [\#176](https://github.com/bancorprotocol/fastlane-bot/issues/176)

- Add the ability to get all pools [\#174](https://github.com/bancorprotocol/fastlane-bot/issues/174)

Closed issues

- unsupported operand type\(s\) for -: 'float' and 'decimal.Decimal' [\#180](https://github.com/bancorprotocol/fastlane-bot/issues/180)

Merged pull requests

- Fix a bug that filtered Carbon Strategies with an empty order 0 [\#177](https://github.com/bancorprotocol/fastlane-bot/pull/177) ([Lesigh-3100](https://github.com/Lesigh-3100))

## [v2.7.52](https://github.com/bancorprotocol/fastlane-bot/tree/v2.7.52) (2023-10-25)

[Full Changelog](https://github.com/bancorprotocol/fastlane-bot/compare/v2.7.51...v2.7.52)

- Several bugs - static pool csv and validator [\#171](https://github.com/bancorprotocol/fastlane-bot/issues/171)

- Create terraformer [\#175](https://github.com/bancorprotocol/fastlane-bot/pull/175) ([Lesigh-3100](https://github.com/Lesigh-3100))

## [v2.7.51](https://github.com/bancorprotocol/fastlane-bot/tree/v2.7.51) (2023-10-25)
Expand Down
2 changes: 1 addition & 1 deletion fastlane_bot/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from .bot import CarbonBot as Bot, __VERSION__, __DATE__
from .config import Config, ConfigNetwork, ConfigDB, ConfigLogger, ConfigProvider

__version__ = '2.7.52'
__version__ = '2.7.53'



41 changes: 23 additions & 18 deletions fastlane_bot/events/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,24 +102,29 @@ def remove_unsupported_exchanges(self) -> None:
pools = self.filter_pools(exchange_name)
self.log_pool_numbers(pools, exchange_name)

def has_balance(self, pool: Dict[str, Any], key: str) -> bool:
def has_balance(self, pool: Dict[str, Any], keys: List[str]) -> bool:
"""
Check if a pool has a balance for a given key
Parameters
----------
pool: Dict[str, Any]
The pool to check
key: str
The key to check for a balance
keys: List[str]
The keys to check for a balance
Returns
-------
bool
True if the pool has a balance for the given key, False otherwise
"""
return key in pool and pool[key] > 0

for key in keys:
if key in pool and pool[key] > 0:
return True
return False


def get_tokens_from_exchange(self, exchange_name: str) -> List[str]:
"""
Expand Down Expand Up @@ -148,28 +153,28 @@ def get_tokens_from_exchange(self, exchange_name: str) -> List[str]:
tokens = list(set(tokens))
return tokens

def filter_pools(self, exchange_name: str, key: str = "") -> List[Dict[str, Any]]:
def filter_pools(self, exchange_name: str, keys: List[str] = "") -> List[Dict[str, Any]]:
"""
Filter pools by exchange name and key
Parameters
----------
exchange_name: str
The exchange name to filter by
key: str
keys: str
The key to filter by
Returns
-------
List[Dict[str, Any]]
The filtered pools
"""
if key:
if keys:
return [
pool
for pool in self.state
if pool["exchange_name"] == exchange_name
and self.has_balance(pool, key)
and self.has_balance(pool, keys)
]
else:
return [
Expand Down Expand Up @@ -209,16 +214,16 @@ def remove_zero_liquidity_pools(self) -> None:
"balancer",
]
keys = [
"liquidity",
"tkn0_balance",
"tkn0_balance",
"tkn0_balance",
"tkn0_balance",
"y_0",
"y_0",
"tkn0_balance",
"liquidity",
"tkn0_balance",
["liquidity"],
["tkn0_balance"],
["tkn0_balance"],
["tkn0_balance"],
["tkn0_balance"],
["y_0"],
["y_0", "y_1"],
["tkn0_balance"],
["liquidity"],
["tkn0_balance"],
]

self.state = [
Expand Down
3 changes: 2 additions & 1 deletion fastlane_bot/helpers/poolandtokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,8 @@ def _carbon_to_cpc(self) -> ConstantProductCurve:
B = Decimal(self.B_1) if i == 0 else Decimal(self.B_0)
y = Decimal(self.y_1) if i == 0 else Decimal(self.y_0)
z = yint = Decimal(self.z_1) if i == 0 else Decimal(self.z_0)

if y <= 0:
continue
encoded_order = EncodedOrder(
**{
"token": self.pair_name.split("/")[i].replace(
Expand Down
38 changes: 30 additions & 8 deletions fastlane_bot/modes/base_triangle.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,13 @@ def get_combos(
.byparams(exchange=self.base_exchange)
.curves
)
if len(base_exchange_curves) == 0:
continue

base_direction_pair = base_exchange_curves[0].pair
base_direction_one = [curve for curve in base_exchange_curves if curve.pair == base_direction_pair]
base_direction_two = [curve for curve in base_exchange_curves if curve.pair != base_direction_pair]
assert len(base_exchange_curves) == len(base_direction_one) + len(base_direction_two)
y_match_curves = CCm.bypairs(
set(CCm.filter_pairs(onein=target_tknx))
& set(CCm.filter_pairs(onein=flt))
Expand All @@ -130,24 +137,39 @@ def get_combos(
set(CCm.filter_pairs(onein=target_tkny))
& set(CCm.filter_pairs(onein=flt))
)

y_match_curves_not_carbon = [
x
for x in y_match_curves
if x.params.exchange != self.base_exchange
]
if len(y_match_curves_not_carbon) == 0:
continue
x_match_curves_not_carbon = [
x
for x in x_match_curves
if x.params.exchange != self.base_exchange
]
combos = self.get_miniverse(
y_match_curves_not_carbon,
base_exchange_curves,
x_match_curves_not_carbon,
flt,
arb_mode,
combos,
)
if len(x_match_curves_not_carbon) == 0:
continue
if len(base_direction_one) > 0:
combos = self.get_miniverse(
y_match_curves_not_carbon,
base_direction_one,
x_match_curves_not_carbon,
flt,
arb_mode,
combos,
)
if len(base_direction_two) > 0:
combos = self.get_miniverse(
y_match_curves_not_carbon,
base_direction_two,
x_match_curves_not_carbon,
flt,
arb_mode,
combos,
)
return combos

@staticmethod
Expand Down
67 changes: 18 additions & 49 deletions fastlane_bot/modes/triangle_multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,62 +39,31 @@ def find_arbitrage(self, candidates: List[Any] = None, ops: Tuple = None, best_p
r = None
CC_cc = CPCContainer(miniverse)
O = MargPOptimizer(CC_cc)
try:
r = O.margp_optimizer(src_token)
trade_instructions_df = r.trade_instructions(O.TIF_DFAGGR)
trade_instructions_dic = r.trade_instructions(O.TIF_DICTS)
trade_instructions = r.trade_instructions()
"""
The following handles an edge case until parallel execution is available:
1 Determine correct direction - opposite of non-Carbon pool
2 Get cids of wrong-direction Carbon pools
3 Create new CPCContainer with correct pools
4 Rerun optimizer
5 Resume normal flow
"""
non_carbon_cids = [
curve.cid
for curve in miniverse
if curve.params.get("exchange") != "carbon_v1"
]
non_carbon_row = trade_instructions_df.loc[non_carbon_cids[0]]
tkn0_into_carbon = non_carbon_row[0] < 0
wrong_direction_cids = [
idx
for idx, row in trade_instructions_df.iterrows()
if (
(tkn0_into_carbon and row[0] < 0)
or (not tkn0_into_carbon and row[0] > 0)
)
and ("-0" in idx or "-1" in idx)
]
if non_carbon_cids and len(wrong_direction_cids) > 0:
self.ConfigObj.logger.debug(
f"\n\nRemoving wrong direction pools & rerunning optimizer\ntrade_instructions_df before: {trade_instructions_df.to_string()}"
)
new_curves = [
curve
for curve in miniverse
if curve.cid not in wrong_direction_cids
]

# Rerun main flow with the new set of curves
CC_cc = CPCContainer(new_curves)
O = MargPOptimizer(CC_cc)
r = O.margp_optimizer(src_token)
profit_src = -r.result
trade_instructions_df = r.trade_instructions(O.TIF_DFAGGR)
trade_instructions_dic = r.trade_instructions(O.TIF_DICTS)
trade_instructions = r.trade_instructions()
except Exception as e:
#try:
r = O.margp_optimizer(src_token)
trade_instructions_dic = r.trade_instructions(O.TIF_DICTS)
if len(trade_instructions_dic) < 3:
# Failed to converge
continue
trade_instructions_df = r.trade_instructions(O.TIF_DFAGGR)
#print(trade_instructions_df)
trade_instructions = r.trade_instructions()
"""
The following handles an edge case until parallel execution is available:
1 Determine correct direction - opposite of non-Carbon pool
2 Get cids of wrong-direction Carbon pools
3 Create new CPCContainer with correct pools
4 Rerun optimizer
5 Resume normal flow
"""

profit_src = -r.result

# Get the cids
cids = [ti["cid"] for ti in trade_instructions_dic]

# Calculate the profit
profit = self.calculate_profit(src_token, profit_src, self.CCm, cids)

if str(profit) == "nan":
self.ConfigObj.logger.debug("profit is nan, skipping")
continue
Expand Down
18 changes: 9 additions & 9 deletions resources/NBTest/NBTest_034_Interface.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
"name": "stdout",
"output_type": "stream",
"text": [
"ConstantProductCurve v2.14 (23/May/2023)\n",
"ConstantProductCurve v3.3.1 (05/Oct/2023)\n",
"CarbonBot v3-b2.2 (20/June/2023)\n",
"UniswapV2 v0.0.1 (2023-07-03)\n",
"UniswapV3 v0.0.1 (2023-07-03)\n",
"SushiswapV2 v0.0.1 (2023-07-03)\n",
"CarbonV1 v0.0.1 (2023-07-03)\n",
"BancorV3 v0.0.1 (2023-07-03)\n",
"UniswapV2 v0.0.2 (2023-08-27)\n",
"UniswapV3 v0.0.2 (2023-08-27)\n",
"SushiswapV2 v0.0.2 (2023-08-27)\n",
"CarbonV1 v0.0.2 (2023-08-27)\n",
"BancorV3 v0.0.2 (2023-08-27)\n",
"QueryInterface v0.0.1 (2023-07-03)\n",
"Token v0.0.1 (2023-07-03)\n",
"imported m, np, pd, plt, os, sys, decimal; defined iseq, raises, require\n",
Expand Down Expand Up @@ -123,8 +123,8 @@
"outputs": [],
"source": [
"qi.state = [{'exchange_name': 'uniswap_v2', 'address': '0x123', 'tkn0_key': 'TKN-0x123', 'tkn1_key': 'TKN-0x456', 'pair_name': 'Pair-0x789', 'liquidity': 10}, {'exchange_name': 'sushiswap_v2', 'address': '0xabc', 'tkn0_key': 'TKN-0xabc', 'tkn1_key': 'TKN-0xdef', 'pair_name': 'Pair-0xghi', 'liquidity': 0}]\n",
"assert (qi.has_balance(qi.state[0], 'liquidity') == True)\n",
"assert (qi.has_balance(qi.state[1], 'liquidity') == False)"
"assert (qi.has_balance(qi.state[0], ['liquidity']) == True)\n",
"assert (qi.has_balance(qi.state[1], ['liquidity']) == False)"
]
},
{
Expand Down Expand Up @@ -268,7 +268,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.8"
"version": "3.10.1"
}
},
"nbformat": 4,
Expand Down
6 changes: 3 additions & 3 deletions resources/NBTest/NBTest_034_Interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# extension: .py
# format_name: light
# format_version: '1.5'
# jupytext_version: 1.15.2
# jupytext_version: 1.14.5
# kernelspec:
# display_name: Python 3
# language: python
Expand Down Expand Up @@ -57,8 +57,8 @@
# ## test_has_balance

qi.state = [{'exchange_name': 'uniswap_v2', 'address': '0x123', 'tkn0_key': 'TKN-0x123', 'tkn1_key': 'TKN-0x456', 'pair_name': 'Pair-0x789', 'liquidity': 10}, {'exchange_name': 'sushiswap_v2', 'address': '0xabc', 'tkn0_key': 'TKN-0xabc', 'tkn1_key': 'TKN-0xdef', 'pair_name': 'Pair-0xghi', 'liquidity': 0}]
assert (qi.has_balance(qi.state[0], 'liquidity') == True)
assert (qi.has_balance(qi.state[1], 'liquidity') == False)
assert (qi.has_balance(qi.state[0], ['liquidity']) == True)
assert (qi.has_balance(qi.state[1], ['liquidity']) == False)

# ## test_filter_pools

Expand Down
2 changes: 1 addition & 1 deletion resources/NBTest/NBTest_038_TestBancorV3Mode.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@
"\n",
"combos = finder.get_combos(flashloan_tokens=flt, CCm=CCm, arb_mode=\"bancor_v3\")\n",
"all_miniverses = finder.get_miniverse_combos(combos)\n",
"assert len(all_miniverses) == 144, f\"[test_bancor_v3] Different data used for tests, expected 144 miniverses, got {len(all_miniverses)}\""
"assert len(all_miniverses) >= 146, f\"[test_bancor_v3] Different data used for tests, expected 146 miniverses, found {len(all_miniverses)}\""
]
},
{
Expand Down
4 changes: 2 additions & 2 deletions resources/NBTest/NBTest_038_TestBancorV3Mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# extension: .py
# format_name: light
# format_version: '1.5'
# jupytext_version: 1.15.2
# jupytext_version: 1.14.5
# kernelspec:
# display_name: Python 3
# language: python
Expand Down Expand Up @@ -307,7 +307,7 @@ def init_bot(mgr: Manager) -> CarbonBot:

combos = finder.get_combos(flashloan_tokens=flt, CCm=CCm, arb_mode="bancor_v3")
all_miniverses = finder.get_miniverse_combos(combos)
assert len(all_miniverses) == 144, f"[test_bancor_v3] Different data used for tests, expected 144 miniverses, got {len(all_miniverses)}"
assert len(all_miniverses) >= 146, f"[test_bancor_v3] Different data used for tests, expected 146 miniverses, found {len(all_miniverses)}"
# -

# ## Test_get_mono_direction_carbon_curves
Expand Down
Loading

0 comments on commit 9ee7acb

Please sign in to comment.