From 0a543545caacd3f8ff5664b0477ebcedba8256ef Mon Sep 17 00:00:00 2001 From: Daniel Schiavini Date: Mon, 22 Jan 2024 12:44:16 +0100 Subject: [PATCH] More fixture unscrambling --- tests/fixtures/accounts.py | 97 ++++++++----------- tests/fixtures/constants.py | 55 ++++++----- tests/fixtures/pools.py | 11 +-- tests/fixtures/tokens.py | 4 +- tests/pools/general/test_oracles.py | 16 +-- tests/pools/general/test_swap_getters.py | 4 +- .../pools/liquidity/test_initial_liquidity.py | 29 +++--- tests/pools/meta/test_exchange_underlying.py | 8 +- .../meta/test_exchange_underlying_reverts.py | 2 +- tests/utils/__init__.py | 2 +- 10 files changed, 98 insertions(+), 130 deletions(-) diff --git a/tests/fixtures/accounts.py b/tests/fixtures/accounts.py index 6f6ac746..b53a9380 100644 --- a/tests/fixtures/accounts.py +++ b/tests/fixtures/accounts.py @@ -121,72 +121,54 @@ def add_base_pool_liquidity(user, base_pool, base_pool_tokens, base_pool_decimal @pytest.fixture() -def add_initial_liquidity_owner( +def add_initial_liquidity_owner_basic( owner, approve_owner, mint_owner, deposit_amounts, - swap, - pool_type, + basic_swap, underlying_tokens, base_pool, base_pool_tokens, base_pool_decimals, base_pool_lp_token, ): - if pool_type == 0: - with boa.env.prank(owner): - swap.add_liquidity(deposit_amounts, 0) - else: - add_base_pool_liquidity(owner, base_pool, base_pool_tokens, base_pool_decimals) - with boa.env.prank(owner): - base_pool_lp_token.approve(swap.address, 2**256 - 1) - lp_token_bal = base_pool_lp_token.balanceOf(owner) - to_mint_token0 = lp_token_bal * 10 ** underlying_tokens[0].decimals() // 10 ** base_pool_lp_token.decimals() - - mint_for_testing(owner, to_mint_token0, underlying_tokens[0], False) - underlying_tokens[0].approve(swap.address, 2**256 - 1) - - swap.add_liquidity([to_mint_token0, lp_token_bal], 0) + with boa.env.prank(owner): + basic_swap.add_liquidity(deposit_amounts, 0) @pytest.fixture() -def add_initial_liquidity_alice( - alice, - approve_alice, - mint_alice, - deposit_amounts, - swap, - pool_type, +def add_initial_liquidity_owner_meta( + owner, + approve_owner, + mint_owner, + deposit_meta_amounts, + meta_swap, + metapool_token, base_pool, base_pool_tokens, base_pool_decimals, base_pool_lp_token, ): - if pool_type == 0: - with boa.env.prank(alice): - swap.add_liquidity(deposit_amounts, 0) - else: - add_base_pool_liquidity(alice, base_pool, base_pool_tokens, base_pool_decimals) - with boa.env.prank(alice): - base_pool_lp_token.approve(swap.address, 2**256 - 1) - swap.add_liquidity(deposit_amounts, 0) + add_base_pool_liquidity(owner, base_pool, base_pool_tokens, base_pool_decimals) + with boa.env.prank(owner): + base_pool_lp_token.approve(meta_swap.address, 2**256 - 1) + lp_token_bal = base_pool_lp_token.balanceOf(owner) + to_mint_token0 = lp_token_bal * 10 ** metapool_token.decimals() // 10 ** base_pool_lp_token.decimals() + + mint_for_testing(owner, to_mint_token0, metapool_token, False) + metapool_token.approve(meta_swap.address, 2**256 - 1) + + meta_swap.add_liquidity([to_mint_token0, lp_token_bal], 0) @pytest.fixture() -def mint_meta_bob( - bob, - mint_bob, - base_pool, - base_pool_tokens, - base_pool_decimals, - underlying_tokens, - initial_amounts, - base_pool_lp_token, -): - add_base_pool_liquidity(bob, base_pool, base_pool_tokens, base_pool_decimals) - mint_for_testing(bob, initial_amounts[0], underlying_tokens[0], False) - assert underlying_tokens[0].balanceOf(bob) == base_pool_lp_token.balanceOf(bob) +def add_initial_liquidity_owner(pool_type, request): + fixture_name = { + POOL_TYPES["basic"]: "add_initial_liquidity_owner_basic", + POOL_TYPES["meta"]: "add_initial_liquidity_owner_meta", + }[pool_type] + return request.getfixturevalue(fixture_name) @pytest.fixture() @@ -199,21 +181,21 @@ def approve_meta_bob(bob, underlying_tokens, swap): @pytest.fixture() def basic_setup( alice, - approve_alice, bob, mint_alice, - deposit_amounts, + deposit_basic_amounts, basic_swap, initial_balance, initial_amounts, pool_tokens, metapool_token_type, ): + approve_account(alice, pool_tokens, basic_swap) assert metapool_token_type is not None, "Fixture required downstream" mint_for_testing(bob, 1 * 10**18, None, True) with boa.env.prank(alice): - basic_swap.add_liquidity(deposit_amounts, 0) + basic_swap.add_liquidity(deposit_basic_amounts, 0) mint_account(bob, pool_tokens, initial_balance, initial_amounts) with boa.env.prank(bob): @@ -225,17 +207,20 @@ def basic_setup( def meta_setup( alice, bob, - mint_alice, - approve_alice, - deposit_amounts, + deposit_meta_amounts, meta_swap, base_pool, base_pool_tokens, base_pool_decimals, base_pool_lp_token, - initial_amounts, + initial_balance, + meta_initial_amounts, underlying_tokens, + pool_tokens, + add_initial_liquidity_owner_meta, ): + approve_account(alice, pool_tokens, meta_swap) + mint_account(alice, pool_tokens, initial_balance, meta_initial_amounts) mint_for_testing(bob, 1 * 10**18, None, True) underlying_token = underlying_tokens[0] @@ -251,10 +236,10 @@ def meta_setup( with boa.env.prank(alice): underlying_token.approve(meta_swap.address, 2**256 - 1) base_pool_lp_token.approve(meta_swap.address, 2**256 - 1) - meta_swap.add_liquidity(deposit_amounts, 0) + meta_swap.add_liquidity(deposit_meta_amounts, 0) add_base_pool_liquidity(bob, base_pool, base_pool_tokens, base_pool_decimals) - mint_for_testing(bob, initial_amounts[0], underlying_token, False) + mint_for_testing(bob, initial_balance, underlying_token, False) assert underlying_token.balanceOf(bob) == pytest.approx(base_pool_lp_token.balanceOf(bob)) with boa.env.prank(bob): @@ -263,11 +248,11 @@ def meta_setup( @pytest.fixture() -def initial_setup(pool_type, request, metapool_token_type): +def initial_setup(pool_type, request, metapool_token_type, pool_token_types, initial_decimals): """ Set up the initial state for a pool test. Run either basic_setup or meta_setup depending on the pool_type. """ - assert metapool_token_type is not None, "Fixture required downstream" + assert metapool_token_type is not None and pool_token_types and initial_decimals, "Fixtures required downstream" fixture_name = {POOL_TYPES["basic"]: "basic_setup", POOL_TYPES["meta"]: "meta_setup"}[pool_type] return request.getfixturevalue(fixture_name) diff --git a/tests/fixtures/constants.py b/tests/fixtures/constants.py index 66ff1091..c88fe092 100644 --- a/tests/fixtures/constants.py +++ b/tests/fixtures/constants.py @@ -9,38 +9,43 @@ def initial_balance() -> int: @pytest.fixture() -def initial_amounts(pool_type, decimals, meta_decimals) -> list[int]: - return ( - [INITIAL_AMOUNT * 10**precision for precision in decimals] - if pool_type == 0 - else [INITIAL_AMOUNT * 10**meta_decimals, INITIAL_AMOUNT * 10**18] - ) +def meta_initial_amounts(meta_decimals) -> list[int]: + return [INITIAL_AMOUNT * 10**meta_decimals, INITIAL_AMOUNT * 10**18] @pytest.fixture() -def deposit_amounts( - initial_amounts: list[int], pool_type, pool_token_types, metapool_token_type, pool_tokens, underlying_tokens +def initial_amounts(pool_type, decimals, meta_initial_amounts) -> list[int]: + return [INITIAL_AMOUNT * 10**precision for precision in decimals] if pool_type == 0 else meta_initial_amounts + + +@pytest.fixture() +def deposit_basic_amounts(initial_amounts: list[int], pool_token_types, pool_tokens) -> list[int]: + return [ + initial_amounts[i] * 10**18 // pool_token.exchangeRate() // 2 + if pool_token_type == 1 + else initial_amounts[i] // 2 + for i, (pool_token_type, pool_token) in enumerate(zip(pool_token_types, pool_tokens)) + ] + + +@pytest.fixture() +def deposit_meta_amounts( + meta_initial_amounts: list[int], metapool_token_type, pool_tokens, underlying_tokens ) -> list[int]: - amounts = [] + return [ + meta_initial_amounts[0] // 2 + if metapool_token_type != 1 + else meta_initial_amounts[0] * 10**18 // underlying_tokens[0].exchangeRate() // 2, + meta_initial_amounts[1] // 2, + ] + +@pytest.fixture() +def deposit_amounts(deposit_basic_amounts, deposit_meta_amounts, pool_type) -> list[int]: # This (almost) adds liquidity in balance for oracle tokens if pool_type == 0: - i = 0 - for ptt, pt in zip(pool_token_types, pool_tokens): - if ptt != 1: - amounts.append(initial_amounts[i] // 2) - else: - amounts.append(initial_amounts[i] * 10**18 // pt.exchangeRate() // 2) - i += 1 - else: - if metapool_token_type != 1: - amounts.append(initial_amounts[0] // 2) - else: - amounts.append(initial_amounts[0] * 10**18 // underlying_tokens[0].exchangeRate() // 2) - - amounts.append(initial_amounts[1] // 2) - - return amounts + return deposit_basic_amounts + return deposit_meta_amounts @pytest.fixture(scope="session") diff --git a/tests/fixtures/pools.py b/tests/fixtures/pools.py index 3767f71d..64ca9399 100644 --- a/tests/fixtures/pools.py +++ b/tests/fixtures/pools.py @@ -53,17 +53,8 @@ def basic_swap(deployer, factory, pool_size, pool_tokens, zero_address, amm_depl @pytest.fixture() def meta_swap( - factory, - set_metapool_implementations, - zero_address, - metapool_token, - base_pool, - meta_deployer, - add_base_pool, - pool_token_types, + factory, set_metapool_implementations, zero_address, metapool_token, base_pool, meta_deployer, add_base_pool ): - assert pool_token_types, "Fixture required downstream" - A = 2000 fee = 1000000 method_id = bytes(b"") diff --git a/tests/fixtures/tokens.py b/tests/fixtures/tokens.py index 882b04f1..1d24d2a8 100644 --- a/tests/fixtures/tokens.py +++ b/tests/fixtures/tokens.py @@ -44,8 +44,8 @@ def pool_tokens(pool_token_types, request, initial_decimals): # <--------------------- Metapool configuration ---------------------> @pytest.fixture() -def metapool_token(metapool_token_type, request, initial_decimals): - assert initial_decimals, "Fixture required for requesting `decimals` downstream" +def metapool_token(metapool_token_type, request, initial_decimals, pool_token_types): + assert initial_decimals and pool_token_types, "Fixtures required for requesting `decimals` downstream" fixture = { TOKEN_TYPES["plain"]: "plain_tokens", TOKEN_TYPES["oracle"]: "oracle_tokens", diff --git a/tests/pools/general/test_oracles.py b/tests/pools/general/test_oracles.py index 413eee23..4c930c9c 100644 --- a/tests/pools/general/test_oracles.py +++ b/tests/pools/general/test_oracles.py @@ -9,7 +9,7 @@ from tests.utils import approx from tests.utils.tokens import mint_for_testing -SETTINGS = {"max_examples": 1000, "deadline": None} +SETTINGS = {"max_examples": 100, "deadline": None, "suppress_health_check": [HealthCheck.function_scoped_fixture]} pytestmark = pytest.mark.usefixtures("initial_setup") @@ -44,10 +44,7 @@ def check_oracle(swap, dt): assert approx(swap.price_oracle(n), p1, 1e-5) -@given( - amount=strategy("uint256", min_value=1, max_value=10**6), - suppress_health_check=HealthCheck.function_scoped_fixture, -) +@given(amount=strategy("uint256", min_value=1, max_value=10**6)) @settings(**SETTINGS) def test_get_p(swap, views_implementation, bob, pool_tokens, decimals, amount): i, j = random.sample(range(swap.N_COINS()), 2) @@ -83,7 +80,6 @@ def test_get_p(swap, views_implementation, bob, pool_tokens, decimals, amount): amount=strategy("uint256", min_value=1, max_value=10**5), dt0=strategy("uint256", min_value=0, max_value=10**6), dt=strategy("uint256", min_value=0, max_value=10**6), - suppress_health_check=HealthCheck.function_scoped_fixture, ) @settings(**SETTINGS) def test_price_ema_exchange(swap, bob, pool_tokens, underlying_tokens, decimals, amount, dt0, dt): @@ -105,7 +101,6 @@ def test_price_ema_exchange(swap, bob, pool_tokens, underlying_tokens, decimals, amount=strategy("uint256", min_value=1, max_value=10**5), dt0=strategy("uint256", min_value=0, max_value=10**6), dt=strategy("uint256", min_value=0, max_value=10**6), - suppress_health_check=HealthCheck.function_scoped_fixture, ) @settings(**SETTINGS) def test_price_ema_remove_one(swap, alice, amount, dt0, dt): @@ -123,7 +118,6 @@ def test_price_ema_remove_one(swap, alice, amount, dt0, dt): frac=strategy("uint256", min_value=1, max_value=8), dt0=strategy("uint256", min_value=0, max_value=10**6), dt=strategy("uint256", min_value=0, max_value=10**6), - suppress_health_check=HealthCheck.function_scoped_fixture, ) @settings(**SETTINGS) def test_price_ema_remove_imbalance(swap, alice, dt0, dt, pool_size, deposit_amounts, frac): @@ -138,10 +132,7 @@ def test_price_ema_remove_imbalance(swap, alice, dt0, dt, pool_size, deposit_amo check_oracle(swap, dt) -@given( - amount=strategy("uint256", min_value=10**9, max_value=10**15), - suppress_health_check=HealthCheck.function_scoped_fixture, -) +@given(amount=strategy("uint256", min_value=10**9, max_value=10**15)) @settings(**SETTINGS) def test_manipulate_ema(basic_swap, bob, pool_tokens, underlying_tokens, decimals, amount): # calc amount in: @@ -170,7 +161,6 @@ def test_manipulate_ema(basic_swap, bob, pool_tokens, underlying_tokens, decimal amount=strategy("uint256", min_value=1, max_value=10**5), dt0=strategy("uint256", min_value=0, max_value=10**6), dt=strategy("uint256", min_value=0, max_value=10**6), - suppress_health_check=HealthCheck.function_scoped_fixture, ) @settings(**SETTINGS) def test_D_ema(swap, bob, pool_tokens, underlying_tokens, decimals, amount, dt0, dt, math_implementation): diff --git a/tests/pools/general/test_swap_getters.py b/tests/pools/general/test_swap_getters.py index 37081f75..17f6aa4b 100644 --- a/tests/pools/general/test_swap_getters.py +++ b/tests/pools/general/test_swap_getters.py @@ -2,14 +2,13 @@ from boa.test import strategy from hypothesis import HealthCheck, given, settings -SETTINGS = {"max_examples": 100, "deadline": None} +SETTINGS = {"max_examples": 100, "deadline": None, "suppress_health_check": [HealthCheck.function_scoped_fixture]} @given( amount_in=strategy("decimal", min_value=0.001, max_value=10**6), i=strategy("uint", min_value=0, max_value=2), j=strategy("uint", min_value=0, max_value=2), - suppress_health_check=HealthCheck.function_scoped_fixture, ) @settings(**SETTINGS) def test_get_dx(i, j, amount_in, swap, factory, initial_setup): @@ -31,7 +30,6 @@ def test_get_dx(i, j, amount_in, swap, factory, initial_setup): amount_in=strategy("decimal", min_value=0.001, max_value=10**6), i=strategy("uint", min_value=0, max_value=4), j=strategy("uint", min_value=0, max_value=4), - suppress_health_check=HealthCheck.function_scoped_fixture, ) @settings(**SETTINGS) def test_get_dx_underlying(i, j, amount_in, meta_swap, factory, initial_setup): diff --git a/tests/pools/liquidity/test_initial_liquidity.py b/tests/pools/liquidity/test_initial_liquidity.py index f09e59ec..cf48a36e 100644 --- a/tests/pools/liquidity/test_initial_liquidity.py +++ b/tests/pools/liquidity/test_initial_liquidity.py @@ -5,7 +5,7 @@ from tests.utils.tokens import mint_for_testing -@pytest.fixture(scope="module") +@pytest.fixture() def initial_setup_alice( alice, deposit_amounts, @@ -20,24 +20,21 @@ def initial_setup_alice( pool_tokens, underlying_tokens, ): - with boa.env.anchor(): - mint_for_testing(alice, 1 * 10**18, None, True) + mint_for_testing(alice, 1 * 10**18, None, True) - if pool_type == 0: - mint_account(alice, pool_tokens, initial_balance, initial_amounts) - with boa.env.prank(alice): - for token in pool_tokens: - token.approve(swap.address, 2**256 - 1) - - else: - add_base_pool_liquidity(alice, base_pool, base_pool_tokens, base_pool_decimals) - mint_for_testing(alice, initial_amounts[0], underlying_tokens[0], False) + if pool_type == 0: + mint_account(alice, pool_tokens, initial_balance, initial_amounts) + with boa.env.prank(alice): + for token in pool_tokens: + token.approve(swap.address, 2**256 - 1) - with boa.env.prank(alice): - for token in underlying_tokens: - token.approve(swap.address, 2**256 - 1) + else: + add_base_pool_liquidity(alice, base_pool, base_pool_tokens, base_pool_decimals) + mint_for_testing(alice, initial_amounts[0], underlying_tokens[0], False) - yield + with boa.env.prank(alice): + for token in underlying_tokens: + token.approve(swap.address, 2**256 - 1) @pytest.mark.parametrize("min_amount", [0, 10**18]) diff --git a/tests/pools/meta/test_exchange_underlying.py b/tests/pools/meta/test_exchange_underlying.py index 0cad2044..7b7ec435 100644 --- a/tests/pools/meta/test_exchange_underlying.py +++ b/tests/pools/meta/test_exchange_underlying.py @@ -4,11 +4,13 @@ from tests.utils import approx -pytestmark = pytest.mark.usefixtures("initial_setup") +pytestmark = pytest.mark.usefixtures("meta_setup") + +permutations = list(itertools.permutations(range(4), 2)) # 0,1...3,2 @pytest.mark.only_plain_tokens -@pytest.mark.parametrize("sending,receiving", filter(lambda k: 0 in k, itertools.permutations(range(4), 2))) +@pytest.mark.parametrize("sending,receiving", [p for p in permutations if 0 in p]) def test_amounts(bob, meta_swap, underlying_tokens, sending, receiving, meta_decimals, base_pool_decimals): underlying_decimals = [meta_decimals] + base_pool_decimals underlying_tokens = [underlying_tokens[0], *underlying_tokens[2:]] @@ -23,7 +25,7 @@ def test_amounts(bob, meta_swap, underlying_tokens, sending, receiving, meta_dec @pytest.mark.only_plain_tokens -@pytest.mark.parametrize("sending,receiving", itertools.permutations(range(4), 2)) +@pytest.mark.parametrize("sending,receiving", permutations) def test_min_dy_underlying(bob, meta_swap, underlying_tokens, sending, receiving, meta_decimals, base_pool_decimals): underlying_decimals = [meta_decimals] + base_pool_decimals underlying_tokens = [underlying_tokens[0], *underlying_tokens[2:]] diff --git a/tests/pools/meta/test_exchange_underlying_reverts.py b/tests/pools/meta/test_exchange_underlying_reverts.py index ddd621ba..3c5013ed 100644 --- a/tests/pools/meta/test_exchange_underlying_reverts.py +++ b/tests/pools/meta/test_exchange_underlying_reverts.py @@ -3,7 +3,7 @@ import boa import pytest -pytestmark = pytest.mark.usefixtures("initial_setup") +pytestmark = pytest.mark.usefixtures("meta_setup") @pytest.mark.parametrize("sending,receiving", itertools.permutations(range(4), 2)) diff --git a/tests/utils/__init__.py b/tests/utils/__init__.py index 8ef59ad0..bf970831 100644 --- a/tests/utils/__init__.py +++ b/tests/utils/__init__.py @@ -1,7 +1,7 @@ from math import log -def approx(x1: int, x2: int, precision: int, abs_precision=None): +def approx(x1: int, x2: int, precision: float, abs_precision=None): if precision >= 1: return True result = False