From 745a8204fdbd4c96e1969f0ac71b91bbe10daba0 Mon Sep 17 00:00:00 2001 From: Alberto Date: Tue, 11 Feb 2025 15:47:54 +0100 Subject: [PATCH] test: update tests to use new mock also removed weth as it's not relevant for testing anymore since the latest version of the pool doesn't wrap/unwrap weth anymore --- tests/fixtures/tokens.py | 18 +- tests/integration/test_create2_deployment.py | 2 +- tests/profiling/conftest.py | 167 +++++++++++++++++++ tests/stateful/stateful_base.py | 3 +- tests/utils/strategies.py | 3 +- 5 files changed, 177 insertions(+), 16 deletions(-) create mode 100644 tests/profiling/conftest.py diff --git a/tests/fixtures/tokens.py b/tests/fixtures/tokens.py index d16e384d..9b556905 100644 --- a/tests/fixtures/tokens.py +++ b/tests/fixtures/tokens.py @@ -2,46 +2,40 @@ import pytest -@pytest.fixture(scope="module") -def weth(deployer): - with boa.env.prank(deployer): - return boa.load("contracts/mocks/WETH.vy") - - @pytest.fixture(scope="module") def usd(deployer): with boa.env.prank(deployer): - return boa.load("contracts/mocks/ERC20Mock.vy", "USD", "USD", 18) + return boa.load("tests/mocks/MockERC20.vy", "USD", "USD", 18) @pytest.fixture(scope="module") def btc(deployer): with boa.env.prank(deployer): - return boa.load("contracts/mocks/ERC20Mock.vy", "BTC", "BTC", 18) + return boa.load("tests/mocks/MockERC20.vy", "BTC", "BTC", 18) @pytest.fixture(scope="module") def stg(deployer): with boa.env.prank(deployer): - return boa.load("contracts/mocks/ERC20Mock.vy", "STG", "STG", 18) + return boa.load("tests/mocks/MockERC20.vy", "STG", "STG", 18) @pytest.fixture(scope="module") def usdt(deployer): with boa.env.prank(deployer): - return boa.load("contracts/mocks/ERC20Mock.vy", "USDT", "USDT", 6) + return boa.load("tests/mocks/MockERC20.vy", "USDT", "USDT", 6) @pytest.fixture(scope="module") def usdc(deployer): with boa.env.prank(deployer): - return boa.load("contracts/mocks/ERC20Mock.vy", "USDC", "USDC", 6) + return boa.load("tests/mocks/MockERC20.vy", "USDC", "USDC", 6) @pytest.fixture(scope="module") def dai(deployer): with boa.env.prank(deployer): - return boa.load("contracts/mocks/ERC20Mock.vy", "DAI", "DAI", 18) + return boa.load("tests/mocks/MockERC20.vy", "DAI", "DAI", 18) @pytest.fixture(scope="module") diff --git a/tests/integration/test_create2_deployment.py b/tests/integration/test_create2_deployment.py index f9c62499..a2404c10 100644 --- a/tests/integration/test_create2_deployment.py +++ b/tests/integration/test_create2_deployment.py @@ -159,7 +159,7 @@ def factory( @pytest.fixture(scope="module") def coins(): - erc20_mock = boa.load_partial("./contracts/mocks/ERC20Mock.vy") + erc20_mock = boa.load_partial("./tests/mocks/MockERC20.vy") return [ erc20_mock.at("0xf939E0A03FB07F59A73314E73794Be0E57ac1b4E"), erc20_mock.at("0xD533a949740bb3306d119CC777fa900bA034cd52"), diff --git a/tests/profiling/conftest.py b/tests/profiling/conftest.py new file mode 100644 index 00000000..3ce1993e --- /dev/null +++ b/tests/profiling/conftest.py @@ -0,0 +1,167 @@ +import boa +import pytest +from hypothesis import assume + +from contracts.experimental.initial_guess import ( + CurveCryptoMathOptimized2 as math_deployer_initial_guess, +) +from contracts.experimental.initial_guess import ( + CurveTwocryptoOptimized as amm_deployer_initial_guess, +) + +# compiling contracts +from contracts.main import CurveCryptoMathOptimized2 as math_deployer +from contracts.main import CurveCryptoViews2Optimized as view_deployer +from contracts.main import CurveTwocryptoFactory as factory_deployer +from contracts.main import CurveTwocryptoOptimized as amm_deployer +from tests.utils.tokens import mint_for_testing + +# ---------------- addresses ---------------- +address = boa.test.strategy("address") +deployer = address +fee_receiver = address +owner = address +params = { + "A": 400000, + "gamma": 145000000000000, + "mid_fee": 26000000, + "out_fee": 45000000, + "allowed_extra_profit": 2000000000000, + "fee_gamma": 230000000000000, + "adjustment_step": 146000000000000, + "ma_exp_time": 866, # # 600 seconds//math.log(2) + "price": 1 * 10**18, +} + + +def _deposit_initial_liquidity(pool, tokens): + # deposit: + user = boa.env.generate_address() + quantities = [10**6 * 10**36 // p for p in [10**18, params["price"]]] # $2M worth + + for coin, quantity in zip(tokens, quantities): + # mint coins for user: + mint_for_testing(coin, user, quantity) + assert coin.balanceOf(user) == quantity + + # approve crypto_swap to trade coin for user: + with boa.env.prank(user): + coin.approve(pool, 2**256 - 1) + + # Very first deposit + with boa.env.prank(user): + pool.add_liquidity(quantities, 0) + + return pool + + +@pytest.fixture(scope="module") +def tokens(): + return [ + boa.load("tests/mocks/MockERC20.vy", "tkn_a", "tkn_a", 18), + boa.load("tests/mocks/MockERC20.vy", "tkn_b", "tkn_b", 18), + ] + + +@pytest.fixture(scope="module") +def factory_no_initial_guess(): + _deployer = boa.env.generate_address() + _fee_receiver = boa.env.generate_address() + _owner = boa.env.generate_address() + + with boa.env.prank(_deployer): + amm_implementation = amm_deployer.deploy_as_blueprint() + math_contract = math_deployer.deploy() + view_contract = view_deployer.deploy() + + _factory = factory_deployer.deploy() + _factory.initialise_ownership(_fee_receiver, _owner) + + with boa.env.prank(_owner): + _factory.set_views_implementation(view_contract) + _factory.set_math_implementation(math_contract) + + # set pool implementations: + _factory.set_pool_implementation(amm_implementation, 0) + + return _factory + + +@pytest.fixture(scope="module") +def factory_initial_guess(): + _deployer = boa.env.generate_address() + _fee_receiver = boa.env.generate_address() + _owner = boa.env.generate_address() + + assume(_fee_receiver != _owner != _deployer) + + with boa.env.prank(_deployer): + amm_implementation = amm_deployer_initial_guess.deploy_as_blueprint() + math_contract = math_deployer_initial_guess.deploy() + view_contract = view_deployer.deploy() + + _factory = factory_deployer.deploy() + _factory.initialise_ownership(_fee_receiver, _owner) + + with boa.env.prank(_owner): + _factory.set_views_implementation(view_contract) + _factory.set_math_implementation(math_contract) + + # set pool implementations: + _factory.set_pool_implementation(amm_implementation, 0) + + return _factory + + +@pytest.fixture(scope="module") +def pool(factory, tokens): + with boa.env.prank(boa.env.generate_address()): + _pool = factory.deploy_pool( + "test_A", + "test_A", + tokens, + 0, + params["A"], + params["gamma"], + params["mid_fee"], + params["out_fee"], + params["fee_gamma"], + params["allowed_extra_profit"], + params["adjustment_step"], + params["ma_exp_time"], + params["price"], + ) + + _pool = amm_deployer.at(_pool) + return _deposit_initial_liquidity(_pool, tokens) + + +@pytest.fixture(scope="module") +def pool_initial_guess(factory_initial_guess, tokens): + with boa.env.prank(boa.env.generate_address()): + _pool = factory_initial_guess.deploy_pool( + "test_B", + "test_B", + tokens, + 0, + params["A"], + params["gamma"], + params["mid_fee"], + params["out_fee"], + params["fee_gamma"], + params["allowed_extra_profit"], + params["adjustment_step"], + params["ma_exp_time"], + params["price"], + ) + + _pool = amm_deployer_initial_guess.at(_pool) + return _deposit_initial_liquidity(_pool, tokens) + + +@pytest.fixture(scope="module") +def pools(pool, pool_initial_guess): + return [ + pool, + # pool_initial_guess, + ] diff --git a/tests/stateful/stateful_base.py b/tests/stateful/stateful_base.py index 2af4b1cd..2c55e44c 100644 --- a/tests/stateful/stateful_base.py +++ b/tests/stateful/stateful_base.py @@ -13,11 +13,12 @@ from hypothesis.strategies import integers from contracts.main import TwocryptoFactory as factory -from contracts.mocks import ERC20Mock as ERC20 from tests.utils.constants import UNIX_DAY from tests.utils.strategies import address, pool_from_preset from tests.utils.tokens import mint_for_testing +ERC20 = boa.load_partial("tests/mocks/MockERC20.vy") + class StatefulBase(RuleBasedStateMachine): pool = None diff --git a/tests/utils/strategies.py b/tests/utils/strategies.py index e842a166..38e16038 100644 --- a/tests/utils/strategies.py +++ b/tests/utils/strategies.py @@ -101,9 +101,8 @@ def fees(draw): # towards 18 in case of failure (instead of 2) token = sampled_from([18, 6, 2]).map( # token = just(18).map( - lambda x: boa.load("contracts/mocks/ERC20Mock.vy", "USD", "USD", x) + lambda x: boa.load("tests/mocks/MockERC20.vy", x) ) -weth = just(boa.load("contracts/mocks/WETH.vy")) # ---------------- pool ----------------