Skip to content

Commit

Permalink
test: update tests to use new mock
Browse files Browse the repository at this point in the history
also removed weth as it's not relevant for testing anymore since the latest version of the pool doesn't wrap/unwrap weth anymore
  • Loading branch information
AlbertoCentonze committed Feb 24, 2025
1 parent cbd9392 commit 745a820
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 16 deletions.
18 changes: 6 additions & 12 deletions tests/fixtures/tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_create2_deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
167 changes: 167 additions & 0 deletions tests/profiling/conftest.py
Original file line number Diff line number Diff line change
@@ -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,
]
3 changes: 2 additions & 1 deletion tests/stateful/stateful_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions tests/utils/strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ----------------
Expand Down

0 comments on commit 745a820

Please sign in to comment.