Verified contractExactMIT
ERC20Stakable
Address0x3f33fb616d20d313eaaefb71e48977101323befa
- Balance
0 ETH
- Transactions
224
- Compiler
solc 0.8.11
- Same code
Only this contract
Details
- Compiler
- solc 0.8.11+commit.d7f03943
- Language
- solidity
- Optimization
- Yes, 200 runs
- EVM version
- london
- License
- MIT
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 14,157,996
- Creation transaction
- 0x9b9689529f…6d92b01d62
stpl.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
/**
* @dev Wrappers over Solidity's arithmetic operations with added overflow
* checks.
*
* Arithmetic operations in Solidity wrap on overflow. This can easily result
* in bugs, because programmers usually assume that an overflow raises an
* error, which is the standard behavior in high level programming languages.
* `SafeMath` restores this intuition by reverting the transaction when an
* operation overflows.
*
* Using this library instead of the unchecked operations eliminates an entire
* class of bugs, so it's recommended to use it always.
*/
library SafeMath {
/**
* @dev Returns the addition of two unsigned integers, reverting on
* overflow.
*
* Counterpart to Solidity's `+` operator.
*
* Requirements:
* - Addition cannot overflow.
*/
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
require(c >= a, "SafeMath: addition overflow");
return c;
}
/**
* @dev Returns the subtraction of two unsigned integers, reverting on
* overflow (when the result is negative).
*
* Counterpart to Solidity's `-` operator.
*
* Requirements:
* - Subtraction cannot overflow.
*/
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
return sub(a, b, "SafeMath: subtraction overflow");
}
/**
* @dev Returns the subtraction of two unsigned integers, reverting with custom message on
* overflow (when the result is negative).
*
* Counterpart to Solidity's `-` operator.
*
* Requirements:
* - Subtraction cannot overflow.
*/
function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b <= a, errorMessage);
uint256 c = a - b;
return c;
}
/**
* @dev Returns the multiplication of two unsigned integers, reverting on
* overflow.
*
* Counterpart to Solidity's `*` operator.
*
* Requirements:
* - Multiplication cannot overflow.
*/
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
// Gas optimization: this is cheaper than requiring 'a' not being zero, but the
// benefit is lost if 'b' is also tested.
// See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
if (a == 0) {
return 0;
}
uint256 c = a * b;
require(c / a == b, "SafeMath: multiplication overflow");
return c;
}
/**
* @dev Returns the integer division of two unsigned integers. Reverts on
* division by zero. The result is rounded towards zero.
*
* Counterpart to Solidity's `/` operator. Note: this function uses a
* `revert` opcode (which leaves remaining gas untouched) while Solidity
* uses an invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
* - The divisor cannot be zero.
*/
function div(uint256 a, uint256 b) internal pure returns (uint256) {
return div(a, b, "SafeMath: division by zero");
}
/**
* @dev Returns the integer division of two unsigned integers. Reverts with custom message on
* division by zero. The result is rounded towards zero.
*
* Counterpart to Solidity's `/` operator. Note: this function uses a
* `revert` opcode (which leaves remaining gas untouched) while Solidity
* uses an invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
* - The divisor cannot be zero.
*/
function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b > 0, errorMessage);
uint256 c = a / b;
// assert(a == b * c + a % b); // There is no case in which this doesn't hold
return c;
}
/**
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* Reverts when dividing by zero.
*
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* opcode (which leaves remaining gas untouched) while Solidity uses an
* invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
* - The divisor cannot be zero.
*/
function mod(uint256 a, uint256 b) internal pure returns (uint256) {
return mod(a, b, "SafeMath: modulo by zero");
}
/**
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* Reverts with custom message when dividing by zero.
*
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* opcode (which leaves remaining gas untouched) while Solidity uses an
* invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
* - The divisor cannot be zero.
*/
function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b != 0, errorMessage);
return a % b;
}
}
abstract contract Ownable {
address public owner;
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
constructor() {
owner = msg.sender;
}
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
function isOwner(address account) public view returns (bool) {
if( account == owner ){
return true;
}
else {
return false;
}
}
function transferOwnership(address _owner) public onlyOwner {
require(_owner != address(0));
owner = _owner;
emit OwnershipTransferred(owner, _owner);
}
}
interface IERC20 {
function transfer(address to, uint256 value) external returns (bool);
function approve(address spender, uint256 value) external returns (bool);
function transferFrom(address from, address to, uint256 value) external returns (bool);
function totalSupply() external view returns (uint256);
function balanceOf(address who) external view returns (uint256);
function allowance(address owner, address spender) external view returns (uint256);
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
}
// ----------------------------------------------------------------------------
// Token Staking Contract
//
// (c) Alan Kim / Plumbug Studio Inc. 2022.
// ----------------------------------------------------------------------------
contract ERC20Stakable is Ownable {
using SafeMath for uint256;
IERC20 private _token;
bool private _paused;
struct LockInfo {
uint256 _locked;
uint256 _releaseTime;
}
struct StakingInfo {
uint256 _stakedAt;
uint256 _fixedAt;
uint256 _fixed;
uint256 _harvested;
uint256 _amount;
LockInfo[] _lockup;
}
mapping (address => StakingInfo) public stakingList;
uint256 public totalStaking;
address[] public stakers;
event Staking(address indexed holder, uint256 value);
event Unstaking(address indexed holder, uint256 value);
event Harvest(address indexed holder, uint256 value, uint256 releaseTime);
event Paused(address account);
event Unpaused(address account);
uint256 constant CYCLE = 168 hours; // 168 HOUR
uint256 constant UNSTAKING_LIMIT = 10 days; // 10 DAY
uint256 constant REWARD0 = 1150684.9312 ether;
uint256 constant REWARD1 = 575342.4656 ether;
uint256 constant REWARD2 = 460273.9722 ether;
uint256 constant REWARD3 = 276164.3822 ether;
uint256 constant REWARD4 = 220931.5066 ether;
uint256 constant REWARD5 = 176745.2050 ether;
uint256 constant REWARD6 = 141396.1640 ether;
uint256 constant TERM0 = 1645542000; // 2022.02.23
uint256 constant TERM1 = 1656601200; // 2022.07.01
uint256 constant TERM2 = 1672498800; // 2023.01.01
uint256 constant TERM3 = 1688137200; // 2023.07.01
uint256 constant TERM4 = 1704034800; // 2024.01.01
uint256 constant TERM5 = 1719759600; // 2024.07.01
uint256 constant TERM6 = 1735657200; // 2025.01.01
constructor(IERC20 token) {
_token = token;
_paused = false;
}
modifier whenNotPaused() {
require(!_paused);
_;
}
modifier whenPaused() {
require(_paused);
_;
}
function pause() public onlyOwner whenNotPaused {
_paused = true;
emit Paused(msg.sender);
}
function unpause() public onlyOwner whenPaused {
_paused = false;
emit Unpaused(msg.sender);
}
function balanceOf(address owner) public view returns (uint256) {
uint256 totalBalance = _token.balanceOf(owner);
return totalBalance;
}
function transfer(address to, uint256 value) public returns (bool) {
return _token.transferFrom(msg.sender, to, value);
}
function truncateBalance() public onlyOwner returns (bool) {
require(balanceOf(address(this)) > 0);
return _token.transferFrom(address(this), owner, balanceOf(address(this)));
}
function stakingAmount(uint256 value) public whenNotPaused returns (bool) {
require(_fixReward());
_staking(msg.sender, value);
return true;
}
function unstakingAmount() public whenNotPaused returns (bool) {
require(harvest());
_unstaking(msg.sender);
return true;
}
function getStandardReward() public view returns (uint256){
return _getReward(block.timestamp);
}
function isPaused() public view returns (bool){
return _paused;
}
function getLocked() public view returns (uint256){
uint256 locked = 0;
for(uint256 i=0; i<stakingList[msg.sender]._lockup.length; i++){
if(stakingList[msg.sender]._lockup[i]._releaseTime > block.timestamp){
locked = locked.add(stakingList[msg.sender]._lockup[i]._locked);
}
}
return locked;
}
function getLockInfo(uint256 index) public view returns (LockInfo memory){
return stakingList[msg.sender]._lockup[index];
}
function harvest() public whenNotPaused returns (bool) {
// require(block.timestamp - stakingList[msg.sender]._fixedAt > CYCLE);
require(_fixReward());
require(stakingList[msg.sender]._fixed > 0);
_token.transfer(msg.sender, stakingList[msg.sender]._fixed);
stakingList[msg.sender]._harvested = stakingList[msg.sender]._harvested.add(stakingList[msg.sender]._fixed);
stakingList[msg.sender]._fixed = 0;
return true;
}
function fixRewardFake() public view returns (uint256) {
address thisAddress = msg.sender;
if(stakingList[thisAddress]._amount <= 0){
return 0;
}
uint256 diff = block.timestamp.sub(stakingList[thisAddress]._fixedAt);
uint256 count = 0;
uint256 fakeReward = 0;
while(count < diff.div(CYCLE)){
uint256 thisTimestamp = block.timestamp.sub(diff).add(CYCLE.mul(count.add(1)));
uint256 thisFixed = _getFixed(thisAddress ,thisTimestamp);
fakeReward = fakeReward.add(thisFixed);
count++;
}
return stakingList[thisAddress]._fixed + fakeReward;
}
function getRepeatCount() public view returns (uint256, uint256) {
if(stakers.length == 0){
return (0, 0);
}
uint256 repeat = 0;
uint256 difficultCount = 0;
for(uint256 i = 0; i < stakers.length; i++){
address thisAddress = stakers[i];
uint256 diff = block.timestamp.sub(stakingList[thisAddress]._fixedAt);
repeat = repeat.add(diff.div(CYCLE));
if(_getReward(block.timestamp) != _getReward(stakingList[thisAddress]._fixedAt)){
difficultCount++;
}
}
return (repeat, difficultCount);
}
function _fixReward() private returns (bool) {
if(stakers.length == 0){
return true;
}
for(uint256 i = 0; i < stakers.length; i++){
address thisAddress = stakers[i];
uint256 lastTimestamp;
uint256 lastFixed = stakingList[thisAddress]._fixed;
for(uint256 j=0; j<stakingList[thisAddress]._lockup.length; j++){
if(stakingList[thisAddress]._lockup[j]._releaseTime < block.timestamp){
_popLockup(j, thisAddress);
}
}
uint256 diff = block.timestamp.sub(stakingList[thisAddress]._fixedAt);
uint256 repeat = diff.div(CYCLE);
uint256 thisTimestamp;
if(_getReward(block.timestamp) == _getReward(stakingList[thisAddress]._fixedAt)){
thisTimestamp = block.timestamp.sub(diff).add(CYCLE.mul(repeat));
lastFixed = lastFixed.add(_getFixed(thisAddress ,thisTimestamp).mul(repeat));
lastTimestamp = thisTimestamp;
}else{
uint256 firstRepeat = _getHalfs(stakingList[thisAddress]._fixedAt);
uint256 lastRepeat = repeat - firstRepeat;
uint256 firstTimestamp = block.timestamp.sub(diff).add(CYCLE.mul(1));
lastFixed = lastFixed.add(_getFixed(thisAddress ,firstTimestamp).mul(firstRepeat));
thisTimestamp = block.timestamp.sub(diff).add(CYCLE.mul(repeat));
lastFixed = lastFixed.add(_getFixed(thisAddress ,thisTimestamp).mul(lastRepeat));
lastTimestamp = thisTimestamp;
// uint256 count = 0;
// while(count < repeat){
// thisTimestamp = block.timestamp.sub(diff).add(CYCLE.mul(count.add(1)));
// lastFixed = lastFixed.add(_getFixed(thisAddress ,thisTimestamp));
// count+=1;
// }
// lastTimestamp = thisTimestamp;
}
if(lastTimestamp > 0){
stakingList[thisAddress]._fixed = lastFixed;
stakingList[thisAddress]._fixedAt = lastTimestamp;
}
}
return true;
}
function _getHalfs(uint256 fixedAt) internal view returns (uint256) {
uint256 nowTerm = _getTerm(block.timestamp);
uint256 firstRepeat = nowTerm.sub(fixedAt).div(CYCLE);
return firstRepeat;
}
function _popStakers(uint256 index) internal {
require(index < stakers.length);
stakers[index] = stakers[stakers.length-1];
stakers.pop();
}
function _popLockup(uint256 index, address holder) internal {
require(index < stakingList[holder]._lockup.length);
stakingList[holder]._lockup[index] = stakingList[holder]._lockup[stakingList[holder]._lockup.length-1];
stakingList[holder]._lockup.pop();
}
function _getFixed(address staker, uint256 timestamp) internal view returns (uint256) {
uint256 rewardAll = _getReward(timestamp);
uint256 thisFixed = rewardAll.mul(stakingList[staker]._amount).div(totalStaking);
return thisFixed;
}
function _staking(address holder, uint256 value) internal returns (bool) {
totalStaking = totalStaking.add(value);
_token.transferFrom(holder, address(this), value);
if(0 == stakingList[holder]._amount){
stakers.push(holder);
}
stakingList[holder]._amount = stakingList[holder]._amount.add(value);
stakingList[holder]._lockup.push( LockInfo(value, block.timestamp.add(UNSTAKING_LIMIT.add(CYCLE.sub(block.timestamp % CYCLE))) ) );
stakingList[holder]._stakedAt = block.timestamp;
stakingList[holder]._fixedAt = block.timestamp;
emit Staking(holder, value);
return true;
}
function _unstaking(address holder) internal returns (bool) {
require(block.timestamp - stakingList[msg.sender]._fixedAt < CYCLE);
uint256 locked = 0;
for(uint256 i=0; i<stakingList[holder]._lockup.length; i++){
locked = locked.add(stakingList[holder]._lockup[i]._locked);
}
_token.transfer(holder, stakingList[holder]._amount.sub(locked));
totalStaking = totalStaking.sub(stakingList[holder]._amount.sub(locked));
stakingList[holder]._amount = locked;
if(locked==0){
for(uint256 i = 0; i < stakers.length; i++){
if(stakers[i] == holder){
_popStakers(i);
}
}
}
emit Unstaking(holder, stakingList[holder]._amount);
return true;
}
function _getReward(uint256 timestamp) internal pure returns (uint256) {
if(timestamp < TERM0){
return REWARD0;
}else if(timestamp < TERM1){
return REWARD1;
}else if (timestamp < TERM2){
return REWARD2;
}else if (timestamp < TERM3){
return REWARD3;
}else if (timestamp < TERM4){
return REWARD4;
}else if (timestamp < TERM5){
return REWARD5;
}else if (timestamp < TERM6){
return REWARD6;
}else{
return 0;
}
}
function _getTerm(uint256 timestamp) internal pure returns (uint256) {
if(timestamp < TERM0){
return 0;
}else if(timestamp < TERM1){
return TERM0;
}else if (timestamp < TERM2){
return TERM1;
}else if (timestamp < TERM3){
return TERM2;
}else if (timestamp < TERM4){
return TERM3;
}else if (timestamp < TERM5){
return TERM4;
}else if (timestamp < TERM6){
return TERM5;
}else{
return TERM6;
}
}
}Compiler settingsShow
{
"metadata": {
"bytecodeHash": "ipfs"
},
"libraries": {},
"optimizer": {
"runs": 200,
"enabled": true
},
"evmVersion": "london",
"remappings": []
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.