编译器
0.8.20+commit.a1b79de6
文件 1 的 4:Context.sol
pragma solidity ^0.8.20;
abstract contract Context {
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes calldata) {
return msg.data;
}
function _contextSuffixLength() internal view virtual returns (uint256) {
return 0;
}
}
文件 2 的 4:IERC20.sol
pragma solidity ^0.8.20;
interface IERC20 {
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address to, uint256 value) external returns (bool);
function allowance(address owner, address spender) external view returns (uint256);
function approve(address spender, uint256 value) external returns (bool);
function transferFrom(address from, address to, uint256 value) external returns (bool);
}
文件 3 的 4:Ownable.sol
pragma solidity ^0.8.20;
import {Context} from "../utils/Context.sol";
abstract contract Ownable is Context {
address private _owner;
error OwnableUnauthorizedAccount(address account);
error OwnableInvalidOwner(address owner);
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
constructor(address initialOwner) {
if (initialOwner == address(0)) {
revert OwnableInvalidOwner(address(0));
}
_transferOwnership(initialOwner);
}
modifier onlyOwner() {
_checkOwner();
_;
}
function owner() public view virtual returns (address) {
return _owner;
}
function _checkOwner() internal view virtual {
if (owner() != _msgSender()) {
revert OwnableUnauthorizedAccount(_msgSender());
}
}
function renounceOwnership() public virtual onlyOwner {
_transferOwnership(address(0));
}
function transferOwnership(address newOwner) public virtual onlyOwner {
if (newOwner == address(0)) {
revert OwnableInvalidOwner(address(0));
}
_transferOwnership(newOwner);
}
function _transferOwnership(address newOwner) internal virtual {
address oldOwner = _owner;
_owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}
}
文件 4 的 4:svstaking.sol
pragma solidity ^0.8.20;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract ShivaVoneStaker is Ownable {
enum MinimumLockTime {
ONEMONTH,
THREEMONTHS,
SIXMONTHS,
ONEYEAR
}
IERC20 stakeToken;
IERC20 rewardToken;
uint8 decimals;
uint256 rewardPercentage = 1;
uint256 yearInSeconds = 365 days;
uint256 public totalStakedTokens;
uint256 public stakers;
MinimumLockTime public minimumLockTime;
mapping(address => uint) public stakingBalance;
mapping(address => bool) public isStaking;
mapping(address => uint256) public latestStakedTime;
constructor(IERC20 _stakeToken, IERC20 _rewardToken,uint8 _stakeTokenDecimals) Ownable(msg.sender) {
stakeToken = _stakeToken;
rewardToken = _rewardToken;
decimals = _stakeTokenDecimals;
}
function stakeTokens(uint _amount) public {
uint256 minimumAmount = 1_000_000 * 10 ** decimals;
require(_amount > 0 && _amount >= minimumAmount, "Must be greater than 1 million");
require(stakeToken.balanceOf(msg.sender) >= _amount, "Insufficient balance");
stakeToken.transferFrom(msg.sender, address(this), _amount);
stakingBalance[msg.sender] += _amount ;
if (!isStaking[msg.sender]) stakers += 1;
isStaking[msg.sender] = true;
latestStakedTime[msg.sender] = block.timestamp;
totalStakedTokens += _amount;
}
function unstakeTokens() public {
uint256 minTime = _getMinimumLockTime();
require(isStaking[msg.sender], "NO STAKE");
uint256 duration = block.timestamp - latestStakedTime[msg.sender];
require(duration > minTime, "Time not exceeded");
uint256 balance = stakingBalance[msg.sender];
uint256 reward = _calculateRewards(msg.sender);
stakingBalance[msg.sender] = 0;
isStaking[msg.sender] = false;
latestStakedTime[msg.sender] = 0;
stakeToken.transfer(msg.sender, balance);
rewardToken.transfer(msg.sender, reward);
}
function calculateRewards(address _staker) external view returns(uint256 reward) {
reward = _calculateRewards(_staker);
}
function _calculateRewards(address _staker) internal view returns(uint256 reward) {
uint256 balance = stakingBalance[_staker];
uint256 duration = block.timestamp - latestStakedTime[_staker];
reward = (duration * balance * rewardPercentage) / yearInSeconds;
}
function _getMinimumLockTime() internal view returns (uint256 minTime){
if (minimumLockTime == MinimumLockTime.ONEYEAR){
minTime = 365 days;
}
else if (minimumLockTime == MinimumLockTime.SIXMONTHS) {
minTime = 180 days;
}
else if (minimumLockTime == MinimumLockTime.THREEMONTHS) {
minTime = 90 days;
}
else {
minTime = 30 days;
}
}
function emergencyWithdrawal(IERC20 _token, uint _amount, address receiver) external onlyOwner {
require(_token != stakeToken, "Cannot withdraw staked tokens");
require(_token.balanceOf(address(this)) > 0, "No tokens to withdraw");
_token.transfer(receiver, _amount);
}
function withDrawRewardTokens(uint _amount, address receiver) external onlyOwner {
require(rewardToken.balanceOf(address(this)) > 0, "No tokens to withdraw");
rewardToken.transfer(receiver, _amount);
}
function setRewardPercentage(uint _amount) external onlyOwner {
rewardPercentage = _amount;
}
function setMinimumLockTime(MinimumLockTime _time) external onlyOwner {
minimumLockTime = _time;
}
function setTimeToRatio(uint256 _timeInSeconds) external onlyOwner {
yearInSeconds = _timeInSeconds;
}
}
{
"compilationTarget": {
"svstaking.sol": "ShivaVoneStaker"
},
"evmVersion": "shanghai",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": true,
"runs": 200
},
"remappings": []
}
[{"inputs":[{"internalType":"contract IERC20","name":"_stakeToken","type":"address"},{"internalType":"contract IERC20","name":"_rewardToken","type":"address"},{"internalType":"uint8","name":"_stakeTokenDecimals","type":"uint8"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[{"internalType":"address","name":"owner","type":"address"}],"name":"OwnableInvalidOwner","type":"error"},{"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"OwnableUnauthorizedAccount","type":"error"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"inputs":[{"internalType":"address","name":"_staker","type":"address"}],"name":"calculateRewards","outputs":[{"internalType":"uint256","name":"reward","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"contract IERC20","name":"_token","type":"address"},{"internalType":"uint256","name":"_amount","type":"uint256"},{"internalType":"address","name":"receiver","type":"address"}],"name":"emergencyWithdrawal","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"isStaking","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"latestStakedTime","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"minimumLockTime","outputs":[{"internalType":"enum ShivaVoneStaker.MinimumLockTime","name":"","type":"uint8"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"enum ShivaVoneStaker.MinimumLockTime","name":"_time","type":"uint8"}],"name":"setMinimumLockTime","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"setRewardPercentage","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_timeInSeconds","type":"uint256"}],"name":"setTimeToRatio","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"stakeTokens","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"stakers","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"stakingBalance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalStakedTokens","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"unstakeTokens","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_amount","type":"uint256"},{"internalType":"address","name":"receiver","type":"address"}],"name":"withDrawRewardTokens","outputs":[],"stateMutability":"nonpayable","type":"function"}]