-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpynVault.sol
344 lines (285 loc) · 9.31 KB
/
OpynVault.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "./SafeMath.sol";
// debug
import "hardhat/console.sol";
contract OpynVault is Ownable {
using SafeMath for uint256;
using SafeERC20 for IERC20;
// uint256 public constant TOTAL_TIME_IN_A_YEAR = 365*24*60*60;
uint256 public constant TOTAL_TIME_IN_A_YEAR = 31536000;
IERC20 public immutable token;
uint256 public yearlyYield;
uint256 public rewardPool;
struct Deposit {
// use same uint width
// for stacking
uint256 amount;
uint256 timestamp;
}
mapping(address => Deposit[]) public depositsByUser;
mapping(address => uint256) public totalDepositsByUser;
// helpers
modifier whenRewardsAvailable() {
// double check just in case
require(rewardPool > 0 && rewardPool != 0 , "No rewards available.");
_;
}
function isContract(address _addr) public view returns (bool) {
uint256 size;
assembly {
size := extcodesize(_addr)
}
return size > 0;
}
// main
constructor(address _token, uint256 _yearlyYield) Ownable(msg.sender) {
require(_token != address(0), "Token must be non-0 address");
require(isContract(_token), "Token must be contract");
token = IERC20(_token);
// yearly yield in proms
// meaning 5% we would send as 5 * 1000 = 5000
// or 0.5% we would send as 0.5 * 1000 = 500
// this is due to more precise calc later on
require(_yearlyYield > 0, "Yield must be greater than 0");
yearlyYield = _yearlyYield;
rewardPool = 0;
}
// getters
function getReward() external view returns (uint256) {
return rewardPool;
}
function getToken() external view returns (address) {
return address(token);
}
// state based getters
function getDeposit(address _user) external view returns (uint256) {
return totalDepositsByUser[_user];
}
// owner only
function depositRewards(uint256 _amount) external onlyOwner returns (bool, uint256) {
require(_amount > 0, "Need to deposit non-zero amount");
// think about user == owner ???
address owner = msg.sender;
require(
token.balanceOf(owner) >= _amount,
"Owner must have sufficient tokens"
);
uint256 balanceBefore = token.balanceOf(address(this));
token.safeTransferFrom(owner, address(this), _amount);
uint256 balanceAfter = token.balanceOf(address(this));
assert(balanceAfter - balanceBefore == _amount);
rewardPool += _amount;
return (true, _amount);
}
function setRewardPercentage(uint256 _yearlyYield) external onlyOwner returns (bool, uint256) {
// yearly yield in proms
// meaning 5% we would send as 5 * 1000 = 5000
// or 0.5% we would send as 0.5 * 1000 = 500
// this is due to more precise calc later on
require(_yearlyYield > 0, "Yield must be greater than 0");
yearlyYield = _yearlyYield;
return (true, yearlyYield);
}
// user funcs
function deposit(uint256 _amount) external whenRewardsAvailable returns (bool, uint256) {
require(_amount > 0, "Need to deposit non-zero amount");
// think about user == owner ???
address user = msg.sender;
require(
token.balanceOf(user) >= _amount,
"User must have sufficient tokens"
);
uint256 balanceBefore = token.balanceOf(address(this));
token.safeTransferFrom(user, address(this), _amount);
uint256 balanceAfter = token.balanceOf(address(this));
assert(balanceAfter > balanceBefore);
Deposit memory newDeposit = Deposit({
amount: _amount,
timestamp: block.timestamp
});
depositsByUser[user].push(newDeposit);
uint256 totalBefore = totalDepositsByUser[user];
totalDepositsByUser[user] += _amount;
uint256 totalAfter = totalDepositsByUser[user];
assert(totalAfter > totalBefore);
return (true, _amount);
}
function calculateReward(address _user) public view returns (uint256) {
require(_user != address(0), "Must be non-zero user address");
Deposit[] memory userDeposits = depositsByUser[_user];
uint256 reward = 0;
for (uint i = 0; i < userDeposits.length; i++) {
console.log(
"[calculateReward]",
" #1",
" userDeposits[i].timestamp = ",
userDeposits[i].timestamp
);
console.log(
"[calculateReward]",
" #1",
" block.timestamp = ",
block.timestamp
);
console.log(
"[calculateReward]",
" #1",
" userDeposits[i].amount = ",
userDeposits[i].amount
);
console.log(
"[calculateReward]",
" #1",
" yearlyYield = ",
yearlyYield
);
console.log(" ");
uint256 yield = calculateYield(
userDeposits[i].amount,
yearlyYield,
userDeposits[i].timestamp,
block.timestamp
);
reward += yield;
}
return reward;
}
function calculateYield(
uint256 _amount,
uint256 _yearlyYield,
uint256 _start_time,
uint256 _end_time
) public pure returns (uint256) {
uint256 deltaT = _end_time.sub(_start_time);
console.log(
"[yield]",
" #1",
" deltaT = ",
deltaT
);
console.log(" ");
uint256 yearlyAmount = _amount.mul(_yearlyYield);
uint256 yieldAmount = (deltaT.mul(yearlyAmount))
.div(TOTAL_TIME_IN_A_YEAR)
.div(1000) // due to proms format
.div(100); // due to being perc
console.log(
"[yield]",
" #2",
" yearlyAmount = ",
yearlyAmount
);
console.log(
"[yield]",
" #2",
" yieldAmount = ",
yieldAmount
);
console.log(" ");
return yieldAmount;
}
function withdraw() whenRewardsAvailable public returns (bool, uint256) {
address user = msg.sender;
uint256 totalUserDepositAmount = totalDepositsByUser[user];
console.log(
"[withdraw]",
" #1",
" totalUserDepositAmount = ",
totalUserDepositAmount
);
// save on gas
if(totalUserDepositAmount == 0) {
return (false, 0);
}
console.log(
"[withdraw]",
" #1",
" totalUserDepositAmount = ",
totalUserDepositAmount
);
console.log(
"[withdraw]",
" #1",
" rewardPool = ",
rewardPool
);
console.log(" ");
uint256 totalReward = calculateReward(user);
uint256 totalWithdrawal = totalUserDepositAmount + totalReward;
console.log(
"[withdraw]",
" #2",
" totalWithdrawal = ",
totalWithdrawal
);
console.log(
"[withdraw]",
" #2",
" totalReward = ",
totalReward
);
console.log(
"[withdraw]",
" #2",
" totalUserDepositAmount = ",
totalUserDepositAmount
);
console.log(
"[withdraw]",
" #2",
"balance(user) before =",
token.balanceOf(user)
);
console.log(" ");
// calculation sanity check
// cant lose money
assert(totalWithdrawal >= totalUserDepositAmount);
// transferable state changes
uint256 totalBefore = token.balanceOf(address(this));
token.safeTransfer(user, totalWithdrawal);
uint256 totalAfter = token.balanceOf(address(this));
console.log(
"[withdraw]",
" #3",
" totalBefore = ",
totalBefore
);
console.log(
"[withdraw]",
" #3",
" totalAfter = ",
totalAfter
);
console.log(
"[withdraw]",
" #3",
"balance(user) after =",
token.balanceOf(user)
);
console.log(" ");
// assert(totalAfter - totalBefore == totalWithdrawal);
// state changes
console.log(
"[withdraw]",
" #4",
" rewardPool = ",
rewardPool
);
console.log(
"[withdraw]",
" #4",
" totalWithdrawal = ",
totalWithdrawal
);
console.log(" ");
rewardPool -= totalReward;
assert(rewardPool >= 0);
delete depositsByUser[user];
delete totalDepositsByUser[user];
return (true, totalWithdrawal);
}
}