Skip to content

Commit

Permalink
fixes and pools tests init
Browse files Browse the repository at this point in the history
  • Loading branch information
amfet42 committed Jun 28, 2023
1 parent 088e621 commit 785369a
Show file tree
Hide file tree
Showing 10 changed files with 203 additions and 22 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
on: [pull_request, push]

name: lint

on: [pull_request, push]

jobs:

lint:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: gauge
name: test_gauge

on:
pull_request:
Expand Down
41 changes: 41 additions & 0 deletions .github/workflows/test_pools_2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: test_gauge

on: [pull_request, push]

env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
WEB3_PROVIDER_URL: ${{ secrets.WEB3_PROVIDER_URL }}

jobs:
tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Cache Compiler Installations
uses: actions/cache@v3
with:
path: |
~/.vvm
key: compiler-cache

- name: Setup Python 3.10.4
uses: actions/setup-python@v4
with:
python-version: 3.10.4

- name: Install Requirements
run: |
pip install poetry==1.5.1
poetry config virtualenvs.in-project true
poetry install --no-interaction
- name: Run All Token Tests 18,18
run: |
source .venv/bin/activate
pytest tests/pools/ --pool-size=2 --pool-type=basic --decimals=18,18 -n auto
- name: Run Plain Tests 18,6
run: |
source .venv/bin/activate
pytest tests/pools/ --pool-size=2 --pool-type=basic --token-types=plain --decimals=18,6 -n auto
24 changes: 11 additions & 13 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def pytest_addoption(parser):
"--pool-size",
action="store",
default="2",
help="comma-separated list of plain pool sizes to test against",
help="pool size to test against",
)
# TODO: add meta implementation
parser.addoption(
Expand Down Expand Up @@ -52,18 +52,16 @@ def pytest_addoption(parser):


def pytest_generate_tests(metafunc):
pool_size = int(metafunc.config.getoption("pool_size"))
if "pool_size" in metafunc.fixturenames:
cli_options = metafunc.config.getoption("pool_size").split(",")
pool_sizes = [int(v) for v in cli_options]

# TODO: remove after adding implementations
assert pool_sizes == [2], "Only 2-coin pools supported"
assert pool_size == 2, "Only 2-coin pools supported"

metafunc.parametrize(
"pool_size",
pool_sizes,
[pool_size],
indirect=True,
ids=[f"(PoolSize={i})" for i in cli_options],
ids=[f"(PoolSize={pool_size})"],
)

if "pool_type" in metafunc.fixturenames:
Expand All @@ -79,12 +77,12 @@ def pytest_generate_tests(metafunc):
if "pool_token_types" in metafunc.fixturenames:
cli_options = metafunc.config.getoption("token_types").split(",")

# TODO: only 2-coin pools are supported
combs = list(combinations(cli_options, 2))
# do not include (eth,eth) pair
for t in cli_options:
if t != "eth":
combs.append((t, t))
combs = list(combinations(cli_options, pool_size))
if pool_size == 2:
# do not include (eth,eth) pair
for t in cli_options:
if t != "eth":
combs.append((t, t))

metafunc.parametrize(
"pool_token_types",
Expand Down
24 changes: 19 additions & 5 deletions tests/fixtures/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ def owner() -> AddressType:
return boa.env.generate_address()


@pytest.fixture(scope="module")
def factory_admin(factory) -> AddressType:
return factory.admin()


@pytest.fixture(scope="module")
def fee_receiver() -> AddressType:
return boa.env.generate_address()
Expand Down Expand Up @@ -61,6 +56,11 @@ def frank():
return boa.env.generate_address()


@pytest.fixture(scope="module")
def accounts(bob, charlie, dave, erin, frank):
return [bob, charlie, dave, erin, frank]


# <--------------------- Functions --------------------->
@pytest.fixture(scope="module")
def mint_owner(owner, pool_tokens, initial_amounts):
Expand All @@ -74,3 +74,17 @@ def approve_owner(owner, pool_tokens, swap):
for pool_token in pool_tokens:
with boa.env.prank(owner):
pool_token.approve(swap.address, 2**256 - 1)


@pytest.fixture(scope="module")
def mint_alice(alice, pool_tokens, initial_amounts):
mint_for_testing(alice, 10**18, None, True)
for pool_token, amount in zip(pool_tokens, initial_amounts):
mint_for_testing(alice, amount, pool_token, False)


@pytest.fixture(scope="module")
def approve_alice(alice, pool_tokens, swap):
for pool_token in pool_tokens:
with boa.env.prank(alice):
pool_token.approve(swap.address, 2**256 - 1)
7 changes: 7 additions & 0 deletions tests/fixtures/pools.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def swap(
owner,
mint_owner,
factory,
weth,
pool_token_types,
pool_tokens,
amm_interface_plain,
Expand Down Expand Up @@ -71,3 +72,9 @@ def swap(
def add_initial_liquidity(owner, approve_owner, mint_owner, deposit_amounts, swap):
with boa.env.prank(owner):
swap.add_liquidity(deposit_amounts, 0)


@pytest.fixture
def add_initial_liquidity_alice(alice, approve_alice, mint_alice, deposit_amounts, swap):
with boa.env.prank(alice):
swap.add_liquidity(deposit_amounts, 0)
3 changes: 2 additions & 1 deletion tests/fixtures/tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ def pool_tokens(pool_token_types, plain_tokens, weth, oracle_tokens, rebase_toke
if t == 0:
pool_tokens.append(plain_tokens[i])
elif t == 1:
pool_tokens.append(weth)
# Enforce eth as 0th token
pool_tokens = [weth] + pool_tokens
elif t == 2:
pool_tokens.append(oracle_tokens[i])
elif t == 3:
Expand Down
Empty file added tests/pools/oracle/__init__.py
Empty file.
Empty file.
120 changes: 120 additions & 0 deletions tests/pools/test_token.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import boa
import pytest

# from eip712.messages import EIP712Message


@pytest.mark.usefixtures("add_initial_liquidity_alice")
class TestPoolToken:
class TestTokenApprove:
@pytest.mark.parametrize("idx", range(4))
def test_initial_approval_is_zero(self, swap, alice, accounts, idx):
assert swap.allowance(alice, accounts[idx]) == 0

def test_approve(self, swap, alice, bob):
swap.approve(bob, 10**19, sender=alice)
assert swap.allowance(alice, bob) == 10**19

def test_modify_approve_zero_nonzero(self, swap, alice, bob):
with boa.env.prank(alice):
swap.approve(bob, 10**19)
swap.approve(bob, 0)
swap.approve(bob, 12345678)

assert swap.allowance(alice, bob) == 12345678

def test_revoke_approve(self, swap, alice, bob):
with boa.env.prank(alice):
swap.approve(bob, 10**19)
swap.approve(bob, 0)

assert swap.allowance(alice, bob) == 0

def test_approve_self(self, swap, alice):
swap.approve(alice, 10**19, sender=alice)
assert swap.allowance(alice, alice) == 10**19

def test_only_affects_target(self, swap, alice, bob):
swap.approve(bob, 10**19, sender=alice)
assert swap.allowance(bob, alice) == 0

def test_returns_true(self, swap, alice, bob):
tx = swap.approve(bob, 10**19, sender=alice)
assert tx is True

# TODO: add event processing
# def test_approval_event_fires(self, alice, bob, swap):
# tx = swap.approve(bob, 10 ** 19, sender=alice)
#
# events = swap.get_logs()
# print(events, type(events[0]))
#
# assert len(tx.events) == 1
# assert tx.events["Approval"].values() == [alice, bob, 10 ** 19]

def test_infinite_approval(self, swap, alice, bob):
swap.approve(bob, 2**256 - 1, sender=alice)
swap.transferFrom(alice, bob, 10**18, sender=bob)

assert swap.allowance(alice, bob) == 2**256 - 1

# def test_permit(self, eth_acc, bob, swap):
# class Permit(EIP712Message):
# # EIP-712 Domain Fields
# _name_: "string" = swap.name() # noqa: F821
# _version_: "string" = swap.version() # noqa: F821
# _chainId_: "uint256" = boa.env.chain.chain_id # noqa: F821
# _verifyingContract_: "address" = swap.address # noqa: F821
#
# # EIP-2612 Data Fields
# owner: "address" # noqa: F821
# spender: "address" # noqa: F821
# value: "uint256" # noqa: F821
# nonce: "uint256" # noqa: F821
# deadline: "uint256" = 2 ** 256 - 1 # noqa: F821
#
# permit = Permit(owner=eth_acc.address, spender=bob, value=2 ** 256 - 1, nonce=0)
# sig = eth_acc.sign_message(permit.signable_message)
#
# with boa.env.prank(bob):
# tx = swap.permit(eth_acc.address, bob, 2 ** 256 - 1, 2 ** 256 - 1, sig.v, sig.r, sig.s)
#
# assert swap.allowance(eth_acc.address, bob) == 2 ** 256 - 1
# assert tx is True
# assert len(swap.get_logs()) == 1
# assert swap.get_logs().events["Approval"].values() == [eth_acc.address.address, bob, 2 ** 256 - 1]
# assert swap.nonces(eth_acc.address) == 1
#
# def test_permit_contract(accounts, bob, chain, swap, web3):
# src = """
# @view
# @external
# def isValidSignature(_hash: bytes32, _sig: Bytes[65]) -> bytes32:
# return 0x1626ba7e00000000000000000000000000000000000000000000000000000000
# """
# mock_contract = brownie.compile_source(src, vyper_version="0.3.1").Vyper.deploy({"from": bob})
# alice = accounts.add("0x416b8a7d9290502f5661da81f0cf43893e3d19cb9aea3c426cfb36e8186e9c09")
#
# class Permit(EIP712Message):
# # EIP-712 Domain Fields
# _name_: "string" = swap.name() # noqa: F821
# _version_: "string" = swap.version() # noqa: F821
# _chainId_: "uint256" = chain.id # noqa: F821
# _verifyingContract_: "address" = swap.address # noqa: F821
#
# # EIP-2612 Data Fields
# owner: "address" # noqa: F821
# spender: "address" # noqa: F821
# value: "uint256" # noqa: F821
# nonce: "uint256" # noqa: F821
# deadline: "uint256" = 2 ** 256 - 1 # noqa: F821
#
# permit = Permit(owner=alice.address, spender=bob.address, value=2 ** 256 - 1, nonce=0)
# sig = alice.sign_message(permit)
#
# tx = swap.permit(
# mock_contract, bob, 2 ** 256 - 1, 2 ** 256 - 1, sig.v, sig.r, sig.s, {"from": bob}
# )
#
# # make sure this is hit when owner is a contract
# assert tx.subcalls[-1]["function"] == "isValidSignature(bytes32,bytes)"

0 comments on commit 785369a

Please sign in to comment.