Verified contractExactMIT
ERC20Stakable
Address0xa466cffba5e57f3e83cdff9205ac6525f1b021f9
- Balance
0 ETH
- Transactions
77
- Compiler
solc 0.8.11
- Same code
Only this contract
Details
- Compiler
- solc 0.8.11+commit.d7f03943
- Language
- solidity
- Optimization
- No
- EVM version
- london
- License
- MIT
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 14,040,702
- Creation transaction
- 0x984073bffe…1bb8ead542
STPL.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.11;
/**
* @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 = 12 hours; // 12 HOUR
uint256 constant UNSTAKING_LIMIT = 72 hours; // 3 DAY
uint256 constant REWARD1 = 41095.8904 ether;
uint256 constant REWARD2 = 32876.7123 ether;
uint256 constant REWARD3 = 19726.0273 ether;
uint256 constant REWARD4 = 15780.8219 ether;
uint256 constant REWARD5 = 12624.6575 ether;
uint256 constant REWARD6 = 10099.7260 ether;
uint256 constant TIMEZONE_KOR = 32400;
uint256 constant TERM1 = 1656633600 - TIMEZONE_KOR; // 2022.07.01
uint256 constant TERM2 = 1672531200 - TIMEZONE_KOR; // 2023.01.01
uint256 constant TERM3 = 1688169600 - TIMEZONE_KOR; // 2023.07.01
uint256 constant TERM4 = 1704067200 - TIMEZONE_KOR; // 2024.01.01
uint256 constant TERM5 = 1719792000 - TIMEZONE_KOR; // 2024.07.01
uint256 constant TERM6 = 1735689600 - TIMEZONE_KOR; // 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 fixReward() private returns (bool) {
if(stakers.length == 0){
return true;
}
for(uint256 i = 0; i < stakers.length; i++){
address thisAddress = stakers[i];
uint256 diff = block.timestamp.sub(stakingList[thisAddress]._fixedAt);
uint256 count = 0;
uint256 lastTimestamp = 0;
for(uint256 j=0; j<stakingList[thisAddress]._lockup.length; j++){
if(stakingList[thisAddress]._lockup[j]._releaseTime < block.timestamp){
_popLockup(j, thisAddress);
}
}
while(count < diff.div(CYCLE)){
uint256 thisTimestamp = block.timestamp.sub(diff).add(CYCLE.mul(count.add(1)));
uint256 thisFixed = _getFixed(thisAddress ,thisTimestamp);
stakingList[thisAddress]._fixed = stakingList[thisAddress]._fixed.add(thisFixed);
lastTimestamp = thisTimestamp;
count++;
}
if(lastTimestamp > 0){
stakingList[thisAddress]._fixedAt = lastTimestamp;
}
}
return true;
}
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 < 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;
}
}
}Compiler settingsShow
{
"metadata": {
"bytecodeHash": "ipfs"
},
"libraries": {},
"optimizer": {
"runs": 200,
"enabled": false
},
"evmVersion": "london",
"remappings": []
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.