🔒Stake
Go20 Stake Program
// Some codepragma solidity ^0.8.0;
contract TokenStaking {
address public owner;
uint256 public totalStaked;
mapping(address => uint256) public stakes;
mapping(address => uint256) public stakingTimes;
uint256 public constant THREE_MONTHS = 7884000; // 3 months in seconds
uint256 public constant SIX_MONTHS = 15768000; // 6 months in seconds
uint256 public constant TWELVE_MONTHS = 31536000; // 12 months in seconds
uint256 public constant THREE_MONTHS_RETURN = 6; // 6% return for 3 months
uint256 public constant SIX_MONTHS_RETURN = 15; // 15% return for 6 months
uint256 public constant TWELVE_MONTHS_RETURN = 45; // 45% return for 12 months
event Staked(address indexed staker, uint256 amount, uint256 stakingTime);
event Withdrawn(address indexed staker, uint256 amount, uint256 stakingTime, uint256 reward);
constructor() {
owner = msg.sender;
}
function stake(uint256 amount, uint256 stakingTime) public {
require(amount > 0, "Amount must be greater than 0");
require(stakingTime == THREE_MONTHS || stakingTime == SIX_MONTHS || stakingTime == TWELVE_MONTHS, "Invalid staking time");
require(stakes[msg.sender] == 0, "Already staked");
require(amount > 100, "Minimum stake is 100 tokens");
require(amount <= 10000, "Maximum stake is 10,000 tokens");
// Transfer tokens from sender to contract
// Assuming token contract is already deployed and approved for transfer
// Replace the address with the token contract address and use the transferFrom() function
// If token is ETH, use the payable keyword and msg.value
// Remember to add the token balance to totalStaked variable
// Also, remember to add stakingTime and amount to their respective mappings
// Emit the Staked event
totalStaked += amount;
stakes[msg.sender] = amount;
stakingTimes[msg.sender] = block.timestamp + stakingTime;
emit Staked(msg.sender, amount, stakingTime);
}
function withdraw() public {
require(stakes[msg.sender] > 0, "No stake to withdraw");
require(block.timestamp >= stakingTimes[msg.sender], "Staking time not over yet");
uint256 reward;
uint256 stakingTime = stakingTimes[msg.sender] - block.timestamp;
if (stakingTime == THREE_MONTHS) {
reward = stakes[msg.sender] * THREE_MONTHS_RETURN / 100;
} else if (stakingTime == SIX_MONTHS) {
reward = stakes[msg.sender] * SIX_MONTHS_RETURN / 100;
} else {
reward = stakes[msg.sender] * TWELVE_MONTHS_RETURN / 100;
}
// Transfer tokens and reward to sender
// If token is ETH, use the transfer() function instead of transferFrom()
// Remember to subtract stake and reward from totalStaked variable
// Also, remember to delete stake and stakingTime from their respective mappings
// Emit the Withdrawn event
totalStaked -= stakes[msg.sender];
Staking
Last updated