Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Class TxHelpers Revision #529

Closed
wants to merge 25 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
8c74ffa
add fusionx
NIXBNT Apr 8, 2024
c381de8
clean up multicarbon handling
NIXBNT Apr 8, 2024
e0ad695
Remove extras & update name
Lesigh-3100 Apr 9, 2024
ec1f451
Revise Class TxHelpers
Apr 9, 2024
9df1461
Remove submithandler.py
Apr 9, 2024
e16f725
remove unused list
NIXBNT Apr 9, 2024
c69a25f
Merge branch 'main' into update_fusionx
zavelevsky Apr 10, 2024
de0697c
Merge pull request #525 from bancorprotocol/update_fusionx
zavelevsky Apr 10, 2024
ce69428
Bump version [skip ci]
actions-user Apr 10, 2024
a51c14c
Update changelog [skip ci]
actions-user Apr 10, 2024
78fa219
github action support for testing more networks [skip ci]
zavelevsky Apr 11, 2024
831a842
Merge branch 'gh_actions_envs' of https://github.com/bancorprotocol/f…
Apr 11, 2024
621c0d3
Reverse previous commit
Apr 11, 2024
05b63da
Merge branch 'tx-helpers-revision' of https://github.com/bancorprotoc…
Apr 11, 2024
6e04ec0
Merge branch 'develop' of https://github.com/bancorprotocol/fastlane-…
Apr 11, 2024
3ff4ea5
Update web3.py's version
Apr 11, 2024
d5f6407
Update poetry lock file
Apr 11, 2024
4721e6d
Debug GitHub's testing environment
Apr 11, 2024
7be3385
Debug
Apr 11, 2024
fed59d1
Debug
Apr 11, 2024
a5c2716
Revert previous commits
Apr 11, 2024
42a028b
Merge branch 'tx-helpers-revision' of https://github.com/bancorprotoc…
Apr 11, 2024
9e81e61
Move tests 059, 062 and 069 to a folder which they will not be execut…
Apr 11, 2024
31a1162
Undo recent stuff
Apr 11, 2024
51d20ed
Merge branch 'tx-helpers-revision' of https://github.com/bancorprotoc…
Apr 11, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/run-pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,19 @@ jobs:
run: |
echo TENDERLY_FORK=$TENDERLY_FORK > .env
echo WEB3_ALCHEMY_PROJECT_ID=$WEB3_ALCHEMY_PROJECT_ID >> .env
echo WEB3_ALCHEMY_BASE=$WEB3_ALCHEMY_BASE >> .env
echo WEB3_FANTOM=$WEB3_FANTOM >> .env
echo WEB3_MANTLE=$WEB3_MANTLE >> .env
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
env:
TENDERLY_FORK: '${{ secrets.TENDERLY_FORK }}'
WEB3_ALCHEMY_PROJECT_ID: '${{ secrets.WEB3_ALCHEMY_PROJECT_ID }}'
WEB3_ALCHEMY_BASE: '${{ secrets.WEB3_ALCHEMY_BASE }}'
WEB3_FANTOM: '${{ secrets.WEB3_FANTOM }}'
WEB3_MANTLE: '${{ secrets.WEB3_MANTLE }}'
ETHERSCAN_TOKEN: '${{ secrets.ETHERSCAN_TOKEN }}'
DEFAULT_MIN_PROFIT_BNT: '${{ secrets.DEFAULT_MIN_PROFIT_BNT }}'
ETH_PRIVATE_KEY_BE_CAREFUL: '${{ secrets.ETH_PRIVATE_KEY_BE_CAREFUL }}'
Expand Down
9 changes: 7 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
# Changelog

## [Unreleased](https://github.com/bancorprotocol/fastlane-bot/tree/HEAD)
## [v3.1.5](https://github.com/bancorprotocol/fastlane-bot/tree/v3.1.5) (2024-04-09)

[Full Changelog](https://github.com/bancorprotocol/fastlane-bot/compare/v3.1.4...HEAD)
[Full Changelog](https://github.com/bancorprotocol/fastlane-bot/compare/v3.1.4...v3.1.5)

- Carbon strategies are duplicated from events [\#521](https://github.com/bancorprotocol/fastlane-bot/issues/521)

- Add support for puffs\_penthouse uniswap\_v3 fork [\#524](https://github.com/bancorprotocol/fastlane-bot/pull/524) ([NIXBNT](https://github.com/NIXBNT))

## [v3.1.4](https://github.com/bancorprotocol/fastlane-bot/tree/v3.1.4) (2024-04-08)

[Full Changelog](https://github.com/bancorprotocol/fastlane-bot/compare/v3.1.3...v3.1.4)

- GPU token external liquidity not found [\#515](https://github.com/bancorprotocol/fastlane-bot/issues/515)
- Hotfix - Carbon strategies duplicated [\#522](https://github.com/bancorprotocol/fastlane-bot/pull/522) ([Lesigh-3100](https://github.com/Lesigh-3100))

## [v3.1.3](https://github.com/bancorprotocol/fastlane-bot/tree/v3.1.3) (2024-04-04)

[Full Changelog](https://github.com/bancorprotocol/fastlane-bot/compare/v3.1.2...v3.1.3)

- gpu external sources added to event mapping [\#517](https://github.com/bancorprotocol/fastlane-bot/pull/517) ([NIXBNT](https://github.com/NIXBNT))

Closed issues

- All files should have doc strings [\#438](https://github.com/bancorprotocol/fastlane-bot/issues/438)
Expand Down
51 changes: 8 additions & 43 deletions fastlane_bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,10 @@
from fastlane_bot.helpers import (
TxRouteHandler,
TxHelpers,
TxHelpersBase,
TradeInstruction,
Univ3Calculator,
add_wrap_or_unwrap_trades_to_route,
split_carbon_trades,
submit_transaction_tenderly
split_carbon_trades
)
from fastlane_bot.helpers.routehandler import maximize_last_trade_per_tkn
from fastlane_bot.tools.cpc import ConstantProductCurve as CPC, CPCContainer, T
Expand Down Expand Up @@ -95,7 +93,7 @@ class CarbonBotBase:
the database manager.
TxRouteHandlerClass
ditto (default: TxRouteHandler).
TxHelpersClass: class derived from TxHelpersBase
TxHelpersClass:
ditto (default: TxHelpers).

"""
Expand All @@ -119,8 +117,6 @@ def __post_init__(self):
if self.ConfigObj is None:
self.ConfigObj = Config()

self.c = self.ConfigObj

assert (
self.polling_interval is None
), "polling_interval is now a parameter to run"
Expand All @@ -129,10 +125,7 @@ def __post_init__(self):
self.TxRouteHandlerClass = TxRouteHandler

if self.TxHelpersClass is None:
self.TxHelpersClass = TxHelpers(ConfigObj=self.ConfigObj)
assert issubclass(
self.TxHelpersClass.__class__, TxHelpersBase
), f"TxHelpersClass not derived from TxHelpersBase {self.TxHelpersClass}"
self.TxHelpersClass = TxHelpers(cfg=self.ConfigObj)

self.db = QueryInterface(ConfigObj=self.ConfigObj)
self.RUN_FLASHLOAN_TOKENS = [*self.ConfigObj.CHAIN_FLASHLOAN_TOKENS.values()]
Expand Down Expand Up @@ -1042,19 +1035,6 @@ def _handle_trade_instructions(

route_struct_maximized = maximize_last_trade_per_tkn(route_struct=route_struct_processed)

# Get the cids
cids = list({ti["cid"] for ti in best_trade_instructions_dic})

# Check if the network is tenderly and submit the transaction accordingly
if self.ConfigObj.NETWORK == self.ConfigObj.NETWORK_TENDERLY:
return submit_transaction_tenderly(
cfg=self.ConfigObj,
flashloan_struct=flashloan_struct,
route_struct=route_struct_maximized,
src_amount=flashloan_amount_wei,
src_address=flashloan_token_address,
)

# Log the route_struct
self.handle_logging_for_trade_instructions(
4, # The log id
Expand All @@ -1065,18 +1045,13 @@ def _handle_trade_instructions(
best_trade_instructions_dic=best_trade_instructions_dic,
)

# Get the tx helpers class
tx_helpers = TxHelpers(ConfigObj=self.ConfigObj)

# Return the validate and submit transaction
return tx_helpers.validate_and_submit_transaction(
return self.TxHelpersClass.validate_and_submit_transaction(
route_struct=route_struct_maximized,
src_amt=flashloan_amount_wei,
src_address=flashloan_token_address,
expected_profit_gastkn=best_profit_gastkn,
expected_profit_usd=best_profit_usd,
safety_override=False,
verbose=True,
log_object=log_dict,
flashloan_struct=flashloan_struct,
)
Expand Down Expand Up @@ -1320,33 +1295,23 @@ def run_single_mode(
randomizer=randomizer,
replay_mode=replay_mode,
)
if tx_hash and tx_hash[0]:
if tx_hash:
self.ConfigObj.logger.info(
f"[bot.run_single_mode] Arbitrage executed [hash={tx_hash}]"
)

# Write the tx hash to a file in the logging_path directory
if self.logging_path:
filename = f"successful_tx_hash_{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}.txt"
print(f"Writing tx_hash hash {tx_hash} to {filename}")
self.ConfigObj.logger.info(f"Writing tx hash {tx_hash} to {filename}")
with open(f"{self.logging_path}/{filename}", "w") as f:

# if isinstance(tx_hash[0], AttributeDict):
# f.write(str(tx_hash[0]))
# else:
for record in tx_hash:
f.write("\n")
f.write("\n")
try:
json.dump(record, f, indent=4)
except:
f.write(str(record))
f.write(tx_hash)

except self.NoArbAvailable as e:
self.ConfigObj.logger.warning(f"[NoArbAvailable] {e}")
except Exception as e:
self.ConfigObj.logger.error(f"[bot:run:single] {e}")
raise
raise e

def _ensure_connection(self, tenderly_fork: str):
"""
Expand Down
1 change: 0 additions & 1 deletion fastlane_bot/config/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,5 @@
FUSIONX_V3_NAME = "fusionx_v3"
CLEOPATRA_V3_NAME = "cleopatra_v3"
CARBON_V1_NAME = "carbon_v1"
CARBON_V1_FORK1_NAME = "altered_carbon"
VELOCIMETER_V2_NAME = "velocimeter_v2"
SOLIDLY_V2_NAME = "solidly_v2"
29 changes: 10 additions & 19 deletions fastlane_bot/config/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

from . import selectors as S
from .base import ConfigBase
from .constants import CARBON_V1_FORK1_NAME, CARBON_V1_NAME
from .constants import CARBON_V1_NAME
load_dotenv()

from decimal import Decimal
Expand Down Expand Up @@ -216,7 +216,6 @@ class ConfigNetwork(ConfigBase):
SUSHISWAP_V2_NAME = "sushiswap_v2"
SUSHISWAP_V3_NAME = "sushiswap_v3"
CARBON_V1_NAME = CARBON_V1_NAME
CARBON_V1_X2_NAME = CARBON_V1_FORK1_NAME
BANCOR_POL_NAME = "bancor_pol"
BALANCER_NAME = "balancer"
PANCAKESWAP_V2_NAME = "pancakeswap_v2"
Expand Down Expand Up @@ -244,8 +243,6 @@ class ConfigNetwork(ConfigBase):

GAS_ORACLE_ADDRESS = None

CARBON_V1_FORKS = [CARBON_V1_NAME, CARBON_V1_X2_NAME]

MULTICALLABLE_EXCHANGES = [BANCOR_V3_NAME, BANCOR_POL_NAME, BALANCER_NAME]
# BANCOR POL
BANCOR_POL_START_BLOCK = 18184448
Expand All @@ -266,25 +263,10 @@ class ConfigNetwork(ConfigBase):

# DEFAULT VALUES SECTION
#######################################################################################
UNIV3_FEE_LIST = [80, 100, 250, 450, 500, 2500, 3000, 10000]
MIN_BNT_LIQUIDITY = 2_000_000_000_000_000_000
DEFAULT_GAS = 950_000
DEFAULT_GAS_PRICE = 0
DEFAULT_GAS_PRICE_OFFSET = 1.09
DEFAULT_GAS_SAFETY_OFFSET = 25_000
DEFAULT_POLL_INTERVAL = 12
DEFAULT_BLOCKTIME_DEVIATION = 13 * 500 * 100 # 10 block time deviation
DEFAULT_MAX_SLIPPAGE = Decimal("1") # 1%
_PROJECT_PATH = os.path.normpath(f"{os.getcwd()}") # TODO: FIX THIS
DEFAULT_CURVES_DATAFILE = os.path.normpath(
f"{_PROJECT_PATH}/carbon/data/curves.csv.gz"
)
CARBON_STRATEGY_CHUNK_SIZE = 200
Q96 = Decimal("2") ** Decimal("96")
DEFAULT_TIMEOUT = 60
CARBON_FEE = Decimal("0.002")
BANCOR_V3_FEE = Decimal("0.0")
DEFAULT_REWARD_PERCENT = Decimal("0.5")
LIMIT_BANCOR3_FLASHLOAN_TOKENS = True
DEFAULT_MIN_PROFIT_GAS_TOKEN = Decimal("0.02")

Expand All @@ -308,6 +290,15 @@ class ConfigNetwork(ConfigBase):
GAS_TKN_IN_FLASHLOAN_TOKENS = None
IS_NO_FLASHLOAN_AVAILABLE = False

# HOOKS
#######################################################################################
@staticmethod
def gas_strategy(web3):
return {
"maxFeePerGas": web3.eth.gas_price,
"maxPriorityFeePerGas": web3.eth.max_priority_fee
}

@classmethod
def new(cls, network=None):
"""
Expand Down
74 changes: 19 additions & 55 deletions fastlane_bot/config/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,51 +112,23 @@ def __init__(self, network: ConfigNetwork, **kwargs):
self.connection.connect_network()
self.w3 = self.connection.web3
self.w3_async = self.connection.w3_async
self.LOCAL_ACCOUNT = self.w3.eth.account.from_key(ETH_PRIVATE_KEY_BE_CAREFUL)


if network.NETWORK in [N.NETWORK_BASE, N.NETWORK_ETHEREUM, N.NETWORK_FANTOM, N.NETWORK_MANTLE]:
self.CARBON_CONTROLLER_CONTRACT = self.w3.eth.contract(
address=network.CARBON_CONTROLLER_ADDRESS,
abi=CARBON_CONTROLLER_ABI,
)
self.BANCOR_ARBITRAGE_CONTRACT = self.w3.eth.contract(
address=self.w3.to_checksum_address(network.FASTLANE_CONTRACT_ADDRESS),
abi=FAST_LANE_CONTRACT_ABI,
)

if network.GAS_ORACLE_ADDRESS:
self.GAS_ORACLE_CONTRACT = self.w3_async.eth.contract(
address=network.GAS_ORACLE_ADDRESS,
abi=GAS_ORACLE_ABI
)

self.BANCOR_ARBITRAGE_CONTRACT = self.w3.eth.contract(
address=self.w3.to_checksum_address(N.FASTLANE_CONTRACT_ADDRESS),
abi=FAST_LANE_CONTRACT_ABI,
)

if network.NETWORK in N.NETWORK_ETHEREUM:
self.BANCOR_NETWORK_INFO_CONTRACT = self.w3.eth.contract(
address=network.BANCOR_V3_NETWORK_INFO_ADDRESS,
abi=BANCOR_V3_NETWORK_INFO_ABI,
if N.GAS_ORACLE_ADDRESS:
self.GAS_ORACLE_CONTRACT = self.w3.eth.contract(
address=N.GAS_ORACLE_ADDRESS,
abi=GAS_ORACLE_ABI,
)
self.ARB_CONTRACT_VERSION = self.BANCOR_ARBITRAGE_CONTRACT.caller.version()

else:
self.CARBON_CONTROLLER_CONTRACT = None
self.ARB_CONTRACT_VERSION = 10
code = self.w3.eth.get_code(self.w3.to_checksum_address(N.FASTLANE_CONTRACT_ADDRESS))
assert len(code) > 10, f"{N.FASTLANE_CONTRACT_ADDRESS}:\n{code}\n{self.RPC_URL}"

if self.BANCOR_ARBITRAGE_CONTRACT is not None:
try:
(
reward_percent,
max_profit,
) = self.BANCOR_ARBITRAGE_CONTRACT.caller.rewards()
self.ARB_REWARD_PERCENTAGE = str(int(reward_percent) / 1000000)
self.ARB_MAX_PROFIT = 1000000 # This is no longer used
except:
self.ARB_REWARD_PERCENTAGE = "0.5"
else:
self.ARB_REWARD_PERCENTAGE = "0.5"

self.EXPECTED_GAS_MODIFIER = "0.85"
self.ARB_CONTRACT_VERSION = self.BANCOR_ARBITRAGE_CONTRACT.caller.version()
self.ARB_REWARDS_PPM = self.BANCOR_ARBITRAGE_CONTRACT.caller.rewards()[0]


class _ConfigProviderTenderly(ConfigProvider):
Expand All @@ -182,26 +154,20 @@ def __init__(self, network: ConfigNetwork, **kwargs):
)
self.connection.connect_network()
self.w3 = self.connection.web3
self.LOCAL_ACCOUNT = self.w3.eth.account.from_key(ETH_PRIVATE_KEY_BE_CAREFUL)

self.BANCOR_NETWORK_INFO_CONTRACT = self.w3.eth.contract(
address=N.BANCOR_V3_NETWORK_INFO_ADDRESS,
abi=BANCOR_V3_NETWORK_INFO_ABI,
)
self.CARBON_CONTROLLER_CONTRACT = self.w3.eth.contract(
address=N.CARBON_CONTROLLER_ADDRESS,
abi=CARBON_CONTROLLER_ABI,
)
self.BANCOR_ARBITRAGE_CONTRACT = self.w3.eth.contract(
address=self.w3.to_checksum_address(N.FASTLANE_CONTRACT_ADDRESS),
abi=FAST_LANE_CONTRACT_ABI,
)
self.ARB_CONTRACT_VERSION = self.BANCOR_ARBITRAGE_CONTRACT.caller.version()

reward_percent, max_profit = self.BANCOR_ARBITRAGE_CONTRACT.caller.rewards()
if N.GAS_ORACLE_ADDRESS:
self.GAS_ORACLE_CONTRACT = self.w3.eth.contract(
address=N.GAS_ORACLE_ADDRESS,
abi=GAS_ORACLE_ABI,
)

self.ARB_REWARD_PERCENTAGE = str(int(reward_percent) / 1000000)
self.ARB_MAX_PROFIT = str(int(max_profit) / (10**18))
self.ARB_CONTRACT_VERSION = self.BANCOR_ARBITRAGE_CONTRACT.caller.version()
self.ARB_REWARDS_PPM = self.BANCOR_ARBITRAGE_CONTRACT.caller.rewards()[0]


class _ConfigProviderInfura(ConfigProvider):
Expand Down Expand Up @@ -232,6 +198,4 @@ def __init__(self, network: ConfigNetwork, **kwargs):
# raise NotImplementedError("Infura not implemented")
self.connection = None
self.w3 = None
self.BANCOR_NETWORK_INFO_CONTRACT = None
self.CARBON_CONTROLLER_CONTRACT = None
self.BANCOR_ARBITRAGE_CONTRACT = None
2 changes: 1 addition & 1 deletion fastlane_bot/data/multichain_addresses.csv
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ agni_v3,mantle,uniswap_v3,0x25780dc8Fc3cfBD75F33bFDAB65e969b603b2035,0x319B69888
puffs_penthouse_v3,mantle,uniswap_v3,0x8f140Fc3e9211b8DC2fC1D7eE3292F6817C5dD5D,0x58b3a3797FF02B934a7d69D00873889F33Fb8829,,59915640,
butter_v3,mantle,uniswap_v3,0xEECa0a86431A7B42ca2Ee5F479832c3D4a4c2644,0xAe5c0a73bAE513fe5842DbcB8E32fc7E3e30DA57,,22966090,
carbon_v1,mantle,carbon_v1,0x7900f766F06e361FDDB4FdeBac5b138c4EEd8d4A,0x7900f766F06e361FDDB4FdeBac5b138c4EEd8d4A,,,
some_carbon_fork,mantle,carbon_v1,0x3749f6Ef09B0f74CD5324126b02d867A03DEE844,0x3749f6Ef09B0f74CD5324126b02d867A03DEE844,,,
fusionx_supernova,mantle,carbon_v1,0x04FBC7f949326fFf7Fe4D6aE96BAfa3D8e8A8c0a,0x04FBC7f949326fFf7Fe4D6aE96BAfa3D8e8A8c0a,,61955475,
cleopatra_v2,mantle,solidly_v2,0xAAA16c016BF556fcD620328f0759252E29b1AB57,0xAAA45c8F5ef92a000a121d102F4e89278a711Faa,,34705175,
cleopatra_v3,mantle,uniswap_v3,0xAAA32926fcE6bE95ea2c51cB4Fcb60836D320C42,0xAAAE99091Fbb28D400029052821653C1C752483B,,34705175,
fusionx_v2,mantle,uniswap_v2,0xE5020961fA51ffd3662CDf307dEf18F9a87Cce7c,0xDd0840118bF9CCCc6d67b2944ddDfbdb995955FD,0.003,2868,
Expand Down
Loading
Loading