Skip to content

Commit 0e8d416

Browse files
test: simplify fixtures
1 parent c24d533 commit 0e8d416

12 files changed

+220
-192
lines changed

tests/conftest.py

Lines changed: 155 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
from hypothesis import settings
77
from tests.utils.deploy import Protocol
88
from tests.utils.deployers import (
9-
ERC20_MOCK_DEPLOYER,
10-
DUMMY_PRICE_ORACLE_DEPLOYER
9+
ERC20_MOCK_DEPLOYER,
10+
CONSTANT_MONETARY_POLICY_DEPLOYER,
11+
CONSTANT_MONETARY_POLICY_LENDING_DEPLOYER,
1112
)
1213

1314

@@ -42,6 +43,40 @@ def factory(proto):
4243
def stablecoin(proto):
4344
return proto.crvUSD
4445

46+
@pytest.fixture(scope="module")
47+
def collateral_token():
48+
# TODO hook decimals fixture
49+
return ERC20_MOCK_DEPLOYER.deploy(18)
50+
51+
52+
@pytest.fixture(scope="module")
53+
def borrowed_token(stablecoin):
54+
"""Default borrowed token for lending tests (crvUSD).
55+
Specific test modules can override this if needed.
56+
"""
57+
# TODO should parametrize to use other tokens
58+
return stablecoin
59+
60+
61+
@pytest.fixture(scope="module")
62+
def lending_mpolicy_deployer():
63+
"""Default monetary policy deployer for lending markets: Constant policy.
64+
Tests can override to use a different lending policy.
65+
"""
66+
return CONSTANT_MONETARY_POLICY_LENDING_DEPLOYER
67+
68+
69+
@pytest.fixture(scope="module")
70+
def monetary_policy(market_type, controller):
71+
"""Monetary policy contract for the current market.
72+
Test modules can override by replacing the policy on the controller.
73+
"""
74+
if market_type == "lending":
75+
# TODO make both controllers use the same policy through a constructor adapter
76+
return CONSTANT_MONETARY_POLICY_LENDING_DEPLOYER.at(controller.monetary_policy())
77+
else:
78+
return CONSTANT_MONETARY_POLICY_DEPLOYER.at(controller.monetary_policy())
79+
4580

4681
@pytest.fixture(scope="module")
4782
def price_oracle(proto):
@@ -73,6 +108,123 @@ def mpolicy_impl(proto):
73108
return proto.blueprints.mpolicy
74109

75110

111+
@pytest.fixture(scope="module", params=["mint", "lending"])
112+
def market_type(request):
113+
return request.param
114+
115+
116+
@pytest.fixture(scope="module")
117+
def amm_A():
118+
return 1000
119+
120+
121+
@pytest.fixture(scope="module")
122+
def amm_fee():
123+
return 10**16
124+
125+
126+
@pytest.fixture(scope="module")
127+
def loan_discount():
128+
return int(0.09 * 10**18)
129+
130+
131+
@pytest.fixture(scope="module")
132+
def liquidation_discount():
133+
return int(0.06 * 10**18)
134+
135+
136+
@pytest.fixture(scope="module")
137+
def min_borrow_rate():
138+
return 10**15 // (365 * 86400) # 0.1% APR
139+
140+
141+
@pytest.fixture(scope="module")
142+
def max_borrow_rate():
143+
return 10**18 // (365 * 86400) # 100% APR
144+
145+
146+
@pytest.fixture(scope="module")
147+
def market(
148+
market_type,
149+
proto,
150+
collateral_token,
151+
price_oracle,
152+
seed_liquidity,
153+
borrowed_token,
154+
lending_mpolicy_deployer,
155+
amm_A,
156+
amm_fee,
157+
loan_discount,
158+
liquidation_discount,
159+
min_borrow_rate,
160+
max_borrow_rate,
161+
):
162+
if market_type == "mint":
163+
return proto.create_mint_market(
164+
collateral_token=collateral_token,
165+
price_oracle=price_oracle,
166+
monetary_policy=proto.mint_monetary_policy,
167+
A=amm_A,
168+
amm_fee=amm_fee,
169+
loan_discount=loan_discount,
170+
liquidation_discount=liquidation_discount,
171+
debt_ceiling=seed_liquidity,
172+
)
173+
else:
174+
return proto.create_lending_market(
175+
borrowed_token=borrowed_token,
176+
collateral_token=collateral_token,
177+
A=amm_A,
178+
fee=amm_fee,
179+
loan_discount=loan_discount,
180+
liquidation_discount=liquidation_discount,
181+
price_oracle=price_oracle,
182+
name="Test Vault",
183+
min_borrow_rate=min_borrow_rate,
184+
max_borrow_rate=max_borrow_rate,
185+
seed_amount=seed_liquidity,
186+
mpolicy_deployer=lending_mpolicy_deployer,
187+
)
188+
189+
190+
@pytest.fixture(scope="module")
191+
def controller(market, market_type, admin, borrow_cap):
192+
"""Controller for the current market (mint or lending).
193+
Sets borrow cap for lending markets to `borrow_cap`.
194+
"""
195+
ctrl = market['controller']
196+
if market_type == "lending" and borrow_cap is not None:
197+
with boa.env.prank(admin):
198+
ctrl.set_borrow_cap(borrow_cap)
199+
return ctrl
200+
201+
202+
203+
204+
@pytest.fixture(scope="module")
205+
def amm(market):
206+
"""AMM for the current market (mint or lending)."""
207+
return market['amm']
208+
209+
210+
@pytest.fixture(scope="module")
211+
def seed_liquidity():
212+
"""Default liquidity amount used to seed markets at creation time.
213+
Override in tests to customize seeding.
214+
"""
215+
return 1000 * 10**18
216+
217+
218+
@pytest.fixture(scope="module")
219+
def borrow_cap(seed_liquidity):
220+
"""Default borrow cap for lending markets equals `seed_liquidity`.
221+
Override to adjust or set to None to skip setting. Ignored for mint.
222+
"""
223+
return seed_liquidity
224+
225+
226+
227+
76228
# ============== Account Fixtures ==============
77229

78230
@pytest.fixture(scope="session")
@@ -96,6 +248,4 @@ def token_mock():
96248
return ERC20_MOCK_DEPLOYER
97249

98250

99-
@pytest.fixture(scope="module")
100-
def collateral_token():
101-
return ERC20_MOCK_DEPLOYER.deploy(18)
251+

tests/controller/test_set_price_oracle.py

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -13,53 +13,6 @@ def decimals():
1313
# change behavior with different decimals
1414
return 18
1515

16-
@pytest.fixture()
17-
def mint_market(proto, collat):
18-
return proto.create_mint_market(
19-
collat,
20-
proto.price_oracle,
21-
proto.mint_monetary_policy,
22-
A=1000,
23-
amm_fee=10**16,
24-
admin_fee=0,
25-
loan_discount= int(0.09 * 10**18),
26-
liquidation_discount=int(0.06 * 10**18),
27-
debt_ceiling=1000 * 10**18
28-
)
29-
30-
31-
@pytest.fixture()
32-
def lend_market(proto, collat):
33-
return proto.create_lending_market(
34-
borrowed_token=proto.crvUSD, # TODO param other tokens
35-
collateral_token=collat,
36-
A=1000,
37-
fee=10**16,
38-
loan_discount=int(0.09 * 10**18),
39-
liquidation_discount=int(0.06 * 10**18),
40-
price_oracle=proto.price_oracle,
41-
name="Test Vault",
42-
min_borrow_rate=10**15 // (365 * 86400), # 0.1% APR (per second)
43-
max_borrow_rate=10**18 // (365 * 86400) # 100% APR (per second)
44-
)
45-
46-
@pytest.fixture(params=["mint", "lending"])
47-
def market(request, mint_market, lend_market):
48-
"""Parametrized fixture that provides both mint and lending markets."""
49-
if request.param == "mint":
50-
return mint_market
51-
else:
52-
return lend_market
53-
54-
@pytest.fixture()
55-
def controller(market):
56-
"""Parametrized controller fixture that works with both market types."""
57-
return market['controller']
58-
59-
@pytest.fixture()
60-
def amm(market):
61-
return market['amm']
62-
6316

6417
@pytest.fixture(scope="module")
6518
def new_oracle(admin):
@@ -203,5 +156,3 @@ def test_same_price_different_oracle(controller, admin, amm):
203156
# Should succeed even with 0 deviation allowed
204157
controller.set_price_oracle(same_price_oracle, 0, sender=admin)
205158
assert amm.price_oracle_contract() == same_price_oracle.address
206-
207-

tests/lending/conftest.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,19 @@ def market_amm(lending_market):
6363

6464

6565
@pytest.fixture(scope="module")
66-
def market_mpolicy(market_controller):
66+
def monetary_policy(market_controller, borrowed_token, admin):
6767
from tests.utils.deployers import SEMILOG_MONETARY_POLICY_DEPLOYER
68-
return SEMILOG_MONETARY_POLICY_DEPLOYER.at(market_controller.monetary_policy())
68+
# Override default policy with Semilog for lending test suite
69+
min_borrow_rate = int(0.005 * 1e18) // (365 * 86400) # 0.5% APR
70+
max_borrow_rate = int(0.5 * 1e18) // (365 * 86400) # 50% APR
71+
with boa.env.prank(admin):
72+
mp = SEMILOG_MONETARY_POLICY_DEPLOYER.deploy(
73+
borrowed_token.address,
74+
min_borrow_rate,
75+
max_borrow_rate,
76+
)
77+
market_controller.set_monetary_policy(mp)
78+
return mp
6979

7080

7181
@pytest.fixture(scope="module")

tests/lending/test_bigfuzz.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -409,9 +409,9 @@ def rule_change_rate(self, min_rate, max_rate):
409409
with boa.env.prank(self.admin):
410410
if min_rate > max_rate or min(min_rate, max_rate) < MIN_RATE or max(min_rate, max_rate) > MAX_RATE:
411411
with boa.reverts():
412-
self.market_mpolicy.set_rates(min_rate, max_rate)
412+
self.monetary_policy.set_rates(min_rate, max_rate)
413413
else:
414-
self.market_mpolicy.set_rates(min_rate, max_rate)
414+
self.monetary_policy.set_rates(min_rate, max_rate)
415415

416416
@rule(dt=time_shift)
417417
def time_travel(self, dt):
@@ -430,7 +430,7 @@ def minted_redeemed(self):
430430

431431

432432
def test_big_fuzz(
433-
vault, borrowed_token, collateral_token, market_mpolicy, accounts, admin, market_amm, market_controller,
433+
vault, borrowed_token, collateral_token, monetary_policy, accounts, admin, market_amm, market_controller,
434434
price_oracle, fake_leverage):
435435
BigFuzz.TestCase.settings = settings(max_examples=2000, stateful_step_count=20)
436436
# Or quick check

tests/lending/test_health_in_trades.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def __init__(self):
2323
self.borrowed_mul = 10**(18 - self.borrowed_token.decimals())
2424
self.collateral_mul = 10**(18 - self.collateral_token.decimals())
2525
with boa.env.prank(self.admin):
26-
self.market_mpolicy.set_rates(int(1e18 * 0.04 / 365 / 86400), int(1e18 * 0.04 / 365 / 86400))
26+
self.monetary_policy.set_rates(int(1e18 * 0.04 / 365 / 86400), int(1e18 * 0.04 / 365 / 86400))
2727
for user in self.accounts[:2]:
2828
with boa.env.prank(user):
2929
self.borrowed_token.approve(self.controller, 2**256 - 1)
@@ -89,7 +89,7 @@ def time_travel(self, t):
8989
@rule(min_rate=rate, max_rate=rate)
9090
def change_rate(self, min_rate, max_rate):
9191
with boa.env.prank(self.admin):
92-
self.market_mpolicy.set_rates(min(min_rate, max_rate), max(min_rate, max_rate))
92+
self.monetary_policy.set_rates(min(min_rate, max_rate), max(min_rate, max_rate))
9393

9494
@invariant()
9595
def health(self):
@@ -98,7 +98,7 @@ def health(self):
9898
assert h > 0
9999

100100

101-
def test_adiabatic_follow(market_amm, filled_controller, market_mpolicy, collateral_token, borrowed_token, price_oracle, accounts, admin):
101+
def test_adiabatic_follow(market_amm, filled_controller, monetary_policy, collateral_token, borrowed_token, price_oracle, accounts, admin):
102102
AdiabaticTrader.TestCase.settings = settings(max_examples=50, stateful_step_count=50)
103103
for k, v in locals().items():
104104
setattr(AdiabaticTrader, k, v)

tests/lending/test_monetary_policy.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99

1010
@given(fill=st.floats(min_value=0.0, max_value=2.0))
11-
def test_monetary_policy(filled_controller, collateral_token, borrowed_token, market_mpolicy, admin, fill):
11+
def test_monetary_policy(filled_controller, collateral_token, borrowed_token, monetary_policy, admin, fill):
1212
available = borrowed_token.balanceOf(filled_controller)
1313
to_borrow = int(fill * available)
1414
c_amount = 2 * 3000 * to_borrow * 10**(18 - borrowed_token.decimals()) // 10**(18 - collateral_token.decimals())
@@ -23,7 +23,7 @@ def test_monetary_policy(filled_controller, collateral_token, borrowed_token, ma
2323
return
2424
else:
2525
filled_controller.create_loan(c_amount, to_borrow, 5)
26-
rate = market_mpolicy.rate(filled_controller)
26+
rate = monetary_policy.rate(filled_controller)
2727
assert rate >= min_default_borrow_rate * (1 - 1e-5)
2828
assert rate <= max_default_borrow_rate * (1 + 1e-5)
2929
theoretical_rate = min_default_borrow_rate * (max_default_borrow_rate / min_default_borrow_rate)**fill

tests/lending/test_shifted_trades.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def trade_to_price(self, p):
2121
super().trade_to_price(int(p * (1 + self.price_shift)))
2222

2323

24-
def test_adiabatic_shifted(market_amm, filled_controller, market_mpolicy, collateral_token, borrowed_token, price_oracle, accounts, admin):
24+
def test_adiabatic_shifted(market_amm, filled_controller, monetary_policy, collateral_token, borrowed_token, price_oracle, accounts, admin):
2525
ShiftedTrader.TestCase.settings = settings(max_examples=50, stateful_step_count=50)
2626
for k, v in locals().items():
2727
setattr(ShiftedTrader, k, v)

tests/lending/test_st_interest_conservation.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,9 @@ def change_rate(self, min_rate, max_rate):
204204
with boa.env.prank(self.admin):
205205
if (min_rate > max_rate or min_rate < MIN_RATE or max_rate < MIN_RATE or min_rate > MAX_RATE or max_rate > MAX_RATE):
206206
with boa.reverts():
207-
self.market_mpolicy.set_rates(min_rate, max_rate)
207+
self.monetary_policy.set_rates(min_rate, max_rate)
208208
else:
209-
self.market_mpolicy.set_rates(min_rate, max_rate)
209+
self.monetary_policy.set_rates(min_rate, max_rate)
210210

211211
@invariant()
212212
def sum_of_debts(self):
@@ -225,14 +225,14 @@ def debt_payable(self):
225225
assert debt + 10 >= supply - b # Can have error of 1 (rounding) at most per step (and 10 stateful steps)
226226

227227

228-
def test_stateful_lendborrow(vault, market_amm, market_controller, market_mpolicy, collateral_token, borrowed_token, accounts, admin):
228+
def test_stateful_lendborrow(vault, market_amm, market_controller, monetary_policy, collateral_token, borrowed_token, accounts, admin):
229229
StatefulLendBorrow.TestCase.settings = settings(max_examples=200, stateful_step_count=10)
230230
for k, v in locals().items():
231231
setattr(StatefulLendBorrow, k, v)
232232
run_state_machine_as_test(StatefulLendBorrow)
233233

234234

235-
def test_borrow_not_reverting(vault, market_amm, market_controller, market_mpolicy, collateral_token, borrowed_token, accounts, admin):
235+
def test_borrow_not_reverting(vault, market_amm, market_controller, monetary_policy, collateral_token, borrowed_token, accounts, admin):
236236
for k, v in locals().items():
237237
setattr(StatefulLendBorrow, k, v)
238238
state = StatefulLendBorrow()

tests/lending/test_vault.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
DEAD_SHARES = 1000
1010

1111

12-
def test_vault_creation(vault, market_controller, market_amm, market_mpolicy, factory, price_oracle,
12+
def test_vault_creation(vault, market_controller, market_amm, monetary_policy, factory, price_oracle,
1313
borrowed_token, collateral_token, stablecoin):
1414
assert vault.amm() == market_amm.address
1515
assert vault.controller() == market_controller.address
16-
assert market_controller.monetary_policy() == market_mpolicy.address
16+
assert market_controller.monetary_policy() == monetary_policy.address
1717
n = factory.market_count()
1818
assert n > 0
1919
assert factory.vaults(n - 1) == vault.address
@@ -22,7 +22,7 @@ def test_vault_creation(vault, market_controller, market_amm, market_mpolicy, fa
2222
assert factory.borrowed_tokens(n - 1) == borrowed_token.address
2323
assert factory.collateral_tokens(n - 1) == collateral_token.address
2424
assert factory.price_oracles(n - 1) == price_oracle.address
25-
assert factory.monetary_policies(n - 1) == market_mpolicy.address
25+
assert factory.monetary_policies(n - 1) == monetary_policy.address
2626

2727
assert factory.vaults(factory.vaults_index(vault.address)) == vault.address
2828

0 commit comments

Comments
 (0)