Skip to content

Commit

Permalink
More fixture unscrambling
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielSchiavini committed Jan 22, 2024
1 parent d61b1b6 commit 0a54354
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 130 deletions.
97 changes: 41 additions & 56 deletions tests/fixtures/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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):
Expand All @@ -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]
Expand All @@ -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):
Expand All @@ -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)
55 changes: 30 additions & 25 deletions tests/fixtures/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
11 changes: 1 addition & 10 deletions tests/fixtures/pools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"")
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
16 changes: 3 additions & 13 deletions tests/pools/general/test_oracles.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")


Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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:
Expand Down Expand Up @@ -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):
Expand Down
4 changes: 1 addition & 3 deletions tests/pools/general/test_swap_getters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down
Loading

0 comments on commit 0a54354

Please sign in to comment.