//SPDX-License-Identifier: MIT
pragma solidity 0.8.20;
/**
* @title Owner
* @dev Set & change owner
*/
contract Ownable {
address private owner;
// event for EVM logging
event OwnerSet(address indexed oldOwner, address indexed newOwner);
// modifier to check if caller is owner
modifier onlyOwner() {
// If the first argument of 'require' evaluates to 'false', execution terminates and all
// changes to the state and to Ether balances are reverted.
// This used to consume all gas in old EVM versions, but not anymore.
// It is often a good idea to use 'require' to check if functions are called correctly.
// As a second argument, you can also provide an explanation about what went wrong.
require(msg.sender == owner, "Caller is not owner");
_;
}
/**
* @dev Set contract deployer as owner
*/
constructor() {
owner = msg.sender; // 'msg.sender' is sender of current call, contract deployer for a constructor
emit OwnerSet(address(0), owner);
}
/**
* @dev Change owner
* @param newOwner address of new owner
*/
function changeOwner(address newOwner) public onlyOwner {
emit OwnerSet(owner, newOwner);
owner = newOwner;
}
/**
* @dev Return owner address
* @return address of owner
*/
function getOwner() external view returns (address) {
return owner;
}
}
interface IERC20 {
function totalSupply() external view returns (uint256);
function symbol() external view returns(string memory);
function name() external view returns(string memory);
/**
* @dev Returns the amount of tokens owned by `account`.
*/
function balanceOf(address account) external view returns (uint256);
/**
* @dev Returns the number of decimal places
*/
function decimals() external view returns (uint8);
/**
* @dev Moves `amount` tokens from the caller's account to `recipient`.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transfer(address recipient, uint256 amount) external returns (bool);
/**
* @dev Returns the remaining number of tokens that `spender` will be
* allowed to spend on behalf of `owner` through {transferFrom}. This is
* zero by default.
*
* This value changes when {approve} or {transferFrom} are called.
*/
function allowance(address owner, address spender) external view returns (uint256);
/**
* @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* IMPORTANT: Beware that changing an allowance with this method brings the risk
* that someone may use both the old and the new allowance by unfortunate
* transaction ordering. One possible solution to mitigate this race
* condition is to first reduce the spender's allowance to 0 and set the
* desired value afterwards:
* https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
*
* Emits an {Approval} event.
*/
function approve(address spender, uint256 amount) external returns (bool);
/**
* @dev Moves `amount` tokens from `sender` to `recipient` using the
* allowance mechanism. `amount` is then deducted from the caller's
* allowance.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
/**
* @dev Emitted when `value` tokens are moved from one account (`from`) to
* another (`to`).
*
* Note that `value` may be zero.
*/
event Transfer(address indexed from, address indexed to, uint256 value);
/**
* @dev Emitted when the allowance of a `spender` for an `owner` is set by
* a call to {approve}. `value` is the new allowance.
*/
event Approval(address indexed owner, address indexed spender, uint256 value);
}
contract StakingContract is Ownable {
// name and symbol for tokenized contract
string private _name = "Staked FIRE";
string private _symbol = "SFIRE";
uint8 private constant _decimals = 18;
// lock time in seconds
uint256 public lockTime = 30 days;
// fee for leaving staking early
uint256 public leaveEarlyFee = 25;
// recipient of fee
address public feeRecipient = 0x42290Ba8C7770f7eFa8d8224675Ca3b658a9834C;
// Staking Token
address public constant token = 0xe0C18Fe05895314F20E5100729f08baB3f3C3A45;
// Reward Token
address public constant reward = 0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599;
// User Info
struct UserInfo {
uint256 amount;
uint256 unlockTime;
uint256 totalExcluded;
}
// Address => UserInfo
mapping ( address => UserInfo ) public userInfo;
// Tracks Dividends
uint256 public totalRewards;
uint256 private totalShares;
uint256 private dividendsPerShare;
uint256 private constant precision = 10**18;
// Events
event SetLockTime(uint LockTime);
event SetEarlyFee(uint earlyFee);
event SetFeeRecipient(address FeeRecipient);
event Transfer(address indexed from, address indexed to, uint256 value);
constructor(){
emit Transfer(address(0), msg.sender, 0);
}
/** Returns the total number of tokens in existence */
function totalSupply() external view returns (uint256) {
return totalShares;
}
/** Returns the number of tokens owned by `account` */
function balanceOf(address account) public view returns (uint256) {
return userInfo[account].amount;
}
/** Token Name */
function name() public view returns (string memory) {
return _name;
}
/** Token Ticker Symbol */
function symbol() public view returns (string memory) {
return _symbol;
}
/** Tokens decimals */
function decimals() public pure returns (uint8) {
return _decimals;
}
function setLockTime(uint256 newLockTime) external onlyOwner {
require(
newLockTime <= 10**7,
'Lock Time Too Long'
);
lockTime = newLockTime;
emit SetLockTime(newLockTime);
}
function setLeaveEarlyFee(uint256 newEarlyFee) external onlyOwner {
require(
newEarlyFee < 100,
'Fee Too High'
);
leaveEarlyFee = newEarlyFee;
emit SetEarlyFee(newEarlyFee);
}
function setFeeRecipient(address newFeeRecipient) external onlyOwner {
require(
newFeeRecipient != address(0),
'Zero Address'
);
feeRecipient = newFeeRecipient;
emit SetFeeRecipient(newFeeRecipient);
}
function withdrawForeignToken(address token_) external onlyOwner {
require(
token != token_,
'Cannot Withdraw Staked Token'
);
require(
IERC20(token_).transfer(
msg.sender,
IERC20(token_).balanceOf(address(this))
),
'Failure On Token Withdraw'
);
}
function claimRewards() external {
_claimReward(msg.sender);
}
function withdraw(uint256 amount) external {
require(
amount <= userInfo[msg.sender].amount,
'Insufficient Amount'
);
require(
amount > 0,
'Zero Amount'
);
if (userInfo[msg.sender].amount > 0) {
_claimReward(msg.sender);
}
totalShares -= amount;
userInfo[msg.sender].amount -= amount;
userInfo[msg.sender].totalExcluded = getCumulativeDividends(userInfo[msg.sender].amount);
uint fee = timeUntilUnlock(msg.sender) == 0 ? 0 : ( amount * leaveEarlyFee ) / 100;
if (fee > 0) {
require(
IERC20(token).transfer(feeRecipient, fee),
'Failure On Token Transfer'
);
}
uint sendAmount = amount - fee;
require(
IERC20(token).transfer(msg.sender, sendAmount),
'Failure On Token Transfer To Sender'
);
emit Transfer(msg.sender, address(0), amount);
}
function stake(uint256 amount) external {
if (userInfo[msg.sender].amount > 0) {
_claimReward(msg.sender);
}
// transfer in tokens
uint received = _transferIn(token, amount);
// update data
unchecked {
totalShares += received;
userInfo[msg.sender].amount += received;
userInfo[msg.sender].unlockTime = block.timestamp + lockTime;
}
userInfo[msg.sender].totalExcluded = getCumulativeDividends(userInfo[msg.sender].amount);
emit Transfer(address(0), msg.sender, amount);
}
function depositRewards(uint256 amount) external {
if (totalShares == 0) {
return;
}
// transfer in reward token
uint received = _transferIn(reward, amount);
// update state
unchecked {
dividendsPerShare += ( received * precision ) / totalShares;
totalRewards += received;
}
}
function _claimReward(address user) internal {
// exit if zero value locked
if (userInfo[user].amount == 0) {
return;
}
// fetch pending rewards
uint256 amount = pendingRewards(user);
// exit if zero rewards
if (amount == 0) {
return;
}
// update total excluded
userInfo[user].totalExcluded = getCumulativeDividends(userInfo[user].amount);
// transfer reward to user
require(
IERC20(reward).transfer(user, amount),
'Failure On Token Claim'
);
}
function _transferIn(address _token, uint256 amount) internal returns (uint256) {
uint before = IERC20(_token).balanceOf(address(this));
IERC20(_token).transferFrom(msg.sender, address(this), amount);
uint After = IERC20(_token).balanceOf(address(this));
require(
After > before,
'Error On TransferIn'
);
return After - before;
}
function timeUntilUnlock(address user) public view returns (uint256) {
return userInfo[user].unlockTime < block.timestamp ? 0 : userInfo[user].unlockTime - block.timestamp;
}
function pendingRewards(address shareholder) public view returns (uint256) {
if(userInfo[shareholder].amount == 0){ return 0; }
uint256 totalDividends = getCumulativeDividends(userInfo[shareholder].amount);
uint256 tExcluded = userInfo[shareholder].totalExcluded;
if(totalDividends <= tExcluded){ return 0; }
return totalDividends <= tExcluded ? 0 : totalDividends - tExcluded;
}
function getCumulativeDividends(uint256 share) internal view returns (uint256) {
return ( share * dividendsPerShare ) / precision;
}
receive() external payable {}
}
{
"compilationTarget": {
"StakingContract.sol": "StakingContract"
},
"evmVersion": "paris",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": true,
"runs": 200
},
"remappings": []
}
[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"oldOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnerSet","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"earlyFee","type":"uint256"}],"name":"SetEarlyFee","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"FeeRecipient","type":"address"}],"name":"SetFeeRecipient","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"LockTime","type":"uint256"}],"name":"SetLockTime","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Transfer","type":"event"},{"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"changeOwner","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"claimRewards","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"depositRewards","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"feeRecipient","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getOwner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"leaveEarlyFee","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"lockTime","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"shareholder","type":"address"}],"name":"pendingRewards","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"reward","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"newFeeRecipient","type":"address"}],"name":"setFeeRecipient","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"newEarlyFee","type":"uint256"}],"name":"setLeaveEarlyFee","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"newLockTime","type":"uint256"}],"name":"setLockTime","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"stake","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"user","type":"address"}],"name":"timeUntilUnlock","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"token","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalRewards","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"userInfo","outputs":[{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"uint256","name":"unlockTime","type":"uint256"},{"internalType":"uint256","name":"totalExcluded","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"withdraw","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"token_","type":"address"}],"name":"withdrawForeignToken","outputs":[],"stateMutability":"nonpayable","type":"function"},{"stateMutability":"payable","type":"receive"}]