This repository has been archived by the owner on Nov 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
CurveSpell.sol
224 lines (198 loc) · 8.68 KB
/
CurveSpell.sol
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// SPDX-License-Identifier: MIT
/*
██████╗ ██╗ ██╗ ██╗███████╗██████╗ ███████╗██████╗ ██████╗ ██╗ ██╗
██╔══██╗██║ ██║ ██║██╔════╝██╔══██╗██╔════╝██╔══██╗██╔══██╗╚██╗ ██╔╝
██████╔╝██║ ██║ ██║█████╗ ██████╔╝█████╗ ██████╔╝██████╔╝ ╚████╔╝
██╔══██╗██║ ██║ ██║██╔══╝ ██╔══██╗██╔══╝ ██╔══██╗██╔══██╗ ╚██╔╝
██████╔╝███████╗╚██████╔╝███████╗██████╔╝███████╗██║ ██║██║ ██║ ██║
╚═════╝ ╚══════╝ ╚═════╝ ╚══════╝╚═════╝ ╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝
*/
pragma solidity 0.8.16;
import "@openzeppelin/contracts-upgradeable/token/ERC20/utils/SafeERC20Upgradeable.sol";
import "./BasicSpell.sol";
import "../interfaces/ICurveOracle.sol";
import "../interfaces/IWCurveGauge.sol";
import "../interfaces/curve/ICurvePool.sol";
import "../interfaces/uniswap/IUniswapV2Router02.sol";
/**
* @title CurveSpell
* @author BlueberryProtocol
* @notice CurveSpell is the factory contract that
* defines how Blueberry Protocol interacts with Curve pools
*/
contract CurveSpell is BasicSpell {
using SafeERC20Upgradeable for IERC20Upgradeable;
/// @dev address of Wrapped Curve Gauge
IWCurveGauge public wCurveGauge;
/// @dev address of CurveOracle
ICurveOracle public crvOracle;
/// @dev address of CRV token
address public CRV;
function initialize(
IBank bank_,
address werc20_,
address weth_,
address wCurveGauge_,
address crvOracle_
) external initializer {
__BasicSpell_init(bank_, werc20_, weth_);
if (wCurveGauge_ == address(0) || crvOracle_ == address(0))
revert Errors.ZERO_ADDRESS();
wCurveGauge = IWCurveGauge(wCurveGauge_);
CRV = address(wCurveGauge.CRV());
crvOracle = ICurveOracle(crvOracle_);
IWCurveGauge(wCurveGauge_).setApprovalForAll(address(bank_), true);
}
/**
* @notice Add strategy to the spell
* @param crvLp Address of crv lp token for given strategy
* @param maxPosSize, USD price of maximum position size for given strategy, based 1e18
*/
function addStrategy(address crvLp, uint256 maxPosSize) external onlyOwner {
_addStrategy(crvLp, maxPosSize);
}
/**
* @notice Add liquidity to Curve pool with 2 underlying tokens, with staking to Curve gauge
* @param minLPMint Desired LP token amount (slippage control)
*/
function openPositionFarm(
OpenPosParam calldata param,
uint256 minLPMint
)
external
existingStrategy(param.strategyId)
existingCollateral(param.strategyId, param.collToken)
{
address lp = strategies[param.strategyId].vault;
if (wCurveGauge.getLpFromGaugeId(param.farmingPoolId) != lp)
revert Errors.INCORRECT_LP(lp);
(address pool, address[] memory tokens, ) = crvOracle.getPoolInfo(lp);
// 1. Deposit isolated collaterals on Blueberry Money Market
_doLend(param.collToken, param.collAmount);
// 2. Borrow specific amounts
uint256 borrowBalance = _doBorrow(
param.borrowToken,
param.borrowAmount
);
// 3. Add liquidity on curve
_ensureApprove(param.borrowToken, pool, borrowBalance);
if (tokens.length == 2) {
uint256[2] memory suppliedAmts;
for (uint256 i = 0; i < 2; i++) {
suppliedAmts[i] = IERC20Upgradeable(tokens[i]).balanceOf(
address(this)
);
}
ICurvePool(pool).add_liquidity(suppliedAmts, minLPMint);
} else if (tokens.length == 3) {
uint256[3] memory suppliedAmts;
for (uint256 i = 0; i < 3; i++) {
suppliedAmts[i] = IERC20Upgradeable(tokens[i]).balanceOf(
address(this)
);
}
ICurvePool(pool).add_liquidity(suppliedAmts, minLPMint);
} else if (tokens.length == 4) {
uint256[4] memory suppliedAmts;
for (uint256 i = 0; i < 4; i++) {
suppliedAmts[i] = IERC20Upgradeable(tokens[i]).balanceOf(
address(this)
);
}
ICurvePool(pool).add_liquidity(suppliedAmts, minLPMint);
}
// 4. Validate MAX LTV
_validateMaxLTV(param.strategyId);
// 5. Validate Max Pos Size
_validateMaxPosSize(param.strategyId);
// 6. Take out collateral and burn
IBank.Position memory pos = bank.getCurrentPositionInfo();
if (pos.collateralSize > 0) {
(uint256 decodedGid, ) = wCurveGauge.decodeId(pos.collId);
if (param.farmingPoolId != decodedGid)
revert Errors.INCORRECT_PID(param.farmingPoolId);
if (pos.collToken != address(wCurveGauge))
revert Errors.INCORRECT_COLTOKEN(pos.collToken);
bank.takeCollateral(pos.collateralSize);
wCurveGauge.burn(pos.collId, pos.collateralSize);
_doRefundRewards(CRV);
}
// 7. Deposit on Curve Gauge, Put wrapped collateral tokens on Blueberry Bank
uint256 lpAmount = IERC20Upgradeable(lp).balanceOf(address(this));
_ensureApprove(lp, address(wCurveGauge), lpAmount);
uint256 id = wCurveGauge.mint(param.farmingPoolId, lpAmount);
bank.putCollateral(address(wCurveGauge), id, lpAmount);
}
function closePositionFarm(
ClosePosParam calldata param,
IUniswapV2Router02 swapRouter,
address[] calldata swapPath
)
external
existingStrategy(param.strategyId)
existingCollateral(param.strategyId, param.collToken)
{
address crvLp = strategies[param.strategyId].vault;
IBank.Position memory pos = bank.getCurrentPositionInfo();
if (pos.collToken != address(wCurveGauge))
revert Errors.INCORRECT_COLTOKEN(pos.collToken);
if (wCurveGauge.getUnderlyingToken(pos.collId) != crvLp)
revert Errors.INCORRECT_UNDERLYING(crvLp);
// 1. Take out collateral - Burn wrapped tokens, receive crv lp tokens and harvest CRV
bank.takeCollateral(param.amountPosRemove);
wCurveGauge.burn(pos.collId, param.amountPosRemove);
{
// 2. Swap rewards tokens to debt token
uint256 rewards = _doCutRewardsFee(CRV);
_ensureApprove(CRV, address(swapRouter), rewards);
swapRouter.swapExactTokensForTokens(
rewards,
0,
swapPath,
address(this),
type(uint256).max
);
}
{
(address pool, address[] memory tokens, ) = crvOracle.getPoolInfo(
crvLp
);
// 3. Calculate actual amount to remove
uint256 amountPosRemove = param.amountPosRemove;
if (amountPosRemove == type(uint256).max) {
amountPosRemove = IERC20Upgradeable(crvLp).balanceOf(
address(this)
);
}
// 4. Remove liquidity
int128 tokenIndex;
for (uint256 i = 0; i < tokens.length; i++) {
if (tokens[i] == pos.debtToken) {
tokenIndex = int128(uint128(i));
break;
}
}
ICurvePool(pool).remove_liquidity_one_coin(
amountPosRemove,
int128(tokenIndex),
0
);
}
// 5. Withdraw isolated collateral from Bank
_doWithdraw(param.collToken, param.amountShareWithdraw);
// 6. Repay
{
// Compute repay amount if MAX_INT is supplied (max debt)
uint256 amountRepay = param.amountRepay;
if (amountRepay == type(uint256).max) {
amountRepay = bank.currentPositionDebt(bank.POSITION_ID());
}
_doRepay(param.borrowToken, amountRepay);
}
_validateMaxLTV(param.strategyId);
// 7. Refund
_doRefund(param.borrowToken);
_doRefund(param.collToken);
_doRefund(CRV);
}
}