Skip to content

Commit

Permalink
Separate fixtures for meta
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielSchiavini committed Jan 22, 2024
1 parent 0a54354 commit ab7de7b
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 120 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,4 @@ jobs:
WEB3_PROVIDER_URL: ${{ secrets.WEB3_PROVIDER_URL }}
run: |
source .venv/bin/activate
pytest --exitfirst --numprocesses=auto tests/${{ matrix.name }}/
pytest --numprocesses=auto tests/${{ matrix.name }}/
50 changes: 13 additions & 37 deletions tests/fixtures/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,36 +78,6 @@ def approve_account(account, pool_tokens, swap):
pool_token.approve(swap.address, 2**256 - 1)


@pytest.fixture()
def mint_owner(owner, pool_tokens, initial_balance, initial_amounts):
mint_account(owner, pool_tokens, initial_balance, initial_amounts)


@pytest.fixture()
def approve_owner(owner, pool_tokens, swap):
approve_account(owner, pool_tokens, swap)


@pytest.fixture()
def mint_alice(alice, pool_tokens, initial_balance, initial_amounts):
mint_account(alice, pool_tokens, initial_balance, initial_amounts)


@pytest.fixture()
def approve_alice(alice, pool_tokens, swap):
approve_account(alice, pool_tokens, swap)


@pytest.fixture()
def mint_bob(bob, pool_tokens, initial_balance, initial_amounts):
mint_account(bob, pool_tokens, initial_balance, initial_amounts)


@pytest.fixture()
def approve_bob(bob, pool_tokens, swap):
approve_account(bob, pool_tokens, swap)


# <--------------------- Functions --------------------->
def add_base_pool_liquidity(user, base_pool, base_pool_tokens, base_pool_decimals):
amount = INITIAL_AMOUNT // 3
Expand All @@ -123,33 +93,39 @@ def add_base_pool_liquidity(user, base_pool, base_pool_tokens, base_pool_decimal
@pytest.fixture()
def add_initial_liquidity_owner_basic(
owner,
approve_owner,
mint_owner,
deposit_amounts,
basic_swap,
underlying_tokens,
base_pool,
base_pool_tokens,
base_pool_decimals,
base_pool_lp_token,
pool_tokens,
initial_balance,
basic_initial_amounts,
):
mint_account(owner, pool_tokens, initial_balance, basic_initial_amounts)
approve_account(owner, pool_tokens, basic_swap)
with boa.env.prank(owner):
basic_swap.add_liquidity(deposit_amounts, 0)


@pytest.fixture()
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,
pool_tokens,
initial_balance,
meta_initial_amounts,
):
mint_account(owner, pool_tokens, initial_balance, meta_initial_amounts)
approve_account(owner, pool_tokens, meta_swap)
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)
Expand Down Expand Up @@ -182,22 +158,22 @@ def approve_meta_bob(bob, underlying_tokens, swap):
def basic_setup(
alice,
bob,
mint_alice,
deposit_basic_amounts,
basic_swap,
initial_balance,
initial_amounts,
basic_initial_amounts,
pool_tokens,
metapool_token_type,
):
mint_account(alice, pool_tokens, initial_balance, basic_initial_amounts)
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_basic_amounts, 0)

mint_account(bob, pool_tokens, initial_balance, initial_amounts)
mint_account(bob, pool_tokens, initial_balance, basic_initial_amounts)
with boa.env.prank(bob):
for token in pool_tokens:
token.approve(basic_swap.address, 2**256 - 1)
Expand Down
9 changes: 7 additions & 2 deletions tests/fixtures/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ def meta_initial_amounts(meta_decimals) -> list[int]:


@pytest.fixture()
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
def basic_initial_amounts(decimals) -> list[int]:
return [INITIAL_AMOUNT * 10**precision for precision in decimals]


@pytest.fixture()
def initial_amounts(pool_type, basic_initial_amounts, meta_initial_amounts) -> list[int]:
return basic_initial_amounts if pool_type == 0 else meta_initial_amounts


@pytest.fixture()
Expand Down
4 changes: 3 additions & 1 deletion tests/fixtures/tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pytest

from tests.constants import TOKEN_TYPES
from tests.fixtures.accounts import mint_account


@pytest.fixture()
Expand Down Expand Up @@ -85,7 +86,8 @@ def coin_reward(owner, erc20_deployer):


@pytest.fixture()
def coin_reward_a(owner, mint_owner, erc20_deployer):
def coin_reward_a(owner, erc20_deployer, pool_tokens, initial_balance, initial_amounts):
mint_account(owner, pool_tokens, initial_balance, initial_amounts)
with boa.env.prank(owner):
return erc20_deployer.deploy("CRa", "CRa", 18)

Expand Down
138 changes: 64 additions & 74 deletions tests/pools/general/test_fees.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,83 +3,73 @@
pytestmark = pytest.mark.usefixtures("initial_setup")


class TestFees:
@pytest.mark.parametrize("sending,receiving", [(0, 1), (1, 0)])
def test_admin_balances(
self, bob, swap, pool_type, pool_tokens, underlying_tokens, initial_amounts, sending, receiving
):
for send, recv in [(sending, receiving), (receiving, sending)]:
swap.exchange(send, recv, initial_amounts[send], 0, sender=bob)

for i in (sending, receiving):
if pool_type == 0:
admin_fee = pool_tokens[i].balanceOf(swap) - swap.balances(i)
assert admin_fee > 0
else:
admin_fee = underlying_tokens[i].balanceOf(swap) - swap.balances(i)
assert admin_fee > 0

@pytest.mark.parametrize("sending,receiving", [(0, 1), (1, 0)])
def test_withdraw_one_coin(
self,
alice,
bob,
fee_receiver,
swap,
pool_type,
pool_tokens,
underlying_tokens,
sending,
receiving,
initial_amounts,
):
swap.exchange(sending, receiving, initial_amounts[sending], 0, sender=bob)

admin_balance = swap.admin_balances(receiving)

assert admin_balance > 0
assert swap.admin_balances(sending) == 0

swap.withdraw_admin_fees(sender=alice)
swap_balance = (
pool_tokens[receiving].balanceOf(swap) if pool_type == 0 else underlying_tokens[receiving].balanceOf(swap)
)
assert swap.balances(receiving) == swap_balance
expected_balance = (
pool_tokens[receiving].balanceOf(fee_receiver)
if pool_type == 0
else underlying_tokens[receiving].balanceOf(fee_receiver)
)

assert admin_balance == pytest.approx(expected_balance, abs=1) # +- 1

def test_no_fees(self, bob, fee_receiver, swap, pool_type, pool_tokens, underlying_tokens):
swap.withdraw_admin_fees(sender=bob)
@pytest.mark.parametrize("sending,receiving", [(0, 1), (1, 0)])
def test_admin_balances(bob, swap, pool_type, pool_tokens, underlying_tokens, initial_amounts, sending, receiving):
for send, recv in [(sending, receiving), (receiving, sending)]:
swap.exchange(send, recv, initial_amounts[send], 0, sender=bob)

for i in (sending, receiving):
if pool_type == 0:
for coin in pool_tokens:
assert coin.balanceOf(fee_receiver) == 0
admin_fee = pool_tokens[i].balanceOf(swap) - swap.balances(i)
assert admin_fee > 0
else:
for coin in underlying_tokens:
assert coin.balanceOf(fee_receiver) == 0
admin_fee = underlying_tokens[i].balanceOf(swap) - swap.balances(i)
assert admin_fee > 0

def test_withdraw_admin_fees(self, bob, swap, pool_type, pool_tokens, underlying_tokens, fee_receiver, decimals):
swap.exchange(1, 0, 10_000 * 10 ** decimals[1], 0, sender=bob)

fees = []
if pool_type == 0:
for i, coin in enumerate(pool_tokens):
assert coin.balanceOf(fee_receiver) == 0
fees.append(swap.admin_balances(i))
else:
for i, coin in enumerate(underlying_tokens[:2]):
assert coin.balanceOf(fee_receiver) == 0
fees.append(swap.admin_balances(i))
@pytest.mark.parametrize("sending,receiving", [(0, 1), (1, 0)])
def test_withdraw_one_coin(
alice, bob, fee_receiver, swap, pool_type, pool_tokens, underlying_tokens, sending, receiving, initial_amounts
):
swap.exchange(sending, receiving, initial_amounts[sending], 0, sender=bob)

swap.withdraw_admin_fees(sender=bob)
if pool_type == 0:
for i, coin in enumerate(pool_tokens):
assert coin.balanceOf(fee_receiver) == pytest.approx(fees[i], abs=1)
else:
for i, coin in enumerate(underlying_tokens[:2]):
assert coin.balanceOf(fee_receiver) == pytest.approx(fees[i], abs=1)
admin_balance = swap.admin_balances(receiving)

assert admin_balance > 0
assert swap.admin_balances(sending) == 0

swap.withdraw_admin_fees(sender=alice)
swap_balance = (
pool_tokens[receiving].balanceOf(swap) if pool_type == 0 else underlying_tokens[receiving].balanceOf(swap)
)
assert swap.balances(receiving) == swap_balance
expected_balance = (
pool_tokens[receiving].balanceOf(fee_receiver)
if pool_type == 0
else underlying_tokens[receiving].balanceOf(fee_receiver)
)

assert admin_balance == pytest.approx(expected_balance, abs=1) # +- 1


def test_no_fees(bob, fee_receiver, swap, pool_type, pool_tokens, underlying_tokens):
swap.withdraw_admin_fees(sender=bob)

if pool_type == 0:
for coin in pool_tokens:
assert coin.balanceOf(fee_receiver) == 0
else:
for coin in underlying_tokens:
assert coin.balanceOf(fee_receiver) == 0


def test_withdraw_admin_fees(bob, swap, pool_type, pool_tokens, underlying_tokens, fee_receiver, decimals):
swap.exchange(1, 0, 10_000 * 10 ** decimals[1], 0, sender=bob)

fees = []
if pool_type == 0:
for i, coin in enumerate(pool_tokens):
assert coin.balanceOf(fee_receiver) == 0
fees.append(swap.admin_balances(i))
else:
for i, coin in enumerate(underlying_tokens[:2]):
assert coin.balanceOf(fee_receiver) == 0
fees.append(swap.admin_balances(i))

swap.withdraw_admin_fees(sender=bob)
if pool_type == 0:
for i, coin in enumerate(pool_tokens):
assert coin.balanceOf(fee_receiver) == pytest.approx(fees[i], abs=1)
else:
for i, coin in enumerate(underlying_tokens[:2]):
assert coin.balanceOf(fee_receiver) == pytest.approx(fees[i], abs=1)
4 changes: 2 additions & 2 deletions tests/pools/general/test_oracles.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def test_price_ema_exchange(swap, bob, pool_tokens, underlying_tokens, decimals,
)
@settings(**SETTINGS)
def test_price_ema_remove_one(swap, alice, amount, dt0, dt):
i = random.sample(range(swap.N_COINS()), 1)[0]
i = random.choice(range(swap.N_COINS()))
alice_lp_bal = swap.balanceOf(alice)
amt_to_remove = int(alice_lp_bal * amount / (10**5 - 1))

Expand All @@ -121,7 +121,7 @@ def test_price_ema_remove_one(swap, alice, amount, dt0, dt):
)
@settings(**SETTINGS)
def test_price_ema_remove_imbalance(swap, alice, dt0, dt, pool_size, deposit_amounts, frac):
i = random.sample(range(swap.N_COINS()), 1)[0]
i = random.choice(range(swap.N_COINS()))
amounts = [0] * pool_size
amounts[i] = deposit_amounts[i] // frac
lp_balance = pool_size * deposit_amounts[i]
Expand Down
15 changes: 12 additions & 3 deletions tests/pools/meta/test_exchange_underlying_reverts.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,20 @@
import boa
import pytest

from tests.constants import TOKEN_TYPES

pytestmark = pytest.mark.usefixtures("meta_setup")

permutations = list(itertools.permutations(range(4), 2)) # 0,1...3,2


@pytest.mark.parametrize("sending,receiving", permutations)
def test_min_dy_too_high(
bob, meta_swap, underlying_tokens, meta_decimals, base_pool_decimals, sending, receiving, metapool_token_type
):
if sending == 0 and metapool_token_type == TOKEN_TYPES["rebasing"]:
return pytest.skip("This test does not revert sending rebasing tokens") # TODO

@pytest.mark.parametrize("sending,receiving", itertools.permutations(range(4), 2))
def test_min_dy_too_high(bob, meta_swap, underlying_tokens, meta_decimals, base_pool_decimals, sending, receiving):
underlying_decimals = [meta_decimals] + base_pool_decimals
underlying_tokens = [underlying_tokens[0], *underlying_tokens[2:]]
amount = 10 ** underlying_decimals[sending]
Expand All @@ -18,7 +27,7 @@ def test_min_dy_too_high(bob, meta_swap, underlying_tokens, meta_decimals, base_
meta_swap.exchange_underlying(sending, receiving, amount, min_dy, sender=bob)


@pytest.mark.parametrize("sending,receiving", itertools.permutations(range(4), 2))
@pytest.mark.parametrize("sending,receiving", permutations)
def test_insufficient_balance(
bob, meta_swap, underlying_tokens, meta_decimals, base_pool_decimals, sending, receiving, zero_address
):
Expand Down

0 comments on commit ab7de7b

Please sign in to comment.