Skip to content

Commit

Permalink
feat: optimizer moved to a separate repo
Browse files Browse the repository at this point in the history
  • Loading branch information
platonfloria committed May 7, 2024
1 parent 8b68733 commit d10b2f7
Show file tree
Hide file tree
Showing 149 changed files with 639 additions and 67,143 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/release-and-pypi-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ jobs:
steps:
# Checkout
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v4
with:
submodules: true

# Check commit message
- id: check
Expand Down
6 changes: 4 additions & 2 deletions .github/workflows/run-pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ jobs:
matrix:
python-version: [3.8]
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
submodules: true
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
Expand All @@ -40,7 +42,7 @@ jobs:
echo ETHERSCAN_TOKEN=$ETHERSCAN_TOKEN >> .env
echo DEFAULT_MIN_PROFIT_BNT=$DEFAULT_MIN_PROFIT_BNT >> .env
echo ETH_PRIVATE_KEY_BE_CAREFUL=$ETH_PRIVATE_KEY_BE_CAREFUL >> .env
cd resources/NBTest;ln -s ../../fastlane_bot fastlane_bot;cd ..;cd ..; poetry run ./run_tests
make test
env:
TENDERLY_FORK: '${{ secrets.TENDERLY_FORK }}'
WEB3_ALCHEMY_PROJECT_ID: '${{ secrets.WEB3_ALCHEMY_PROJECT_ID }}'
Expand Down
6 changes: 2 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ carbon/tools/*
*/.coverage
*/.coverage.*
*/.cover
NBTest/carbon/*
NBTest/carbon
resources/NBTest/fastlane_bot

.python-version

/.env
*.env
Expand Down Expand Up @@ -72,4 +71,3 @@ logs/*
/fastlane_bot/data/blockchain_data/*/token_detail/
missing_tokens_df.csv
tokens_and_fee_df.csv
fastlane_bot/tests/nbtest/*
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "arb-optimizer"]
path = arb-optimizer
url = [email protected]:bancorprotocol/arb-optimizer.git
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test:
poetry run pytest fastlane_bot/tests -v $1
1 change: 1 addition & 0 deletions arb-optimizer
Submodule arb-optimizer added at 261f01
27 changes: 14 additions & 13 deletions fastlane_bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
from typing import Generator, List, Dict, Tuple, Any, Callable
from typing import Optional

from arb_optimizer import CurveContainer, ConstantProductCurve as CPC

from fastlane_bot.config import Config
from fastlane_bot.helpers import (
TxRouteHandler,
Expand All @@ -66,7 +68,6 @@
split_carbon_trades,
maximize_last_trade_per_tkn
)
from fastlane_bot.tools.cpc import ConstantProductCurve as CPC, CPCContainer, T
from .config.constants import FLASHLOAN_FEE_MAP
from .events.interface import QueryInterface
from .modes.pairwise_multi import FindArbitrageMultiPairwise
Expand Down Expand Up @@ -128,13 +129,13 @@ def __post_init__(self):
self.db = QueryInterface(ConfigObj=self.ConfigObj)
self.RUN_FLASHLOAN_TOKENS = [*self.ConfigObj.CHAIN_FLASHLOAN_TOKENS.values()]

def get_curves(self) -> CPCContainer:
def get_curves(self) -> CurveContainer:
"""
Gets the curves from the database.
Returns
-------
CPCContainer
CurveContainer
The container of curves.
"""
self.db.refresh_pool_data()
Expand Down Expand Up @@ -184,7 +185,7 @@ def get_curves(self) -> CPCContainer:
f"[bot.get_curves] MUST FIX UNEXPECTED ERROR converting pool to curve {p}\n[ERR={e}]\n\n"
)

return CPCContainer(curves)
return CurveContainer(curves)

def _simple_ordering_by_src_token(
self, best_trade_instructions_dic, best_src_token
Expand Down Expand Up @@ -277,7 +278,7 @@ def _get_arb_finder(cls, arb_mode: str) -> Callable:
def _find_arbitrage(
self,
flashloan_tokens: List[str],
CCm: CPCContainer,
CCm: CurveContainer,
arb_mode: str,
randomizer: int
) -> dict:
Expand All @@ -295,7 +296,7 @@ def _find_arbitrage(
def _run(
self,
flashloan_tokens: List[str],
CCm: CPCContainer,
CCm: CurveContainer,
*,
arb_mode: str,
randomizer: int,
Expand All @@ -311,7 +312,7 @@ def _run(
----------
flashloan_tokens: List[str]
The tokens to flashloan.
CCm: CPCContainer
CCm: CurveContainer
The container.
arb_mode: str
The arbitrage mode.
Expand Down Expand Up @@ -575,7 +576,7 @@ def custom_sort(self, data, sort_sequence):

def calculate_profit(
self,
CCm: CPCContainer,
CCm: CurveContainer,
best_profit: Decimal,
fl_token: str,
flashloan_fee_amt: int = 0,
Expand All @@ -585,7 +586,7 @@ def calculate_profit(
Parameters
----------
CCm: CPCContainer
CCm: CurveContainer
The container.
best_profit: Decimal
The best profit.
Expand Down Expand Up @@ -696,7 +697,7 @@ def calculate_arb(

def _handle_trade_instructions(
self,
CCm: CPCContainer,
CCm: CurveContainer,
arb_mode: str,
r: Any,
replay_from_block: int = None
Expand All @@ -709,7 +710,7 @@ def _handle_trade_instructions(
Parameters
----------
CCm: CPCContainer
CCm: CurveContainer
The container.
arb_mode: str
The arbitrage mode.
Expand Down Expand Up @@ -893,7 +894,7 @@ def run(
self,
*,
flashloan_tokens: List[str] = None,
CCm: CPCContainer = None,
CCm: CurveContainer = None,
arb_mode: str = None,
run_data_validator: bool = False,
randomizer: int = 0,
Expand All @@ -908,7 +909,7 @@ def run(
----------
flashloan_tokens: List[str]
The flashloan tokens (optional; default: RUN_FLASHLOAN_TOKENS)
CCm: CPCContainer
CCm: CurveContainer
The complete market data container (optional; default: database via get_curves())
arb_mode: str
the arbitrage mode (default: None; can be set depending on arbmode)
Expand Down
7 changes: 4 additions & 3 deletions fastlane_bot/helpers/poolandtokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
from dataclasses import dataclass
from typing import Dict, Any, List, Union

from arb_optimizer import ConstantProductCurve

from fastlane_bot.config import Config

# from fastlane_bot.config import SUPPORTED_EXCHANGES, CARBON_V1_NAME, UNISWAP_V3_NAME
from fastlane_bot.helpers.univ3calc import Univ3Calculator
from fastlane_bot.tools.cpc import ConstantProductCurve
from fastlane_bot.utils import EncodedOrder


Expand Down Expand Up @@ -368,8 +369,8 @@ def _other_to_cpc(self) -> List[Any]:

# create a typed-dictionary of the arguments
typed_args = {
"x_tknb": tkn0_balance,
"y_tknq": tkn1_balance,
"liq_tknb": tkn0_balance,
"liq_tknq": tkn1_balance,
"pair": self.pair_name.replace(self.ConfigObj.NATIVE_GAS_TOKEN_ADDRESS, self.ConfigObj.WRAPPED_GAS_TOKEN_ADDRESS),
"fee": self.fee,
"cid": self.cid,
Expand Down
3 changes: 2 additions & 1 deletion fastlane_bot/helpers/routehandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@
import eth_abi
import pandas as pd

from arb_optimizer.curves import T

from .tradeinstruction import TradeInstruction
from ..events.interface import Pool
from ..tools.cpc import T
from fastlane_bot.config.constants import AGNI_V3_NAME, BUTTER_V3_NAME, CLEOPATRA_V3_NAME, PANCAKESWAP_V3_NAME, \
ETHEREUM, METAVAULT_V3_NAME

Expand Down
3 changes: 0 additions & 3 deletions fastlane_bot/modes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
from _decimal import Decimal
import pandas as pd

from fastlane_bot.tools.cpc import T
from fastlane_bot.utils import num_format


class ArbitrageFinderBase:
"""
Expand Down
7 changes: 4 additions & 3 deletions fastlane_bot/modes/base_pairwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
import itertools
from typing import List, Tuple, Any, Union

from arb_optimizer import CurveContainer

from fastlane_bot.modes.base import ArbitrageFinderBase
from fastlane_bot.tools.cpc import CPCContainer


class ArbitrageFinderPairwiseBase(ArbitrageFinderBase):
Expand All @@ -30,14 +31,14 @@ def find_arbitrage(self, candidates: List[Any] = None, ops: Tuple = None, best_p

@staticmethod
def get_combos(
CCm: CPCContainer, flashloan_tokens: List[str]
CCm: CurveContainer, flashloan_tokens: List[str]
) -> Tuple[List[Any], List[Any]]:
"""
Get combos for pairwise arbitrage
Parameters
----------
CCm : CPCContainer
CCm : CurveContainer
Container for all the curves
flashloan_tokens : list
List of flashloan tokens
Expand Down
3 changes: 2 additions & 1 deletion fastlane_bot/modes/base_triangle.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@

import pandas as pd

from arb_optimizer.curves import T

from fastlane_bot.modes.base import ArbitrageFinderBase
from fastlane_bot.tools.cpc import T


class ArbitrageFinderTriangleBase(ArbitrageFinderBase):
Expand Down
8 changes: 4 additions & 4 deletions fastlane_bot/modes/pairwise_multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

import pandas as pd

from arb_optimizer import CurveContainer, PairOptimizer

from fastlane_bot.modes.base_pairwise import ArbitrageFinderPairwiseBase
from fastlane_bot.tools.cpc import CPCContainer
from fastlane_bot.tools.optimizer import MargPOptimizer, PairOptimizer


class FindArbitrageMultiPairwise(ArbitrageFinderPairwiseBase):
Expand Down Expand Up @@ -161,12 +161,12 @@ def run_main_flow(
"""
Run main flow to find arbitrage.
"""
CC_cc = CPCContainer(curves)
CC_cc = CurveContainer(curves)
O = PairOptimizer(CC_cc)
pstart = {
tkn0: CC_cc.bypairs(f"{tkn0}/{tkn1}")[0].p
} # this intentionally selects the non_carbon curve
r = O.optimize(src_token, params=dict(pstart=pstart))
r = O.optimize(src_token)
profit_src = -r.result
trade_instructions_df = r.trade_instructions(O.TIF_DFAGGR)
return O, profit_src, r, trade_instructions_df
Expand Down
8 changes: 4 additions & 4 deletions fastlane_bot/modes/pairwise_multi_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@

import pandas as pd

from arb_optimizer import CurveContainer, PairOptimizer

from fastlane_bot.modes.base_pairwise import ArbitrageFinderPairwiseBase
from fastlane_bot.tools.cpc import CPCContainer
from fastlane_bot.tools.optimizer import MargPOptimizer, PairOptimizer


class FindArbitrageMultiPairwiseAll(ArbitrageFinderPairwiseBase):
Expand Down Expand Up @@ -157,13 +157,13 @@ def run_main_flow(
"""
Run main flow to find arbitrage.
"""
CC_cc = CPCContainer(curves)
CC_cc = CurveContainer(curves)
O = PairOptimizer(CC_cc)
pstart = {
tkn0: CC_cc.bypairs(f"{tkn0}/{tkn1}")[0].p
} # this intentionally selects the non_carbon curve

r = O.optimize(src_token, params=dict(pstart=pstart))
r = O.optimize(src_token)

profit_src = -r.result
trade_instructions_df = r.trade_instructions(O.TIF_DFAGGR)
Expand Down
17 changes: 9 additions & 8 deletions fastlane_bot/modes/pairwise_multi_pol.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
All rights reserved.
Licensed under MIT.
"""
import itertools
from typing import List, Any, Tuple, Union, Hashable

import pandas as pd
import itertools

from arb_optimizer import CurveContainer, PairOptimizer
from arb_optimizer.curves import T

from fastlane_bot.modes.base_pairwise import ArbitrageFinderPairwiseBase
from fastlane_bot.tools.cpc import CPCContainer
from fastlane_bot.tools.optimizer import MargPOptimizer, PairOptimizer
from fastlane_bot.tools.cpc import T


class FindArbitrageMultiPairwisePol(ArbitrageFinderPairwiseBase):
Expand Down Expand Up @@ -152,12 +153,12 @@ def run_main_flow(
"""
Run main flow to find arbitrage.
"""
CC_cc = CPCContainer(curves)
CC_cc = CurveContainer(curves)
O = PairOptimizer(CC_cc)
pstart = {
tkn0: CC_cc.bypairs(f"{tkn0}/{tkn1}")[0].p
} # this intentionally selects the non_carbon curve
r = O.optimize(src_token, params=dict(pstart=pstart))
r = O.optimize(src_token)
profit_src = -r.result
trade_instructions_df = r.trade_instructions(O.TIF_DFAGGR)
return O, profit_src, r, trade_instructions_df
Expand All @@ -174,14 +175,14 @@ def process_wrong_direction_pools(
return new_curves

def get_combos_pol(self,
CCm: CPCContainer, flashloan_tokens: List[str]
CCm: CurveContainer, flashloan_tokens: List[str]
) -> Tuple[List[Any], List[Any]]:
"""
Get combos for pairwise arbitrage specific to Bancor POL
Parameters
----------
CCm : CPCContainer
CCm : CurveContainer
Container for all the curves
flashloan_tokens : list
List of flashloan tokens
Expand Down
8 changes: 4 additions & 4 deletions fastlane_bot/modes/pairwise_single.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

from tqdm.contrib import itertools

from arb_optimizer import CurveContainer, PairOptimizer

from fastlane_bot.modes.base_pairwise import ArbitrageFinderPairwiseBase
from fastlane_bot.tools.cpc import CPCContainer
from fastlane_bot.tools.optimizer import MargPOptimizer, PairOptimizer


class FindArbitrageSinglePairwise(ArbitrageFinderPairwiseBase):
Expand Down Expand Up @@ -60,12 +60,12 @@ def find_arbitrage(self, candidates: List[Any] = None, ops: Tuple = None, best_p
continue

for curve_combo in curve_combos:
CC_cc = CPCContainer(curve_combo)
CC_cc = CurveContainer(curve_combo)
O = PairOptimizer(CC_cc)
src_token = tkn1
try:
pstart = {tkn0: CC_cc.bypairs(f"{tkn0}/{tkn1}")[0].p}
r = O.optimize(src_token, params=dict(pstart=pstart))
r = O.optimize(src_token)
profit_src = -r.result
trade_instructions_df = r.trade_instructions(O.TIF_DFAGGR)
trade_instructions_dic = r.trade_instructions(O.TIF_DICTS)
Expand Down
Loading

0 comments on commit d10b2f7

Please sign in to comment.