This repository has been archived by the owner on Mar 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBBLeverage.sol
163 lines (144 loc) · 6.61 KB
/
BBLeverage.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
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.22;
// External
import {RebaseLibrary, Rebase} from "@boringcrypto/boring-solidity/contracts/libraries/BoringRebase.sol";
import {BoringERC20} from "@boringcrypto/boring-solidity/contracts/libraries/BoringERC20.sol";
import {IERC20} from "@boringcrypto/boring-solidity/contracts/ERC20.sol";
// Tapioca
import {SafeApprove} from "../../libraries/SafeApprove.sol";
import {BBLendingCommon} from "./BBLendingCommon.sol";
/*
████████╗ █████╗ ██████╗ ██╗ ██████╗ ██████╗ █████╗
╚══██╔══╝██╔══██╗██╔══██╗██║██╔═══██╗██╔════╝██╔══██╗
██║ ███████║██████╔╝██║██║ ██║██║ ███████║
██║ ██╔══██║██╔═══╝ ██║██║ ██║██║ ██╔══██║
██║ ██║ ██║██║ ██║╚██████╔╝╚██████╗██║ ██║
╚═╝ ╚═╝ ╚═╝╚═╝ ╚═╝ ╚═════╝ ╚═════╝╚═╝ ╚═╝
*/
contract BBLeverage is BBLendingCommon {
using RebaseLibrary for Rebase;
using SafeApprove for address;
using BoringERC20 for IERC20;
// ************** //
// *** ERRORS *** //
// ************** //
error LeverageExecutorNotValid();
error CollateralShareNotValid();
struct _BuyCollateralCalldata {
address from;
uint256 borrowAmount;
uint256 supplyAmount;
bytes data;
}
struct _BuyCollateralMemoryData {
uint256 supplyShareToAmount;
uint256 borrowShareToAmount;
}
/// @notice Lever up: Borrow more and buy collateral with it.
/// @param from The user who buys
/// @param borrowAmount Amount of extra asset borrowed
/// @param supplyAmount Amount of asset supplied (down payment)
/// @param data LeverageExecutor data
/// @return amountOut Actual collateral amount purchased
function buyCollateral(address from, uint256 borrowAmount, uint256 supplyAmount, bytes calldata data)
external
optionNotPaused(PauseType.LeverageBuy)
solvent(from, false)
notSelf(from)
returns (uint256 amountOut)
{
if (address(leverageExecutor) == address(0)) {
revert LeverageExecutorNotValid();
}
// Stack too deep fix
_BuyCollateralCalldata memory calldata_;
_BuyCollateralMemoryData memory memoryData;
{
calldata_.from = from;
calldata_.borrowAmount = borrowAmount;
calldata_.supplyAmount = supplyAmount;
calldata_.data = data;
}
{
uint256 supplyShare = yieldBox.toShare(assetId, calldata_.supplyAmount, true);
if (supplyShare > 0) {
(memoryData.supplyShareToAmount,) =
yieldBox.withdraw(assetId, calldata_.from, address(leverageExecutor), 0, supplyShare);
}
}
{
(, uint256 borrowShare) = _borrow(
calldata_.from,
address(this),
calldata_.borrowAmount,
_computeVariableOpeningFee(calldata_.borrowAmount)
);
(memoryData.borrowShareToAmount,) =
yieldBox.withdraw(assetId, address(this), address(leverageExecutor), 0, borrowShare);
}
{
amountOut = leverageExecutor.getCollateral(
collateralId,
address(asset),
address(collateral),
memoryData.supplyShareToAmount + memoryData.borrowShareToAmount,
calldata_.from,
calldata_.data
);
}
uint256 collateralShare = yieldBox.toShare(collateralId, amountOut, false);
address(asset).safeApprove(address(yieldBox), type(uint256).max);
yieldBox.depositAsset(collateralId, address(this), address(this), 0, collateralShare); // TODO Check for rounding attack?
address(asset).safeApprove(address(yieldBox), 0);
if (collateralShare == 0) revert CollateralShareNotValid();
_allowedBorrow(calldata_.from, collateralShare);
_addCollateral(calldata_.from, calldata_.from, false, 0, collateralShare);
}
struct _SellCollateralMemoryData {
uint256 obtainedShare;
uint256 leverageAmount;
uint256 shareOut;
uint256 partOwed;
uint256 amountOwed;
uint256 shareOwed;
}
/// @notice Lever down: Sell collateral to repay debt; excess goes to YB
/// @param from The user who sells
/// @param share Collateral YieldBox-shares to sell
/// @param data LeverageExecutor data
/// @return amountOut Actual asset amount received in the sale
function sellCollateral(address from, uint256 share, bytes calldata data)
external
optionNotPaused(PauseType.LeverageSell)
solvent(from, false)
notSelf(from)
returns (uint256 amountOut)
{
if (address(leverageExecutor) == address(0)) {
revert LeverageExecutorNotValid();
}
_allowedBorrow(from, share);
_removeCollateral(from, address(this), share);
_SellCollateralMemoryData memory memoryData;
(, memoryData.obtainedShare) =
yieldBox.withdraw(collateralId, address(this), address(leverageExecutor), 0, share);
memoryData.leverageAmount = yieldBox.toAmount(collateralId, memoryData.obtainedShare, false);
amountOut = leverageExecutor.getAsset(
assetId, address(collateral), address(asset), memoryData.leverageAmount, from, data
);
memoryData.shareOut = yieldBox.toShare(assetId, amountOut, false);
address(asset).safeApprove(address(yieldBox), type(uint256).max);
yieldBox.depositAsset(collateralId, address(this), address(this), 0, memoryData.shareOut); // TODO Check for rounding attack?
address(asset).safeApprove(address(yieldBox), 0);
memoryData.partOwed = userBorrowPart[from];
memoryData.amountOwed = totalBorrow.toElastic(memoryData.partOwed, true);
memoryData.shareOwed = yieldBox.toShare(assetId, memoryData.amountOwed, true);
if (memoryData.shareOwed <= memoryData.shareOut) {
_repay(from, from, memoryData.partOwed);
} else {
//repay as much as we can
uint256 partOut = totalBorrow.toBase(amountOut, false);
_repay(from, from, partOut);
}
}
}