-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLock-up.sol
More file actions
287 lines (286 loc) · 11.1 KB
/
Lock-up.sol
File metadata and controls
287 lines (286 loc) · 11.1 KB
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
// SPDX-License-Identifier: MIT
pragma solidity >=0.6.0 <0.8.0;
contract EternalStorage {
mapping(bytes32 => uint256) internal uintStorage;
mapping(bytes32 => string) internal stringStorage;
mapping(bytes32 => address) internal addressStorage;
mapping(bytes32 => bytes) internal bytesStorage;
mapping(bytes32 => bool) internal boolStorage;
mapping(bytes32 => int256) internal intStorage;
}
pragma solidity >=0.6.0 <0.8.0;
abstract contract ReentrancyGuard {
uint256 private constant _NOT_ENTERED = 1;
uint256 private constant _ENTERED = 2;
uint256 private _status;
constructor () internal {
_status = _NOT_ENTERED;
}
modifier nonReentrant() {
require(_status != _ENTERED, "ReentrancyGuard: reentrant call");
_status = _ENTERED;
_;
_status = _NOT_ENTERED;
}
}
library SafeMath {
function tryAdd(uint256 a, uint256 b) internal pure returns (bool, uint256) {
uint256 c = a + b;
if (c < a) return (false, 0);
return (true, c);
}
function trySub(uint256 a, uint256 b) internal pure returns (bool, uint256) {
if (b > a) return (false, 0);
return (true, a - b);
}
function tryMul(uint256 a, uint256 b) internal pure returns (bool, uint256) {
if (a == 0) return (true, 0);
uint256 c = a * b;
if (c / a != b) return (false, 0);
return (true, c);
}
function tryDiv(uint256 a, uint256 b) internal pure returns (bool, uint256) {
if (b == 0) return (false, 0);
return (true, a / b);
}
function tryMod(uint256 a, uint256 b) internal pure returns (bool, uint256) {
if (b == 0) return (false, 0);
return (true, a % b);
}
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
require(c >= a, "SafeMath: addition overflow");
return c;
}
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
require(b <= a, "SafeMath: subtraction overflow");
return a - b;
}
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
if (a == 0) return 0;
uint256 c = a * b;
require(c / a == b, "SafeMath: multiplication overflow");
return c;
}
function div(uint256 a, uint256 b) internal pure returns (uint256) {
require(b > 0, "SafeMath: division by zero");
return a / b;
}
function mod(uint256 a, uint256 b) internal pure returns (uint256) {
require(b > 0, "SafeMath: modulo by zero");
return a % b;
}
function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b <= a, errorMessage);
return a - b;
}
function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b > 0, errorMessage);
return a / b;
}
function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b > 0, errorMessage);
return a % b;
}
}
abstract contract Context {
function _msgSender() internal view virtual returns (address payable) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes memory) {
this;
return msg.data;
}
}
abstract contract Ownable is Context {
address private _owner;
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
constructor () internal {
address msgSender = _msgSender();
_owner = msgSender;
emit OwnershipTransferred(address(0), msgSender);
}
function owner() public view virtual returns (address) {
return _owner;
}
modifier onlyOwner() {
require(owner() == _msgSender(), "Ownable: caller is not the owner");
_;
}
function renounceOwnership() public virtual onlyOwner {
emit OwnershipTransferred(_owner, address(0));
_owner = address(0);
}
function transferOwnership(address newOwner) public virtual onlyOwner {
require(newOwner != address(0), "Ownable: new owner is the zero address");
emit OwnershipTransferred(_owner, newOwner);
_owner = newOwner;
}
}
library TransferHelper {
function safeApprove(
address token,
address to,
uint256 value
) internal {
(bool success, bytes memory data) = token.call(abi.encodeWithSelector(0x095ea7b3, to, value));
require(
success && (data.length == 0 || abi.decode(data, (bool))),
'TransferHelper::safeApprove: approve failed'
);
}
function safeTransfer(
address token,
address to,
uint256 value
) internal {
(bool success, bytes memory data) = token.call(abi.encodeWithSelector(0xa9059cbb, to, value));
require(
success && (data.length == 0 || abi.decode(data, (bool))),
'TransferHelper::safeTransfer: transfer failed'
);
}
function safeTransferFrom(
address token,
address from,
address to,
uint256 value
) internal {
(bool success, bytes memory data) = token.call(abi.encodeWithSelector(0x23b872dd, from, to, value));
require(
success && (data.length == 0 || abi.decode(data, (bool))),
'TransferHelper::transferFrom: transferFrom failed'
);
}
function safeTransferBaseToken(address token, address payable to, uint value, bool isERC20) internal {
if (!isERC20) {
to.transfer(value);
} else {
(bool success, bytes memory data) = token.call(abi.encodeWithSelector(0xa9059cbb, to, value));
require(success && (data.length == 0 || abi.decode(data, (bool))), 'TransferHelper: TRANSFER_FAILED');
}
}
}
interface IERCBurn {
function burn(uint256 _amount) external;
function approve(address spender, uint256 amount) external returns (bool);
function allowance(address owner, address spender) external returns (uint256);
function balanceOf(address account) external view returns (uint256);
}
interface IUniFactory {
function getPair(address tokenA, address tokenB) external view returns (address);
}
interface IMigrator {
function migrate(address lpToken, uint256 amount, uint256 unlockDate, address owner) external returns (bool);
}
contract TokenLocker is Ownable, ReentrancyGuard,EternalStorage {
using SafeMath for uint256;
struct TokenLock {
uint256 lockDate;
uint256 amount;
uint256 initialAmount;
uint256 unlockDate;
address owner;
}
mapping(address => address[]) public lockedTokens;
mapping(address => address[]) public lockedUser;
mapping(address => mapping (address => TokenLock)) public tokenLocks;
struct FeeStruct {
uint256 ethFee;
uint256 liquidityFee;
}
FeeStruct public gFees;
address payable devaddr;
address payable lpaddr;
IMigrator migrator;
event onDeposit(address lpToken, address user, uint256 amount, uint256 lockDate, uint256 unlockDate);
event onWithdraw(address lpToken, uint256 amount);
constructor(address payable _lpaddr) public {
devaddr = msg.sender;
lpaddr = _lpaddr;
gFees.ethFee = 100 ether;
gFees.liquidityFee = 10; // 1%
}
function setDev(address payable _devaddr) public onlyOwner {
devaddr = _devaddr;
}
function setMigrator(IMigrator _migrator) public onlyOwner {
migrator = _migrator;
}
function setFees(uint256 _ethFee, uint256 _liquidityFee) public onlyOwner {
gFees.ethFee = _ethFee;
gFees.liquidityFee = _liquidityFee;
}
function lockLPToken (address _lpToken, uint256 _amount, uint256 _unlock_date, address payable _withdrawer) external payable nonReentrant {
require(_unlock_date < 10000000000, 'TIMESTAMP INVALID');
require(_amount > 0, 'INSUFFICIENT');
TransferHelper.safeTransferFrom(_lpToken, address(msg.sender), address(this), _amount);
uint256 ethFee = gFees.ethFee;
require(msg.value == ethFee, 'FEE NOT MET');
uint256 devFee = ethFee;
devaddr.transfer(devFee);
uint256 liquidityFee = _amount.mul(gFees.liquidityFee).div(1000);
TransferHelper.safeTransfer(_lpToken, lpaddr, liquidityFee);
uint256 amountLocked = _amount.sub(liquidityFee);
if(!boolStorage[keccak256(abi.encodePacked(_lpToken,_withdrawer))]){
TokenLock memory token_lock;
token_lock.lockDate = block.timestamp;
token_lock.amount = amountLocked;
token_lock.initialAmount = amountLocked;
token_lock.unlockDate = _unlock_date;
token_lock.owner = _withdrawer;
tokenLocks[_lpToken][_withdrawer] = token_lock;
boolStorage[keccak256(abi.encodePacked(_lpToken,_withdrawer))] = true;
if(!boolStorage[keccak256(abi.encodePacked(_withdrawer,_lpToken))]){
lockedTokens[_lpToken].push(_withdrawer);
lockedUser[_withdrawer].push(_lpToken);
boolStorage[keccak256(abi.encodePacked(_withdrawer,_lpToken))] = true;
}
emit onDeposit(_lpToken, msg.sender, token_lock.amount, token_lock.lockDate, token_lock.unlockDate);
}else{
require(msg.sender == _withdrawer, '_withdrawer no sender');
TokenLock storage tokenLock = tokenLocks[_lpToken][_withdrawer];
tokenLock.amount= tokenLock.amount.add(amountLocked);
tokenLock.initialAmount= tokenLock.initialAmount.add(amountLocked);
tokenLock.lockDate = block.timestamp;
if(_unlock_date > tokenLock.unlockDate){
tokenLock.unlockDate = _unlock_date;
}
emit onDeposit(_lpToken, msg.sender, tokenLock.amount, tokenLock.lockDate, tokenLock.unlockDate);
}
}
function relock (address _lpToken, uint256 _unlock_date) external nonReentrant {
require(_unlock_date < 10000000000, 'TIMESTAMP INVALID');
TokenLock storage userLock = tokenLocks[_lpToken][msg.sender];
require(userLock.owner == msg.sender, 'LOCK MISMATCH');
require(userLock.unlockDate < _unlock_date, 'UNLOCK BEFORE');
userLock.unlockDate = _unlock_date;
}
function withdraw (address _lpToken, uint256 _amount) external nonReentrant {
require(_amount > 0, 'ZERO WITHDRAWL');
TokenLock storage userLock = tokenLocks[_lpToken][msg.sender];
require(userLock.owner == msg.sender, 'LOCK MISMATCH');
require(userLock.unlockDate < block.timestamp, 'NOT YET');
userLock.amount = userLock.amount.sub(_amount);
if (userLock.amount == 0) {
boolStorage[keccak256(abi.encodePacked(_lpToken,msg.sender))] = false;
}
TransferHelper.safeTransfer(_lpToken, msg.sender, _amount);
emit onWithdraw(_lpToken, _amount);
}
function getLockForToken (address _lpToken) external view
returns (address[] memory) {
address[] memory addr_list = lockedTokens[_lpToken];
return addr_list;
}
function getLockForUser (address _user) external view
returns (address[] memory) {
address[] memory addr_list = lockedUser[_user];
return addr_list;
}
function getUserLockForToken (address _user, address _lpToken) external view
returns (uint256, uint256, uint256, uint256, address) {
TokenLock storage tokenLock = tokenLocks[_lpToken][_user];
return (tokenLock.lockDate, tokenLock.amount, tokenLock.initialAmount, tokenLock.unlockDate, tokenLock.owner);
}
}