forked from autofarmnetwork/autofarm-v2-contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoFarmV2.sol
More file actions
372 lines (316 loc) · 12.7 KB
/
AutoFarmV2.sol
File metadata and controls
372 lines (316 loc) · 12.7 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
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
// SPDX-License-Identifier: MIT
pragma solidity 0.6.12;
import "./helpers/ERC20.sol";
import "./libraries/Address.sol";
import "./libraries/SafeERC20.sol";
import "./libraries/EnumerableSet.sol";
import "./helpers/Ownable.sol";
import "./helpers/ReentrancyGuard.sol";
// import "./AUTOToken.sol";
abstract contract AUTOToken is ERC20 {
function mint(address _to, uint256 _amount) public virtual;
}
// For interacting with our own strategy
interface IStrategy {
// Total want tokens managed by stratfegy
function wantLockedTotal() external view returns (uint256);
// Sum of all shares of users to wantLockedTotal
function sharesTotal() external view returns (uint256);
// Main want token compounding function
function earn() external;
// Transfer want tokens autoFarm -> strategy
function deposit(address _userAddress, uint256 _wantAmt)
external
returns (uint256);
// Transfer want tokens strategy -> autoFarm
function withdraw(address _userAddress, uint256 _wantAmt)
external
returns (uint256);
function inCaseTokensGetStuck(
address _token,
uint256 _amount,
address _to
) external;
}
contract AutoFarmV2 is Ownable, ReentrancyGuard {
using SafeMath for uint256;
using SafeERC20 for IERC20;
// Info of each user.
struct UserInfo {
uint256 shares; // How many LP tokens the user has provided.
uint256 rewardDebt; // Reward debt. See explanation below.
// We do some fancy math here. Basically, any point in time, the amount of AUTO
// entitled to a user but is pending to be distributed is:
//
// amount = user.shares / sharesTotal * wantLockedTotal
// pending reward = (amount * pool.accAUTOPerShare) - user.rewardDebt
//
// Whenever a user deposits or withdraws want tokens to a pool. Here's what happens:
// 1. The pool's `accAUTOPerShare` (and `lastRewardBlock`) gets updated.
// 2. User receives the pending reward sent to his/her address.
// 3. User's `amount` gets updated.
// 4. User's `rewardDebt` gets updated.
}
struct PoolInfo {
IERC20 want; // Address of the want token.
uint256 allocPoint; // How many allocation points assigned to this pool. AUTO to distribute per block.
uint256 lastRewardBlock; // Last block number that AUTO distribution occurs.
uint256 accAUTOPerShare; // Accumulated AUTO per share, times 1e12. See below.
address strat; // Strategy address that will auto compound want tokens
}
address public AUTO = 0x4508ABB72232271e452258530D4Ed799C685eccb; // 1:1 migration to AUTOv2
address public AUTOv2 = 0xa184088a740c695E156F91f5cC086a06bb78b827;
address public burnAddress = 0x000000000000000000000000000000000000dEaD;
uint256 public ownerAUTOReward = 138; // 12%
uint256 public AUTOMaxSupply = 80000e18;
uint256 public AUTOPerBlock = 8000000000000000; // AUTO tokens created per block
uint256 public startBlock = 3888888; //https://bscscan.com/block/countdown/3888888
PoolInfo[] public poolInfo; // Info of each pool.
mapping(uint256 => mapping(address => UserInfo)) public userInfo; // Info of each user that stakes LP tokens.
uint256 public totalAllocPoint = 0; // Total allocation points. Must be the sum of all allocation points in all pools.
event Deposit(address indexed user, uint256 indexed pid, uint256 amount);
event Withdraw(address indexed user, uint256 indexed pid, uint256 amount);
event EmergencyWithdraw(
address indexed user,
uint256 indexed pid,
uint256 amount
);
function poolLength() external view returns (uint256) {
return poolInfo.length;
}
// Add a new lp to the pool. Can only be called by the owner.
// XXX DO NOT add the same LP token more than once. Rewards will be messed up if you do. (Only if want tokens are stored here.)
function add(
uint256 _allocPoint,
IERC20 _want,
bool _withUpdate,
address _strat
) public onlyOwner {
if (_withUpdate) {
massUpdatePools();
}
uint256 lastRewardBlock =
block.number > startBlock ? block.number : startBlock;
totalAllocPoint = totalAllocPoint.add(_allocPoint);
poolInfo.push(
PoolInfo({
want: _want,
allocPoint: _allocPoint,
lastRewardBlock: lastRewardBlock,
accAUTOPerShare: 0,
strat: _strat
})
);
}
// Update the given pool's AUTO allocation point. Can only be called by the owner.
function set(
uint256 _pid,
uint256 _allocPoint,
bool _withUpdate
) public onlyOwner {
if (_withUpdate) {
massUpdatePools();
}
totalAllocPoint = totalAllocPoint.sub(poolInfo[_pid].allocPoint).add(
_allocPoint
);
poolInfo[_pid].allocPoint = _allocPoint;
}
// Return reward multiplier over the given _from to _to block.
function getMultiplier(uint256 _from, uint256 _to)
public
view
returns (uint256)
{
if (IERC20(AUTOv2).totalSupply() >= AUTOMaxSupply) {
return 0;
}
return _to.sub(_from);
}
// View function to see pending AUTO on frontend.
function pendingAUTO(uint256 _pid, address _user)
external
view
returns (uint256)
{
PoolInfo storage pool = poolInfo[_pid];
UserInfo storage user = userInfo[_pid][_user];
uint256 accAUTOPerShare = pool.accAUTOPerShare;
uint256 sharesTotal = IStrategy(pool.strat).sharesTotal();
if (block.number > pool.lastRewardBlock && sharesTotal != 0) {
uint256 multiplier =
getMultiplier(pool.lastRewardBlock, block.number);
uint256 AUTOReward =
multiplier.mul(AUTOPerBlock).mul(pool.allocPoint).div(
totalAllocPoint
);
accAUTOPerShare = accAUTOPerShare.add(
AUTOReward.mul(1e12).div(sharesTotal)
);
}
return user.shares.mul(accAUTOPerShare).div(1e12).sub(user.rewardDebt);
}
// View function to see staked Want tokens on frontend.
function stakedWantTokens(uint256 _pid, address _user)
external
view
returns (uint256)
{
PoolInfo storage pool = poolInfo[_pid];
UserInfo storage user = userInfo[_pid][_user];
uint256 sharesTotal = IStrategy(pool.strat).sharesTotal();
uint256 wantLockedTotal =
IStrategy(poolInfo[_pid].strat).wantLockedTotal();
if (sharesTotal == 0) {
return 0;
}
return user.shares.mul(wantLockedTotal).div(sharesTotal);
}
// Update reward variables for all pools. Be careful of gas spending!
function massUpdatePools() public {
uint256 length = poolInfo.length;
for (uint256 pid = 0; pid < length; ++pid) {
updatePool(pid);
}
}
// Update reward variables of the given pool to be up-to-date.
function updatePool(uint256 _pid) public {
PoolInfo storage pool = poolInfo[_pid];
if (block.number <= pool.lastRewardBlock) {
return;
}
uint256 sharesTotal = IStrategy(pool.strat).sharesTotal();
if (sharesTotal == 0) {
pool.lastRewardBlock = block.number;
return;
}
uint256 multiplier = getMultiplier(pool.lastRewardBlock, block.number);
if (multiplier <= 0) {
return;
}
uint256 AUTOReward =
multiplier.mul(AUTOPerBlock).mul(pool.allocPoint).div(
totalAllocPoint
);
AUTOToken(AUTOv2).mint(
owner(),
AUTOReward.mul(ownerAUTOReward).div(1000)
);
AUTOToken(AUTOv2).mint(address(this), AUTOReward);
pool.accAUTOPerShare = pool.accAUTOPerShare.add(
AUTOReward.mul(1e12).div(sharesTotal)
);
pool.lastRewardBlock = block.number;
}
// Want tokens moved from user -> AUTOFarm (AUTO allocation) -> Strat (compounding)
function deposit(uint256 _pid, uint256 _wantAmt) public nonReentrant {
updatePool(_pid);
PoolInfo storage pool = poolInfo[_pid];
UserInfo storage user = userInfo[_pid][msg.sender];
if (user.shares > 0) {
uint256 pending =
user.shares.mul(pool.accAUTOPerShare).div(1e12).sub(
user.rewardDebt
);
if (pending > 0) {
safeAUTOTransfer(msg.sender, pending);
}
}
if (_wantAmt > 0) {
pool.want.safeTransferFrom(
address(msg.sender),
address(this),
_wantAmt
);
pool.want.safeIncreaseAllowance(pool.strat, _wantAmt);
uint256 sharesAdded =
IStrategy(poolInfo[_pid].strat).deposit(msg.sender, _wantAmt);
user.shares = user.shares.add(sharesAdded);
}
user.rewardDebt = user.shares.mul(pool.accAUTOPerShare).div(1e12);
emit Deposit(msg.sender, _pid, _wantAmt);
}
// Withdraw LP tokens from MasterChef.
function withdraw(uint256 _pid, uint256 _wantAmt) public nonReentrant {
updatePool(_pid);
PoolInfo storage pool = poolInfo[_pid];
UserInfo storage user = userInfo[_pid][msg.sender];
uint256 wantLockedTotal =
IStrategy(poolInfo[_pid].strat).wantLockedTotal();
uint256 sharesTotal = IStrategy(poolInfo[_pid].strat).sharesTotal();
require(user.shares > 0, "user.shares is 0");
require(sharesTotal > 0, "sharesTotal is 0");
// Withdraw pending AUTO
uint256 pending =
user.shares.mul(pool.accAUTOPerShare).div(1e12).sub(
user.rewardDebt
);
if (pending > 0) {
safeAUTOTransfer(msg.sender, pending);
}
// Withdraw want tokens
uint256 amount = user.shares.mul(wantLockedTotal).div(sharesTotal);
if (_wantAmt > amount) {
_wantAmt = amount;
}
if (_wantAmt > 0) {
uint256 sharesRemoved =
IStrategy(poolInfo[_pid].strat).withdraw(msg.sender, _wantAmt);
if (sharesRemoved > user.shares) {
user.shares = 0;
} else {
user.shares = user.shares.sub(sharesRemoved);
}
uint256 wantBal = IERC20(pool.want).balanceOf(address(this));
if (wantBal < _wantAmt) {
_wantAmt = wantBal;
}
pool.want.safeTransfer(address(msg.sender), _wantAmt);
}
user.rewardDebt = user.shares.mul(pool.accAUTOPerShare).div(1e12);
emit Withdraw(msg.sender, _pid, _wantAmt);
}
function withdrawAll(uint256 _pid) public nonReentrant {
withdraw(_pid, uint256(-1));
}
// Withdraw without caring about rewards. EMERGENCY ONLY.
function emergencyWithdraw(uint256 _pid) public nonReentrant {
PoolInfo storage pool = poolInfo[_pid];
UserInfo storage user = userInfo[_pid][msg.sender];
uint256 wantLockedTotal =
IStrategy(poolInfo[_pid].strat).wantLockedTotal();
uint256 sharesTotal = IStrategy(poolInfo[_pid].strat).sharesTotal();
uint256 amount = user.shares.mul(wantLockedTotal).div(sharesTotal);
IStrategy(poolInfo[_pid].strat).withdraw(msg.sender, amount);
pool.want.safeTransfer(address(msg.sender), amount);
emit EmergencyWithdraw(msg.sender, _pid, amount);
user.shares = 0;
user.rewardDebt = 0;
}
// Safe AUTO transfer function, just in case if rounding error causes pool to not have enough
function safeAUTOTransfer(address _to, uint256 _AUTOAmt) internal {
uint256 AUTOBal = IERC20(AUTOv2).balanceOf(address(this));
if (_AUTOAmt > AUTOBal) {
IERC20(AUTOv2).transfer(_to, AUTOBal);
} else {
IERC20(AUTOv2).transfer(_to, _AUTOAmt);
}
}
function inCaseTokensGetStuck(address _token, uint256 _amount)
public
onlyOwner
{
require(_token != AUTOv2, "!safe");
IERC20(_token).safeTransfer(msg.sender, _amount);
}
function migrateToAUTOv2(uint256 _inputAmt) public {
require(block.number < 5033333, "too late :("); // 20 Feb 2021 - https://bscscan.com/block/countdown/5033333
IERC20(AUTO).safeIncreaseAllowance(burnAddress, _inputAmt);
IERC20(AUTO).safeTransferFrom(
address(msg.sender),
burnAddress,
_inputAmt
);
AUTOToken(AUTOv2).mint(msg.sender, _inputAmt);
}
}