This repository has been archived by the owner on Nov 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
MockIchiFarm.sol
337 lines (297 loc) · 11.7 KB
/
MockIchiFarm.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
// SPDX-License-Identifier: MIT
pragma solidity 0.8.16;
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract MockIchiFarm is Ownable {
using SafeERC20 for IERC20;
/// @notice Info of each IFV2 user.
/// `amount` LP token amount the user has provided.
/// `rewardDebt` The amount of ICHI entitled to the user.
struct UserInfo {
uint256 amount;
int256 rewardDebt;
}
/// @notice Info of each IFV2 pool.
/// `allocPoint` The amount of allocation points assigned to the pool.
/// Also known as the amount of ICHI to distribute per block.
struct PoolInfo {
uint128 accIchiPerShare;
uint64 lastRewardBlock;
uint64 allocPoint;
}
/// @dev Address of ICHI contract.
IERC20 private immutable ICHI;
/// @notice Info of each IFV2 pool.
PoolInfo[] public poolInfo;
/// @notice Address of the LP token for each IFV2 pool.
IERC20[] public lpToken;
/// @dev List of all added LP tokens.
mapping(address => bool) private addedLPs;
/// @notice Info of each user that stakes LP tokens.
mapping(uint256 => mapping(address => UserInfo)) public userInfo;
/// @notice Total allocation points. Must be the sum of all allocation points in all pools.
uint256 public totalAllocPoint;
/// @notice ICHI tokens created per block.
uint256 public ichiPerBlock;
/// @dev Extra decimals for pool's accIchiPerShare attribute. Needed in order to accomodate different types of LPs.
uint256 private constant ACC_ICHI_PRECISION = 1e18;
/// @dev nonReentrant flag used to secure functions with external calls.
bool private nonReentrant;
event Deposit(
address indexed user,
uint256 indexed pid,
uint256 amount,
address indexed to
);
event Withdraw(
address indexed user,
uint256 indexed pid,
uint256 amount,
address indexed to
);
event EmergencyWithdraw(
address indexed user,
uint256 indexed pid,
uint256 amount,
address indexed to
);
event Harvest(address indexed user, uint256 indexed pid, uint256 amount);
event LogPoolAddition(
uint256 indexed pid,
uint256 allocPoint,
IERC20 indexed lpToken
);
event LogSetPool(uint256 indexed pid, uint256 allocPoint);
event LogUpdatePool(
uint256 indexed pid,
uint64 lastRewardBlock,
uint256 lpSupply,
uint256 accIchiPerShare
);
event SetIchiPerBlock(uint256 ichiPerBlock, bool withUpdate);
/// @param _ichi The ICHI token contract address.
/// @param _ichiPerBlock ICHI tokens created per block.
constructor(IERC20 _ichi, uint256 _ichiPerBlock) {
ICHI = _ichi;
ichiPerBlock = _ichiPerBlock;
totalAllocPoint = 0;
}
/// @notice Update number of ICHI tokens created per block. Can only be called by the owner.
/// @param _ichiPerBlock ICHI tokens created per block.
/// @param _withUpdate true if massUpdatePools should be triggered as well.
function setIchiPerBlock(uint256 _ichiPerBlock, bool _withUpdate)
external
onlyOwner
{
if (_withUpdate) {
massUpdateAllPools();
}
ichiPerBlock = _ichiPerBlock;
emit SetIchiPerBlock(_ichiPerBlock, _withUpdate);
}
/// @notice Set the nonReentrant flag. Could be used to pause/resume the farm operations. Can only be called by the owner.
/// @param _val nonReentrant flag value to be set.
function setNonReentrant(bool _val) external onlyOwner returns (bool) {
nonReentrant = _val;
return nonReentrant;
}
/// @notice Returns the number of IFV2 pools.
function poolLength() external view returns (uint256 pools) {
pools = poolInfo.length;
}
/// @notice Returns the ICHI reward value for a specific pool.
function poolIchiReward(uint256 _pid) external view returns (uint256) {
if (totalAllocPoint == 0) return 0;
return (ichiPerBlock * (poolInfo[_pid].allocPoint)) / totalAllocPoint;
}
/// @notice Returns the total number of LPs staked in the farm.
function getLPSupply(uint256 _pid) external view returns (uint256) {
uint256 lpSupply = lpToken[_pid].balanceOf(address(this));
return lpSupply;
}
/// @notice Add a new LP to the pool. Can only be called by the owner.
/// DO NOT add the same LP token more than once. Rewards will be messed up if you do.
/// @param allocPoint AP of the new pool.
/// @param _lpToken Address of the LP ERC-20 token.
function add(uint256 allocPoint, IERC20 _lpToken) external onlyOwner {
require(
!addedLPs[address(_lpToken)],
"ichiFarmV2::there is already a pool with this LP"
);
uint256 lastRewardBlock = block.number;
totalAllocPoint += allocPoint;
lpToken.push(_lpToken);
addedLPs[address(_lpToken)] = true;
poolInfo.push(
PoolInfo({
allocPoint: uint64(allocPoint),
lastRewardBlock: uint64(lastRewardBlock),
accIchiPerShare: 0
})
);
emit LogPoolAddition(lpToken.length - 1, allocPoint, _lpToken);
}
/// @notice Update the given pool's ICHI allocation point. Can only be called by the owner.
/// @param _pid The index of the pool. See `poolInfo`.
/// @param _allocPoint New AP of the pool.
function set(uint256 _pid, uint256 _allocPoint) external onlyOwner {
totalAllocPoint =
totalAllocPoint -
poolInfo[_pid].allocPoint +
_allocPoint;
poolInfo[_pid].allocPoint = uint64(_allocPoint);
emit LogSetPool(_pid, _allocPoint);
}
/// @notice View function to see pending ICHI on frontend.
/// @param _pid The index of the pool. See `poolInfo`.
/// @param _user Address of user.
/// @return pending ICHI reward for a given user.
function pendingIchi(uint256 _pid, address _user)
external
view
returns (uint256 pending)
{
PoolInfo memory pool = poolInfo[_pid];
UserInfo storage user = userInfo[_pid][_user];
uint256 accIchiPerShare = pool.accIchiPerShare;
uint256 lpSupply = lpToken[_pid].balanceOf(address(this));
if (
block.number > pool.lastRewardBlock &&
lpSupply > 0 &&
totalAllocPoint > 0
) {
uint256 blocks = block.number - pool.lastRewardBlock;
accIchiPerShare +=
((blocks *
ichiPerBlock *
pool.allocPoint *
ACC_ICHI_PRECISION) / totalAllocPoint) /
lpSupply;
}
pending =
(user.amount * accIchiPerShare) /
ACC_ICHI_PRECISION -
uint256(user.rewardDebt);
}
/// @notice Update reward variables for all pools. Be careful of gas spending!
function massUpdateAllPools() public {
uint256 len = poolInfo.length;
for (uint256 pid = 0; pid < len; ++pid) {
updatePool(pid);
}
}
/// @notice Update reward variables for specified pools. Be careful of gas spending!
/// @param pids Pool IDs of all to be updated. Make sure to update all active pools.
function massUpdatePools(uint256[] calldata pids) external {
uint256 len = pids.length;
for (uint256 i = 0; i < len; ++i) {
updatePool(pids[i]);
}
}
/// @notice Update reward variables of the given pool.
/// @param pid The index of the pool. See `poolInfo`.
/// @return pool Returns the pool that was updated.
function updatePool(uint256 pid) public returns (PoolInfo memory pool) {
pool = poolInfo[pid];
if (block.number > pool.lastRewardBlock) {
uint256 lpSupply = lpToken[pid].balanceOf(address(this));
if (lpSupply > 0 && totalAllocPoint > 0) {
uint256 blocks = block.number - pool.lastRewardBlock;
pool.accIchiPerShare += uint128(
((blocks *
ichiPerBlock *
pool.allocPoint *
ACC_ICHI_PRECISION) / totalAllocPoint) / lpSupply
);
}
pool.lastRewardBlock = uint64(block.number);
poolInfo[pid] = pool;
emit LogUpdatePool(
pid,
pool.lastRewardBlock,
lpSupply,
pool.accIchiPerShare
);
}
}
/// @notice Deposit LP tokens to IFV2 for ICHI allocation.
/// @param pid The index of the pool. See `poolInfo`.
/// @param amount LP token amount to deposit.
/// @param to The receiver of `amount` deposit benefit.
function deposit(
uint256 pid,
uint256 amount,
address to
) external {
require(!nonReentrant, "ichiFarmV2::nonReentrant - try again");
nonReentrant = true;
PoolInfo memory pool = updatePool(pid);
UserInfo storage user = userInfo[pid][to];
// Effects
user.amount += amount;
user.rewardDebt += int256(
(amount * pool.accIchiPerShare) / ACC_ICHI_PRECISION
);
// Interactions
lpToken[pid].safeTransferFrom(msg.sender, address(this), amount);
emit Deposit(msg.sender, pid, amount, to);
nonReentrant = false;
}
/// @notice Withdraw LP tokens from IFV2.
/// @param pid The index of the pool. See `poolInfo`.
/// @param amount LP token amount to withdraw.
/// @param to Receiver of the LP tokens.
function withdraw(
uint256 pid,
uint256 amount,
address to
) external {
require(!nonReentrant, "ichiFarmV2::nonReentrant - try again");
nonReentrant = true;
PoolInfo memory pool = updatePool(pid);
UserInfo storage user = userInfo[pid][msg.sender];
// Effects
user.rewardDebt -= int256(
(amount * pool.accIchiPerShare) / ACC_ICHI_PRECISION
);
user.amount -= amount;
// Interactions
lpToken[pid].safeTransfer(to, amount);
emit Withdraw(msg.sender, pid, amount, to);
nonReentrant = false;
}
/// @notice Harvest proceeds for transaction sender to `to`.
/// @param pid The index of the pool. See `poolInfo`.
/// @param to Receiver of ICHI rewards.
function harvest(uint256 pid, address to) external {
require(!nonReentrant, "ichiFarmV2::nonReentrant - try again");
nonReentrant = true;
PoolInfo memory pool = updatePool(pid);
UserInfo storage user = userInfo[pid][msg.sender];
int256 accumulatedIchi = int256(
(user.amount * pool.accIchiPerShare) / ACC_ICHI_PRECISION
);
uint256 _pendingIchi = uint256(accumulatedIchi - user.rewardDebt);
// Effects
user.rewardDebt = accumulatedIchi;
// Interactions
if (_pendingIchi > 0) {
ICHI.safeTransfer(to, _pendingIchi);
}
emit Harvest(msg.sender, pid, _pendingIchi);
nonReentrant = false;
}
/// @notice Withdraw without caring about rewards. EMERGENCY ONLY.
/// @param pid The index of the pool. See `poolInfo`.
/// @param to Receiver of the LP tokens.
function emergencyWithdraw(uint256 pid, address to) public {
require(address(0) != to, "ichiFarmV2::can't withdraw to address zero");
UserInfo storage user = userInfo[pid][msg.sender];
uint256 amount = user.amount;
user.amount = 0;
user.rewardDebt = 0;
// Note: transfer can fail or succeed if `amount` is zero.
lpToken[pid].safeTransfer(to, amount);
emit EmergencyWithdraw(msg.sender, pid, amount, to);
}
}