From 57a6d4ef33d21b0d33a1a575a2faaea27e04c02d Mon Sep 17 00:00:00 2001 From: Sheng Lundquist Date: Wed, 23 Oct 2024 10:38:10 -0700 Subject: [PATCH] No longer checking for min txn amount, due to clear error from pypechain --- .../crash_report/known_error_checks.py | 44 --- tests/min_txn_amount_test.py | 345 ------------------ 2 files changed, 389 deletions(-) delete mode 100644 tests/min_txn_amount_test.py diff --git a/src/agent0/core/hyperdrive/crash_report/known_error_checks.py b/src/agent0/core/hyperdrive/crash_report/known_error_checks.py index 5d214a9d3f..7f0d77a4f3 100644 --- a/src/agent0/core/hyperdrive/crash_report/known_error_checks.py +++ b/src/agent0/core/hyperdrive/crash_report/known_error_checks.py @@ -35,7 +35,6 @@ def check_for_known_errors(trade_result: TradeResult, interface: HyperdriveReadI trade_result = check_for_invalid_balance(trade_result, interface) trade_result = check_for_insufficient_allowance(trade_result, interface) trade_result = check_for_slippage(trade_result) - trade_result = check_for_min_txn_amount(trade_result) trade_result = check_for_long_proceeds_less_than_fees(trade_result, interface) return trade_result @@ -303,49 +302,6 @@ def check_for_slippage(trade_result: TradeResult) -> TradeResult: return trade_result -def check_for_min_txn_amount(trade_result: TradeResult) -> TradeResult: - """Detects minimum transaction amount errors in trade_result and adds additional information to the - exception in trade_result - - Arguments - --------- - trade_result: TradeResult - The trade result object from trading - - Returns - ------- - TradeResult - A modified trade_result that has a custom exception argument message prepended - """ - - assert trade_result.pool_config is not None - assert trade_result.trade_object is not None - - trade_type = trade_result.trade_object.market_action.action_type - add_arg = None - is_min_txn_amount = False - - # Redeem withdrawal shares is not subject to minimum transaction amounts - if trade_type != HyperdriveActionType.REDEEM_WITHDRAW_SHARE: - min_txn_amount = trade_result.pool_config["minimum_transaction_amount"] - trade_amount = trade_result.trade_object.market_action.trade_amount - if trade_amount < min_txn_amount: - add_arg = ( - f"Minimum Transaction Amount: {trade_type.name} for {trade_amount}, " - f"minimum transaction amount is {min_txn_amount}." - ) - is_min_txn_amount = True - - # Prepend balance error argument to exception args - if is_min_txn_amount: - assert add_arg is not None - assert trade_result.exception is not None - trade_result.exception.args = (add_arg,) + trade_result.exception.args - trade_result.is_min_txn_amount = True - - return trade_result - - def check_for_long_proceeds_less_than_fees( trade_result: TradeResult, interface: HyperdriveReadInterface ) -> TradeResult: diff --git a/tests/min_txn_amount_test.py b/tests/min_txn_amount_test.py deleted file mode 100644 index 2d434d87f1..0000000000 --- a/tests/min_txn_amount_test.py +++ /dev/null @@ -1,345 +0,0 @@ -"""Test for invalid trades due to trade too small.""" - -from __future__ import annotations - -from typing import TYPE_CHECKING - -import pytest -from fixedpointmath import FixedPoint -from pypechain.core import PypechainCallException -from utils import expect_failure_with_funded_bot # type: ignore -from web3.exceptions import ContractCustomError - -from agent0.core.base import Trade -from agent0.core.hyperdrive import HyperdriveMarketAction, HyperdriveWallet -from agent0.core.hyperdrive.agent import ( - add_liquidity_trade, - close_long_trade, - close_short_trade, - open_long_trade, - open_short_trade, - remove_liquidity_trade, -) -from agent0.core.hyperdrive.interactive import LocalHyperdrive -from agent0.core.hyperdrive.policies import HyperdriveBasePolicy - -if TYPE_CHECKING: - from agent0.ethpy.hyperdrive import HyperdriveReadInterface - -# ruff: noqa: PLR2004 (magic values used for counter) - - -# Start by defining policies for failed trades -# One policy per failed trade - -SMALL_TRADE_AMOUNT = FixedPoint(scaled_value=1000) - - -class InvalidAddLiquidity(HyperdriveBasePolicy): - """An agent that submits an invalid add liquidity due to min txn amount.""" - - def action( - self, interface: HyperdriveReadInterface, wallet: HyperdriveWallet - ) -> tuple[list[Trade[HyperdriveMarketAction]], bool]: - """Add liquidity. - - Arguments - --------- - interface: HyperdriveReadInterface - The trading market interface. - wallet: HyperdriveWallet - The agent's wallet. - - Returns - ------- - tuple[list[HyperdriveMarketAction], bool] - A tuple where the first element is a list of actions, - and the second element defines if the agent is done trading - """ - # pylint: disable=unused-argument - action_list = [add_liquidity_trade(SMALL_TRADE_AMOUNT)] - return action_list, True - - -class InvalidRemoveLiquidity(HyperdriveBasePolicy): - """An agent that submits an invalid remove liquidity due to min txn amount.""" - - counter = 0 - - def action( - self, interface: HyperdriveReadInterface, wallet: HyperdriveWallet - ) -> tuple[list[Trade[HyperdriveMarketAction]], bool]: - """Remove liquidity. - - Arguments - --------- - interface: HyperdriveReadInterface - The trading market interface. - wallet: HyperdriveWallet - The agent's wallet. - - Returns - ------- - tuple[list[HyperdriveMarketAction], bool] - A tuple where the first element is a list of actions, - and the second element defines if the agent is done trading - """ - # pylint: disable=unused-argument - action_list = [] - done_trading = False - if self.counter == 0: - # Add liquidity - action_list.append(add_liquidity_trade(FixedPoint(10_000))) - elif self.counter == 2: - # Remove liquidity - action_list.append(remove_liquidity_trade(SMALL_TRADE_AMOUNT)) - done_trading = True - self.counter += 1 - return action_list, done_trading - - -class InvalidOpenLong(HyperdriveBasePolicy): - """An agent that submits an invalid open long due to min txn amount.""" - - def action( - self, interface: HyperdriveReadInterface, wallet: HyperdriveWallet - ) -> tuple[list[Trade[HyperdriveMarketAction]], bool]: - """Open long. - - Arguments - --------- - interface: HyperdriveReadInterface - The trading market interface. - wallet: HyperdriveWallet - The agent's wallet. - - Returns - ------- - tuple[list[HyperdriveMarketAction], bool] - A tuple where the first element is a list of actions, - and the second element defines if the agent is done trading - """ - # pylint: disable=unused-argument - action_list = [] - # Closing non-existent long - action_list.append(open_long_trade(SMALL_TRADE_AMOUNT, self.slippage_tolerance)) - return action_list, True - - -class InvalidOpenShort(HyperdriveBasePolicy): - """An agent that submits an invalid open short due to min txn amount.""" - - def action( - self, interface: HyperdriveReadInterface, wallet: HyperdriveWallet - ) -> tuple[list[Trade[HyperdriveMarketAction]], bool]: - """Open short. - - Arguments - --------- - interface: HyperdriveReadInterface - The trading market interface. - wallet: HyperdriveWallet - The agent's wallet. - - Returns - ------- - tuple[list[HyperdriveMarketAction], bool] - A tuple where the first element is a list of actions, - and the second element defines if the agent is done trading - """ - # pylint: disable=unused-argument - # Open a short for too few bonds - action_list = [open_short_trade(SMALL_TRADE_AMOUNT, self.slippage_tolerance)] - return action_list, True - - -class InvalidCloseLong(HyperdriveBasePolicy): - """An agent that submits an invalid close long due to min txn amount.""" - - counter = 0 - - def action( - self, interface: HyperdriveReadInterface, wallet: HyperdriveWallet - ) -> tuple[list[Trade[HyperdriveMarketAction]], bool]: - """Close long. - - Arguments - --------- - interface: HyperdriveReadInterface - The trading market interface. - wallet: HyperdriveWallet - The agent's wallet. - - Returns - ------- - tuple[list[HyperdriveMarketAction], bool] - A tuple where the first element is a list of actions, - and the second element defines if the agent is done trading - """ - # pylint: disable=unused-argument - action_list = [] - done_trading = False - if self.counter == 0: - # Add liquidity for other valid trades - action_list.append(add_liquidity_trade(FixedPoint(100_000))) - if self.counter == 1: - # Open Long - action_list.append(open_long_trade(FixedPoint(10_000), self.slippage_tolerance)) - elif self.counter == 2: - # Closing existing long for a small trade amount - assert len(wallet.longs) == 1 - for long_time in wallet.longs.keys(): - action_list.append(close_long_trade(SMALL_TRADE_AMOUNT, long_time, self.slippage_tolerance)) - done_trading = True - self.counter += 1 - return action_list, done_trading - - -class InvalidCloseShort(HyperdriveBasePolicy): - """An agent that submits an invalid close short due to min txn amount.""" - - counter = 0 - - def action( - self, interface: HyperdriveReadInterface, wallet: HyperdriveWallet - ) -> tuple[list[Trade[HyperdriveMarketAction]], bool]: - """Close short. - - Arguments - --------- - interface: HyperdriveReadInterface - The trading market interface. - wallet: HyperdriveWallet - The agent's wallet. - - Returns - ------- - tuple[list[HyperdriveMarketAction], bool] - A tuple where the first element is a list of actions, - and the second element defines if the agent is done trading - """ - # pylint: disable=unused-argument - action_list = [] - done_trading = False - if self.counter == 0: - # Add liquidity for other valid trades - action_list.append(add_liquidity_trade(FixedPoint(100_000))) - if self.counter == 1: - # Open Short - action_list.append(open_short_trade(FixedPoint(10_000), self.slippage_tolerance)) - elif self.counter == 2: - # Closing existent short for more than I have - assert len(wallet.shorts) == 1 - for short_time in wallet.shorts.keys(): - action_list.append(close_short_trade(SMALL_TRADE_AMOUNT, short_time, self.slippage_tolerance)) - done_trading = True - self.counter += 1 - return action_list, done_trading - - -class TestMinTxAmount: - """Test pipeline from bots making invalid trades.""" - - @pytest.mark.anvil - def test_invalid_add_liquidity_min_txn( - self, - fast_hyperdrive_fixture: LocalHyperdrive, - ): - try: - expect_failure_with_funded_bot(fast_hyperdrive_fixture, InvalidAddLiquidity) - except PypechainCallException as exc: - # Expected error due to illegal trade - # We do add an argument for invalid balance to the args, so check for that here - assert "Minimum Transaction Amount:" in exc.args[0] - # Fails on remove liquidity - assert exc.function_name == "addLiquidity" - assert exc.decoded_error == "MinimumTransactionAmount()" - assert exc.orig_exception is not None - assert isinstance(exc.orig_exception, ContractCustomError) - - @pytest.mark.anvil - def test_invalid_remove_liquidity_min_txn( - self, - fast_hyperdrive_fixture: LocalHyperdrive, - ): - try: - expect_failure_with_funded_bot(fast_hyperdrive_fixture, InvalidRemoveLiquidity) - except PypechainCallException as exc: - # Expected error due to illegal trade - # We do add an argument for invalid balance to the args, so check for that here - assert "Minimum Transaction Amount:" in exc.args[0] - # Fails on remove liquidity - assert exc.function_name == "removeLiquidity" - assert exc.decoded_error == "MinimumTransactionAmount()" - assert exc.orig_exception is not None - assert isinstance(exc.orig_exception, ContractCustomError) - - # We don't test withdrawal shares because redeeming withdrawal shares are not subject to min_txn_amount - - @pytest.mark.anvil - def test_invalid_open_long_min_txn( - self, - fast_hyperdrive_fixture: LocalHyperdrive, - ): - try: - expect_failure_with_funded_bot(fast_hyperdrive_fixture, InvalidOpenLong) - except PypechainCallException as exc: - # Expected error due to illegal trade - # We do add an argument for invalid balance to the args, so check for that here - assert "Minimum Transaction Amount:" in exc.args[0] - # Fails on remove liquidity - assert exc.function_name == "openLong" - assert exc.decoded_error == "MinimumTransactionAmount()" - assert exc.orig_exception is not None - assert isinstance(exc.orig_exception, ContractCustomError) - - @pytest.mark.anvil - def test_invalid_open_short_min_txn( - self, - fast_hyperdrive_fixture: LocalHyperdrive, - ): - try: - expect_failure_with_funded_bot(fast_hyperdrive_fixture, InvalidOpenShort) - except PypechainCallException as exc: - # Expected error due to illegal trade - # We do add an argument for invalid balance to the args, so check for that here - assert "Minimum Transaction Amount:" in exc.args[0] - # Fails on remove liquidity - assert exc.function_name == "openShort" - assert exc.decoded_error == "MinimumTransactionAmount()" - assert exc.orig_exception is not None - assert isinstance(exc.orig_exception, ContractCustomError) - - @pytest.mark.anvil - def test_invalid_close_long_min_txn( - self, - fast_hyperdrive_fixture: LocalHyperdrive, - ): - try: - expect_failure_with_funded_bot(fast_hyperdrive_fixture, InvalidCloseLong) - except PypechainCallException as exc: - # Expected error due to illegal trade - # We do add an argument for invalid balance to the args, so check for that here - assert "Minimum Transaction Amount:" in exc.args[0] - # Fails on remove liquidity - assert exc.function_name == "closeLong" - assert exc.decoded_error == "MinimumTransactionAmount()" - assert exc.orig_exception is not None - assert isinstance(exc.orig_exception, ContractCustomError) - - @pytest.mark.anvil - def test_invalid_close_short_min_txn( - self, - fast_hyperdrive_fixture: LocalHyperdrive, - ): - try: - expect_failure_with_funded_bot(fast_hyperdrive_fixture, InvalidCloseShort) - except PypechainCallException as exc: - # Expected error due to illegal trade - # We do add an argument for invalid balance to the args, so check for that here - assert "Minimum Transaction Amount:" in exc.args[0] - # Fails on remove liquidity - assert exc.function_name == "closeShort" - assert exc.decoded_error == "MinimumTransactionAmount()" - assert exc.orig_exception is not None - assert isinstance(exc.orig_exception, ContractCustomError)