-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLPStaking.sol
More file actions
174 lines (152 loc) · 5.66 KB
/
LPStaking.sol
File metadata and controls
174 lines (152 loc) · 5.66 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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;
interface IBEP20 {
function balanceOf(address account) external view returns (uint256);
function transfer(
address recipient,
uint256 amount
) external returns (bool);
function approve(address spender, uint256 amount) external returns (bool);
function transferFrom(
address sender,
address recipient,
uint256 amount
) external returns (bool);
function totalSupply() external view returns (uint256);
function decimals() external view returns (uint8);
function symbol() external view returns (string memory);
function mint(address account, uint amount) external;
}
contract LPStaking {
struct UserInfo {
uint256 amount;
uint256 rewardDebt;
}
struct PoolInfo {
IBEP20 lpToken;
uint256 allocPoint;
uint256 lastRewardBlock;
uint256 accRewardPerShare;
}
IBEP20 public rewardToken;
uint256 public rewardPerBlock;
uint256 public totalAllocPoint;
uint256 public startBlock;
uint256 public endBlock;
PoolInfo[] public poolInfo;
mapping(uint256 => mapping(address => UserInfo)) public userInfo;
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
);
constructor(
IBEP20 _rewardToken,
uint256 _rewardPerBlock,
uint256 _startBlock,
uint256 _endBlock
) {
rewardToken = _rewardToken;
rewardPerBlock = _rewardPerBlock;
startBlock = _startBlock;
endBlock = _endBlock;
}
function poolLength() external view returns (uint256) {
return poolInfo.length;
}
function addPool(uint256 _allocPoint, IBEP20 _lpToken) external {
for (uint256 pid = 0; pid < poolInfo.length; ++pid) {
require(poolInfo[pid].lpToken != _lpToken, "Pool already added");
}
totalAllocPoint += _allocPoint;
poolInfo.push(
PoolInfo({
lpToken: _lpToken,
allocPoint: _allocPoint,
lastRewardBlock: block.number > startBlock
? block.number
: startBlock,
accRewardPerShare: 0
})
);
}
function setPool(uint256 _pid, uint256 _allocPoint) external {
totalAllocPoint =
totalAllocPoint -
poolInfo[_pid].allocPoint +
_allocPoint;
poolInfo[_pid].allocPoint = _allocPoint;
}
function getPendingReward(
address _user,
uint256 _pid
) public view returns (uint256) {
PoolInfo storage pool = poolInfo[_pid];
UserInfo storage user = userInfo[_pid][_user];
uint256 accRewardPerShare = pool.accRewardPerShare;
uint256 lpSupply = pool.lpToken.balanceOf(address(this));
if (block.number > pool.lastRewardBlock && lpSupply != 0) {
uint256 multiplier = block.number - pool.lastRewardBlock;
uint256 reward = (multiplier * rewardPerBlock * pool.allocPoint) /
totalAllocPoint;
accRewardPerShare += (reward * 1e12) / lpSupply;
}
return (user.amount * accRewardPerShare) / 1e12 - user.rewardDebt;
}
function updatePool(uint256 _pid) internal {
PoolInfo storage pool = poolInfo[_pid];
if (block.number <= pool.lastRewardBlock) {
return;
}
uint256 lpSupply = pool.lpToken.balanceOf(address(this));
if (lpSupply == 0) {
pool.lastRewardBlock = block.number;
return;
}
uint256 multiplier = block.number - pool.lastRewardBlock;
uint256 reward = (multiplier * rewardPerBlock * pool.allocPoint) /
totalAllocPoint;
rewardToken.mint(address(this), reward);
pool.accRewardPerShare += (reward * 1e12) / lpSupply;
pool.lastRewardBlock = block.number;
}
function deposit(uint256 _pid, uint256 _amount) external {
PoolInfo storage pool = poolInfo[_pid];
UserInfo storage user = userInfo[_pid][msg.sender];
updatePool(_pid);
if (user.amount > 0) {
uint256 pending = (user.amount * pool.accRewardPerShare) /
1e12 -
user.rewardDebt;
rewardToken.transfer(msg.sender, pending);
}
pool.lpToken.transferFrom(msg.sender, address(this), _amount);
user.amount += _amount;
user.rewardDebt = (user.amount * pool.accRewardPerShare) / 1e12;
emit Deposit(msg.sender, _pid, _amount);
}
function withdraw(uint256 _pid, uint256 _amount) external {
PoolInfo storage pool = poolInfo[_pid];
UserInfo storage user = userInfo[_pid][msg.sender];
require(user.amount >= _amount, "Withdraw: insufficient balance");
updatePool(_pid);
uint256 pending = (user.amount * pool.accRewardPerShare) /
1e12 -
user.rewardDebt;
rewardToken.transfer(msg.sender, pending);
user.amount -= _amount;
user.rewardDebt = (user.amount * pool.accRewardPerShare) / 1e12;
pool.lpToken.transfer(msg.sender, _amount);
emit Withdraw(msg.sender, _pid, _amount);
}
function emergencyWithdraw(uint256 _pid) external {
PoolInfo storage pool = poolInfo[_pid];
UserInfo storage user = userInfo[_pid][msg.sender];
pool.lpToken.transfer(msg.sender, user.amount);
emit EmergencyWithdraw(msg.sender, _pid, user.amount);
user.amount = 0;
user.rewardDebt = 0;
}
}