-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVault.sol
237 lines (157 loc) · 7.62 KB
/
Vault.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
225
226
227
228
229
230
231
232
233
234
235
236
237
// SPDX-License-Identifier: MIT
pragma solidity >=0.6.12;
import "./libraries/safemath.sol";
import "./libraries/IERC20.sol";
import "./libraries/yearnVault.sol";
// Special thanks to twiiter@prism0x for this distribution algo
// https://solmaz.io/2019/02/24/scalable-reward-changing/
// minimial approch to self repaying loans, hardcodes 75% LTV or about 133% over-collaterizaiton
// eventually will make this adjustable
///======================================================================================================================================
///
/// minimial approch to self repaying loans, hardcodes 75% LTV or about 133% over-collaterizaiton eventually will make this adjustable
///
/// Todo: Rebuild on top of SAVault logic
///
///======================================================================================================================================
contract pVault {
using SafeMath for uint;
///======================================================================================================================================
/// Data Structures
///======================================================================================================================================
struct Identity {
uint256 deposits;
uint256 debt;
uint256 tracker; // sum of changes of deposit * yeildPerToken
}
struct Context {
// Basis Points
uint16 maxLTV;
uint16 fee;
}
///======================================================================================================================================
/// State Variables
///======================================================================================================================================
// ENSURE COLLATERAL AND SYNTH TOKEN REVERTS ON FAILED TRANSFER
IERC20 internal collateral;
IERC20 internal synthetic;
yVault internal yearnVault;
address immutable feeCollector;
mapping (address => Identity) identity;
//totalDebt = supply of synth
uint256 public totalDeposits;
uint256 public totalYearnDeposited;
uint256 public feesAccumlated;
// 75% = ~133% Over-collaterized
// 75% BP LTV :: 1.25% BP fee
Context ctx = Context(7500, 125);
// sum of all distribution events ( yeild / totalDeposits )
// scaled 1e10.. As long as rewards accumlated between distributions
// are greater than 1/1e10 totalDeposits , distribute() should return a nonzero value
uint256 internal yeildPerDeposit;
uint256 internal SCALAR;
constructor(
address _synthetic,
address _yearnVault,
address _collateral,
address _feeCollector
) {
yearnVault = yVault(_yearnVault);
collateral = IERC20(_collateral);
synthetic = IERC20(_synthetic);
feeCollector = _feeCollector;
}
///======================================================================================================================================
/// External Functions
///======================================================================================================================================
function deposit(uint amount) external {
identity[msg.sender].deposits = identity[msg.sender].deposits.add(amount);
totalDeposits = totalDeposits.add(amount);
identity[msg.sender].tracker =
identity[msg.sender].tracker
.add(yeildPerDeposit.mul(amount).div(SCALAR));
uint depositing = amount.mul(5000).div(10000);
totalYearnDeposited = totalYearnDeposited.add(depositing);
// Make sure Collateral tokens Revert
collateral.transferFrom(msg.sender, address(this), amount);
// deposit 50% to yVault
yearnVault.deposit(depositing);
}
function withdraw(uint amount) external {
require( withdrawable(msg.sender) >= amount, "Amount too high");
identity[msg.sender].deposits = identity[msg.sender].deposits.sub(amount);
totalDeposits = totalDeposits.sub(amount);
identity[msg.sender].tracker =
identity[msg.sender].tracker
.sub(yeildPerDeposit.mul(amount).div(SCALAR));
if ( amount > collateral.balanceOf(address(this)) ) {
withdrawNeeded(amount);
}
collateral.transfer(msg.sender, amount);
}
function incurDebt(uint amount) external {
require(
identity[msg.sender].debt.add(amount) <= identity[msg.sender].deposits.mul(ctx.maxLTV).div(10000)
);
identity[msg.sender].debt = identity[msg.sender].debt.add(amount);
uint feeAdjust = amount.mul(ctx.fee).div(10000);
feesAccumlated = feesAccumlated.add( amount.sub(feeAdjust) );
synthetic.mint(msg.sender, feeAdjust);
}
function repayDebtSynth(uint amount) external {
require ( identity[msg.sender].debt >= amount );
identity[msg.sender].debt = identity[msg.sender].debt.sub(amount);
synthetic.transferFrom(msg.sender, address(this), amount); //change to burn
}
function repayDebtAsset(uint amount) external {
require ( identity[msg.sender].debt >= amount );
identity[msg.sender].debt = identity[msg.sender].debt.sub(amount);
collateral.transferFrom(msg.sender, address(this), amount);
}
function claimFees() external {
require(msg.sender == feeCollector);
uint fees = feesAccumlated;
feesAccumlated = 0;
synthetic.mint(msg.sender, fees);
}
///======================================================================================================================================
/// Public Functions
///======================================================================================================================================
function harvestAndDistribute() public {
uint yeild = getYeild();
yearnVault.withdraw(yeild);
yeildPerDeposit = yeildPerDeposit
.add(yeild.mul(SCALAR).div(totalDeposits));
}
function burnSynth(uint amount) public {
// reverts on fail
synthetic.transferFrom(msg.sender, address(0), amount);
// system should always be overcollatirzed so this cant fail
collateral.transfer(msg.sender, amount);
}
///======================================================================================================================================
/// Internal Functions
///======================================================================================================================================
function getYeild() internal returns (uint) {
uint price = yearnVault.getPricePerFullShare();
uint totalClaimable = yearnVault.balanceOf(address(this)).mul(price);
return totalClaimable.sub(totalYearnDeposited);
}
function withdrawable(address who) internal view returns (uint) {
uint deposits = identity[who].deposits;
uint yeild = deposits
.mul(yeildPerDeposit).div(SCALAR)
.sub(identity[msg.sender].tracker);
// 75% of deposit = ~133% over-collateralized
uint overCollatDebt = identity[who].debt.mul(13300).div(10000);
return deposits.sub(overCollatDebt).add(yeild);
}
function withdrawNeeded(uint amount) internal {
uint tokenNeeded = amount.sub( collateral.balanceOf(address(this)) );
uint price = yearnVault.getPricePerFullShare();
// tokenNeeded / sharePrice = sharesNeeded
uint sharesNeeded = tokenNeeded.div(price);
totalYearnDeposited = totalYearnDeposited.sub(sharesNeeded);
yearnVault.withdraw(sharesNeeded);
}
}