File 1 of 1: PabloStakeETH.sol
pragma solidity 0.7.4;
library SafeMath {
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) {
return sub(a, b, "SafeMath: subtraction overflow");
}
function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b <= a, errorMessage);
uint256 c = a - b;
return c;
}
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) {
return div(a, b, "SafeMath: division by zero");
}
function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b > 0, errorMessage);
uint256 c = a / b;
return c;
}
function mod(uint256 a, uint256 b) internal pure returns (uint256) {
return mod(a, b, "SafeMath: modulo by zero");
}
function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b != 0, errorMessage);
return a % b;
}
function ceil(uint a, uint m) internal pure returns (uint r) {
return (a + m - 1) / m * m;
}
}
contract Owned {
address payable public owner;
event OwnershipTransferred(address indexed _from, address indexed _to);
constructor() {
owner = msg.sender;
}
modifier onlyOwner {
require(msg.sender == owner);
_;
}
function transferOwnership(address payable _newOwner) public onlyOwner {
require(_newOwner != address(0), "ERC20: sending to the zero address");
owner = _newOwner;
emit OwnershipTransferred(msg.sender, _newOwner);
}
}
interface IERC20 {
function totalSupply() external view returns (uint256);
function balanceOf(address tokenOwner) external view returns (uint256 balance);
function allowance(address tokenOwner, address spender) external view returns (uint256 remaining);
function transfer(address to, uint256 tokens) external returns (bool success);
function approve(address spender, uint256 tokens) external returns (bool success);
function transferFrom(address from, address to, uint256 tokens) external returns (bool success);
function burnTokens(uint256 _amount) external;
event Transfer(address indexed from, address indexed to, uint256 tokens);
event Approval(address indexed tokenOwner, address indexed spender, uint256 tokens);
}
contract PabloStakeETH is Owned {
using SafeMath for uint256;
address public Pablo = 0x926FF6584b5905CC793cfB19bfc0AD6443671F47;
uint256 public totalStakes = 0;
uint256 unstakingFee = 0;
uint256 public totalDividends = 0;
uint256 private scaledRemainder = 0;
uint256 private scaling = uint256(10) ** 12;
uint public round = 1;
bool public breaker = false;
mapping(address => uint256) public farmTime;
address[] public farmers;
uint256 public profitGenerated = 0;
struct USER{
uint256 stakedTokens;
uint256 lastDividends;
uint256 fromTotalDividend;
uint round;
uint256 remainder;
bool initialized;
}
address[] internal stakeholders;
mapping(address => USER) stakers;
mapping (uint => uint256) public payouts;
event STAKED(address staker, uint256 tokens, uint256 time);
event EARNED(address staker, uint256 tokens);
event UNSTAKED(address staker, uint256 tokens, uint256 time);
event PAYOUT(uint256 round, uint256 tokens, address sender);
event CLAIMEDREWARD(address staker, uint256 reward);
event WithdrawalLockDurationSet(uint256 value, address sender);
function setPABLO(address _token) external onlyOwner {
require(_token != address(0), "ERC20: Setting Zero Address");
Pablo = _token;
}
function unlockTime(address _staker) public view returns(uint256){
return farmTime[_staker]+90 days;
}
function setBreaker(bool _breaker) external onlyOwner {
breaker = _breaker;
}
function getFarmersLength() public view returns (uint256) {
return farmers.length;
}
function isStakeholder(address _address)
public
view
returns(bool)
{
if(stakers[_address].initialized) return true;
else return false;
}
function addStakeholder(address _stakeholder)
internal
{
(bool _isStakeholder) = isStakeholder(_stakeholder);
if(!_isStakeholder) {
stakers[_stakeholder].initialized = true;
farmers.push(_stakeholder);
}
farmTime[_stakeholder] = block.timestamp;
}
function STAKE(uint256 tokens) external {
require(IERC20(Pablo).transferFrom(msg.sender, address(this), tokens), "Tokens cannot be transferred from user account");
uint256 owing = pendingReward(msg.sender);
stakers[msg.sender].remainder += owing;
stakers[msg.sender].stakedTokens = tokens.add(stakers[msg.sender].stakedTokens);
stakers[msg.sender].lastDividends = owing;
stakers[msg.sender].fromTotalDividend= totalDividends;
stakers[msg.sender].round = round;
totalStakes = totalStakes.add(tokens);
addStakeholder(msg.sender);
emit STAKED(msg.sender, tokens, block.timestamp);
}
function ADDFUNDS() external payable{
uint256 tokens = msg.value;
require(tokens > 0, "Must send some ethers");
profitGenerated = profitGenerated.add(tokens);
_addPayout(tokens);
}
function _addPayout(uint256 tokens) private{
uint256 available = (tokens.mul(scaling)).add(scaledRemainder);
uint256 dividendPerToken = available.div(totalStakes);
scaledRemainder = available.mod(totalStakes);
totalDividends = totalDividends.add(dividendPerToken);
payouts[round] = payouts[round - 1].add(dividendPerToken);
emit PAYOUT(round, tokens, msg.sender);
round++;
}
function CLAIMREWARD() public {
if(totalDividends > stakers[msg.sender].fromTotalDividend){
uint256 owing = pendingReward(msg.sender);
owing = owing.add(stakers[msg.sender].remainder);
stakers[msg.sender].remainder = 0;
stakers[msg.sender].lastDividends = owing;
stakers[msg.sender].round = round;
stakers[msg.sender].fromTotalDividend = totalDividends;
( bool transferOne, ) = payable(msg.sender).call{value: owing}("");
require(transferOne, "ERROR: error in sending reward from contract");
emit CLAIMEDREWARD(msg.sender, owing);
}
}
function pendingReward(address staker) private returns (uint256) {
require(staker != address(0), "ERC20: sending to the zero address");
uint stakersRound = stakers[staker].round;
uint256 amount = ((totalDividends.sub(payouts[stakersRound - 1])).mul(stakers[staker].stakedTokens)).div(scaling);
stakers[staker].remainder += ((totalDividends.sub(payouts[stakersRound - 1])).mul(stakers[staker].stakedTokens)) % scaling ;
return amount;
}
function getPendingReward(address staker) public view returns(uint256 _pendingReward) {
require(staker != address(0), "ERC20: sending to the zero address");
uint stakersRound = stakers[staker].round;
uint256 amount = ((totalDividends.sub(payouts[stakersRound - 1])).mul(stakers[staker].stakedTokens)).div(scaling);
amount += ((totalDividends.sub(payouts[stakersRound - 1])).mul(stakers[staker].stakedTokens)) % scaling ;
return (amount.add(stakers[staker].remainder));
}
function WITHDRAW(uint256 tokens) external {
require(breaker == false, "Admin Restricted WITHDRAW");
require(stakers[msg.sender].stakedTokens >= tokens && tokens > 0, "Invalid token amount to withdraw");
if(unlockTime(msg.sender) >= block.timestamp){
unstakingFee = 800;
}
uint256 _unstakingFee = (onePercent(tokens).mul(unstakingFee)).div(10);
uint256 owing = pendingReward(msg.sender);
stakers[msg.sender].remainder += owing;
stakers[msg.sender].stakedTokens = stakers[msg.sender].stakedTokens.sub(tokens);
stakers[msg.sender].lastDividends = owing;
stakers[msg.sender].fromTotalDividend= totalDividends;
stakers[msg.sender].round = round;
totalStakes = totalStakes.sub(tokens);
if(_unstakingFee > 0){
require(IERC20(Pablo).transfer(owner, (_unstakingFee)), "Error in sending tokens");
}
require(IERC20(Pablo).transfer(msg.sender, tokens.sub(_unstakingFee)), "Error in un-staking tokens");
if(stakers[msg.sender].stakedTokens == 0){
farmTime[msg.sender] = 0;
}
emit UNSTAKED(msg.sender, tokens.sub(_unstakingFee), block.timestamp);
}
function onePercent(uint256 _tokens) private pure returns (uint256){
uint256 roundValue = _tokens.ceil(100);
uint onePercentofTokens = roundValue.mul(100).div(100 * 10**uint(2));
return onePercentofTokens;
}
function StakedPabloTokens(address staker) public view returns(uint256 stakedPablo){
require(staker != address(0), "ERC20: sending to the zero address");
return stakers[staker].stakedTokens;
}
function saveEmergencyTokens(address tokenAddress) external onlyOwner {
IERC20 token = IERC20(tokenAddress);
uint256 tokenBalance = token.balanceOf(address(this));
token.transfer(owner, tokenBalance);
}
function saveETH() external onlyOwner {
payable(owner).transfer(address(this).balance);
}
receive() external payable {}
}