-
Notifications
You must be signed in to change notification settings - Fork 0
/
FarmingChallenge.sol
149 lines (106 loc) · 4.32 KB
/
FarmingChallenge.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
//"SPDX-License-Identifier: UNLICENSED"
pragma solidity ^0.6.6;
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/SafeERC20.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/math/SafeMath.sol";
import "@chainlink/contracts/src/v0.6/ChainlinkClient.sol";
import "./RewardToken.sol";
contract FarmingTimeBaseReward is Ownable, ChainlinkClient {
using SafeMath for uint256;
using SafeERC20 for IERC20;
uint256 public lockUnit = 1 days;
//Pool information
struct PoolInfo{
IERC20 lpToken;
uint256 totalPointAccumulated;
uint256 lockPeriod;
uint256 startBlock;
}
// staker information
struct StakerInfo {
uint256 amount;
uint256 rewardDebt;
uint256 point;
uint256 dailyPoint;
}
PoolInfo[] public poolInfo; //array to store all pool
StakerInfo[] public user;
mapping(uint256 => mapping(address => StakerInfo)) stakerInfo; // mapping to store staker information
uint256 public startBlock; // block where the user actitvity start;
RewardToken rewardToken; // reward token
//events
event Deposit(address indexed user, uint256 indexed pid, uint256 amount);
event Withdraw(address indexed user, uint256 indexed pid, uint256 amount);
constructor (
RewardToken _rewardToken
) public {
rewardToken = _rewardToken;
}
// get the total existing pools
function poolLength() external view returns (uint256) {
return poolInfo.length;
}
// function to create a new pool
function createPool(IERC20 _lpToken, uint256 _period) public onlyOwner {
poolInfo.push(PoolInfo({
lpToken: _lpToken,
totalPointAccumulated: 0,
lockPeriod: _period.mul(lockUnit),
startBlock: block.timestamp
}));
}
//function to get the userPoint
function getUserPoint(uint256 _pid, address _staker) public view returns (uint256){
StakerInfo storage staker = stakerInfo[_pid][_staker];
return staker.point;
}
// mint the reward
function generateReward(uint256 _pid) public {
PoolInfo storage pool = poolInfo[_pid];
if(block.timestamp >= pool.startBlock.mul(pool.lockPeriod)){
uint256 reward = pool.totalPointAccumulated;
rewardToken.mint(address(this), reward);
} else {
return;
}
}
// claim the reward token
function claim(uint256 _pid) public {
PoolInfo storage pool = poolInfo[_pid];
StakerInfo storage staker = stakerInfo[_pid][msg.sender];
uint256 balanceLp = pool.lpToken.balanceOf(address(this));
if(block.timestamp >= pool.startBlock.mul(pool.lockPeriod)){
staker.rewardDebt = staker.point.mul(balanceLp).div(pool.totalPointAccumulated);
rewardToken.transfer(msg.sender, staker.rewardDebt);
staker.rewardDebt = 0;
}
}
// stake your lpToken
function deposit(uint256 _pid, uint256 _amount) public {
PoolInfo storage pool = poolInfo[_pid];
StakerInfo storage staker = stakerInfo[_pid][msg.sender];
if(staker.amount > 0){
staker.point = staker.amount.add(_amount);
}
if(_amount > 0){
pool.lpToken.safeTransferFrom(address(msg.sender), address(this), _amount);
staker.amount = staker.amount.add(_amount);
pool.totalPointAccumulated = pool.totalPointAccumulated.add(_amount);
}
emit Deposit(msg.sender, _pid, _amount);
}
// withdraw your lpToken
function withdraw(uint256 _pid, uint256 _amount) public {
PoolInfo storage pool = poolInfo[_pid];
StakerInfo storage staker = stakerInfo[_pid][msg.sender];
require(staker.amount >= _amount, "error withdraw");
if(_amount > 0) {
pool.lpToken.safeTransfer(msg.sender, _amount);
staker.amount = staker.amount.sub(_amount);
pool.totalPointAccumulated = pool.totalPointAccumulated.sub(_amount);
}
emit Withdraw(msg.sender, _pid, _amount);
}
}