forked from yearn/brownie-strategy-mix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_operation.py
135 lines (109 loc) · 4.47 KB
/
test_operation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import brownie
from brownie import Contract
import pytest
def test_operation(
chain, accounts, token, vault, strategy, user, strategist, amount, RELATIVE_APPROX
):
# Deposit to the vault
user_balance_before = token.balanceOf(user)
token.approve(vault.address, amount, {"from": user})
vault.deposit(amount, {"from": user})
assert token.balanceOf(vault.address) == amount
# harvest
chain.sleep(1)
strategy.harvest()
assert pytest.approx(strategy.estimatedTotalAssets(), rel=RELATIVE_APPROX) == amount
# tend()
strategy.tend()
# withdrawal
vault.withdraw({"from": user})
assert (
pytest.approx(token.balanceOf(user), rel=RELATIVE_APPROX) == user_balance_before
)
def test_emergency_exit(
chain, accounts, token, vault, strategy, user, strategist, amount, RELATIVE_APPROX
):
# Deposit to the vault
token.approve(vault.address, amount, {"from": user})
vault.deposit(amount, {"from": user})
chain.sleep(1)
strategy.harvest()
assert pytest.approx(strategy.estimatedTotalAssets(), rel=RELATIVE_APPROX) == amount
# set emergency and exit
strategy.setEmergencyExit()
chain.sleep(1)
strategy.harvest()
assert strategy.estimatedTotalAssets() < amount
def test_profitable_harvest(
chain, accounts, token, vault, strategy, user, strategist, amount, RELATIVE_APPROX
):
# Deposit to the vault
token.approve(vault.address, amount, {"from": user})
vault.deposit(amount, {"from": user})
assert token.balanceOf(vault.address) == amount
# Harvest 1: Send funds through the strategy
chain.sleep(1)
strategy.harvest()
assert pytest.approx(strategy.estimatedTotalAssets(), rel=RELATIVE_APPROX) == amount
# TODO: Add some code before harvest #2 to simulate earning yield
# Harvest 2: Realize profit
chain.sleep(1)
strategy.harvest()
chain.sleep(3600 * 6) # 6 hrs needed for profits to unlock
chain.mine(1)
profit = token.balanceOf(vault.address) # Profits go to vault
# TODO: Uncomment the lines below
# assert token.balanceOf(strategy) + profit > amount
# assert vault.pricePerShare() > before_pps
def test_change_debt(
chain, gov, token, vault, strategy, user, strategist, amount, RELATIVE_APPROX
):
# Deposit to the vault and harvest
token.approve(vault.address, amount, {"from": user})
vault.deposit(amount, {"from": user})
vault.updateStrategyDebtRatio(strategy.address, 5_000, {"from": gov})
chain.sleep(1)
strategy.harvest()
half = int(amount / 2)
assert pytest.approx(strategy.estimatedTotalAssets(), rel=RELATIVE_APPROX) == half
vault.updateStrategyDebtRatio(strategy.address, 10_000, {"from": gov})
chain.sleep(1)
strategy.harvest()
assert pytest.approx(strategy.estimatedTotalAssets(), rel=RELATIVE_APPROX) == amount
# In order to pass this tests, you will need to implement prepareReturn.
# TODO: uncomment the following lines.
# vault.updateStrategyDebtRatio(strategy.address, 5_000, {"from": gov})
# chain.sleep(1)
# strategy.harvest()
# assert pytest.approx(strategy.estimatedTotalAssets(), rel=RELATIVE_APPROX) == half
def test_sweep(gov, vault, strategy, token, user, amount, weth, weth_amount):
# Strategy want token doesn't work
token.transfer(strategy, amount, {"from": user})
assert token.address == strategy.want()
assert token.balanceOf(strategy) > 0
with brownie.reverts("!want"):
strategy.sweep(token, {"from": gov})
# Vault share token doesn't work
with brownie.reverts("!shares"):
strategy.sweep(vault.address, {"from": gov})
# TODO: If you add protected tokens to the strategy.
# Protected token doesn't work
# with brownie.reverts("!protected"):
# strategy.sweep(strategy.protectedToken(), {"from": gov})
before_balance = weth.balanceOf(gov)
weth.transfer(strategy, weth_amount, {"from": user})
assert weth.address != strategy.want()
assert weth.balanceOf(user) == 0
strategy.sweep(weth, {"from": gov})
assert weth.balanceOf(gov) == weth_amount + before_balance
def test_triggers(
chain, gov, vault, strategy, token, amount, user, weth, weth_amount, strategist
):
# Deposit to the vault and harvest
token.approve(vault.address, amount, {"from": user})
vault.deposit(amount, {"from": user})
vault.updateStrategyDebtRatio(strategy.address, 5_000, {"from": gov})
chain.sleep(1)
strategy.harvest()
strategy.harvestTrigger(0)
strategy.tendTrigger(0)