Created
May 24, 2022 22:54
-
-
Save Turupawn/7c6b89d43aed88a0fc29b47ea6f0c8c2 to your computer and use it in GitHub Desktop.
Busfarm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity 0.8.14; | |
abstract contract Context { | |
function _msgSender() internal view virtual returns (address) { | |
return msg.sender; | |
} | |
function _msgData() internal view virtual returns (bytes calldata) { | |
return msg.data; | |
} | |
} | |
/** | |
* @dev Contract module which provides a basic access control mechanism, where | |
* there is an account (an owner) that can be granted exclusive access to | |
* specific functions. | |
* | |
* By default, the owner account will be the one that deploys the contract. This | |
* can later be changed with {transferOwnership}. | |
* | |
* This module is used through inheritance. It will make available the modifier | |
* `onlyOwner`, which can be applied to your functions to restrict their use to | |
* the owner. | |
*/ | |
abstract contract Ownable is Context { | |
address private _owner; | |
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner); | |
/** | |
* @dev Initializes the contract setting the deployer as the initial owner. | |
*/ | |
constructor() { | |
_transferOwnership(_msgSender()); | |
} | |
/** | |
* @dev Returns the address of the current owner. | |
*/ | |
function owner() public view virtual returns (address) { | |
return _owner; | |
} | |
/** | |
* @dev Throws if called by any account other than the owner. | |
*/ | |
modifier onlyOwner() { | |
require(owner() == _msgSender(), "Ownable: caller is not the owner"); | |
_; | |
} | |
/** | |
* @dev Leaves the contract without owner. It will not be possible to call | |
* `onlyOwner` functions anymore. Can only be called by the current owner. | |
* | |
* NOTE: Renouncing ownership will leave the contract without an owner, | |
* thereby removing any functionality that is only available to the owner. | |
*/ | |
function renounceOwnership() public virtual onlyOwner { | |
_transferOwnership(address(0)); | |
} | |
/** | |
* @dev Transfers ownership of the contract to a new account (`newOwner`). | |
* Can only be called by the current owner. | |
*/ | |
function transferOwnership(address newOwner) public virtual onlyOwner { | |
require(newOwner != address(0), "Ownable: new owner is the zero address"); | |
_transferOwnership(newOwner); | |
} | |
/** | |
* @dev Transfers ownership of the contract to a new account (`newOwner`). | |
* Internal function without access restriction. | |
*/ | |
function _transferOwnership(address newOwner) internal virtual { | |
address oldOwner = _owner; | |
_owner = newOwner; | |
emit OwnershipTransferred(oldOwner, newOwner); | |
} | |
} | |
contract BusStaking is Ownable { | |
IERC20 public busd; | |
IERC20 public busToken; | |
// Staking data | |
uint public totalDeposits; | |
uint public pendingClaim; | |
mapping(address => uint) public lastClaimTimestamp; | |
mapping(address => uint) public depositAmount; | |
// Daily history | |
uint public lastDayCalculated; | |
uint public lastDayCalculatedTimestamp; | |
mapping(uint => uint) public dayRewards; | |
// Launch state | |
uint public rewardGenesisTiemstamp; | |
bool public isClaimActive = false; | |
// Mechanics | |
uint STAKING_CLOSE_PERIOD = 2 minutes;// 1 days; | |
uint public rewardRateDailyPercentage = 1000; | |
constructor(address busd_address, address bus_token_address) { | |
busd = IERC20(busd_address); | |
busToken = IERC20(bus_token_address); | |
} | |
// Admin | |
function initalizeClaim() external onlyOwner | |
{ | |
rewardGenesisTiemstamp = lastDayCalculatedTimestamp = block.timestamp; | |
isClaimActive = true; | |
} | |
// Modifiers | |
modifier updateReward() { | |
if(isClaimActive) | |
{ | |
uint daysSinceLastDayRewardCalculation = (block.timestamp - lastDayCalculatedTimestamp)/(STAKING_CLOSE_PERIOD); | |
uint currentDayReward; | |
if(totalDeposits > 0) | |
{ | |
for(uint i=0; i<daysSinceLastDayRewardCalculation; i++) | |
{ | |
uint contractTokenSupply = busd.balanceOf(address(this)) - pendingClaim; | |
currentDayReward = ((contractTokenSupply * rewardRateDailyPercentage) / 10000)/ totalDeposits; | |
pendingClaim += currentDayReward * totalDeposits; | |
dayRewards[lastDayCalculated + i] = currentDayReward; | |
} | |
} | |
lastDayCalculated = lastDayCalculated + daysSinceLastDayRewardCalculation; | |
lastDayCalculatedTimestamp = block.timestamp; | |
} | |
_; | |
} | |
// External functions | |
function depositBUSD(uint amount) external updateReward() { | |
busd.transferFrom(msg.sender, address(this), amount); | |
} | |
function stake(uint amount) external updateReward() { | |
totalDeposits += amount; | |
lastClaimTimestamp[msg.sender] = block.timestamp; | |
depositAmount[msg.sender] += amount; | |
busToken.transferFrom(msg.sender, address(this), amount); | |
} | |
function withdrawBus() external updateReward() { | |
claim(); | |
totalDeposits -= depositAmount[msg.sender]; | |
busToken.transferFrom(address(this), msg.sender, depositAmount[msg.sender]); | |
depositAmount[msg.sender] = 0; | |
} | |
function claim() public updateReward() { | |
uint amountDaysToClaim = (block.timestamp - lastClaimTimestamp[msg.sender]) / (STAKING_CLOSE_PERIOD); | |
uint firstDayToClaim = (lastClaimTimestamp[msg.sender] - rewardGenesisTiemstamp) / (STAKING_CLOSE_PERIOD); | |
uint reward; | |
for(uint i=0; i<amountDaysToClaim;i++) | |
{ | |
reward += dayRewards[firstDayToClaim + i] * depositAmount[msg.sender]; | |
} | |
lastClaimTimestamp[msg.sender] = block.timestamp; | |
pendingClaim -= reward; | |
busd.transfer(msg.sender, reward); | |
} | |
function calculateClaim(address participant) public view returns(uint) | |
{ | |
uint amountDaysToClaim = (block.timestamp - lastClaimTimestamp[participant]) / (STAKING_CLOSE_PERIOD); | |
uint firstDayToClaim = (lastClaimTimestamp[participant] - rewardGenesisTiemstamp) / (STAKING_CLOSE_PERIOD); | |
uint reward; | |
for(uint i=0; i<amountDaysToClaim;i++) | |
{ | |
if(firstDayToClaim + i > lastDayCalculated) | |
break; | |
reward += dayRewards[firstDayToClaim + i] * depositAmount[msg.sender]; | |
} | |
uint contractTokenSupply = busd.balanceOf(address(this)); | |
uint currentDayReward; | |
if(totalDeposits > 0) | |
currentDayReward = ((contractTokenSupply * rewardRateDailyPercentage) / 10000)/ totalDeposits; | |
reward += (firstDayToClaim+amountDaysToClaim - lastDayCalculated) * currentDayReward; | |
return reward; | |
} | |
} | |
interface IERC20 { | |
function totalSupply() external view returns (uint); | |
function balanceOf(address account) external view returns (uint); | |
function transfer(address recipient, uint amount) external returns (bool); | |
function allowance(address owner, address spender) external view returns (uint); | |
function approve(address spender, uint amount) external returns (bool); | |
function transferFrom( | |
address sender, | |
address recipient, | |
uint amount | |
) external returns (bool); | |
event Transfer(address indexed from, address indexed to, uint value); | |
event Approval(address indexed owner, address indexed spender, uint value); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment