-
-
Save VaWheel/072250e3b419fb9ad8e5c9b411776579 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Stake.sol | |
// Uses aves to stake aves | |
// To the smart contract is supply the amount of aves to supply the reward for the stakers | |
pragma solidity ^0.8.20; | |
contract StakeAVS { | |
address public owner; | |
struct Staker { | |
uint256 amount; | |
uint256 block_number; | |
address staker_address; | |
uint256 reward; | |
} | |
uint256 public percentage_per_30000_blocks = 1; | |
uint256 public limit = 100000; | |
bool public stop = false; | |
mapping(address => Staker) public stakers; | |
constructor() { | |
owner = msg.sender; | |
} | |
// function to calulate the reward | |
// user can't withdraw before 10000 blocks | |
function calculate_reward(address _staker_address) public view returns(uint256) { | |
Staker memory staker = stakers[_staker_address]; | |
uint256 blocks_since_stake = block.number - staker.block_number; | |
require(blocks_since_stake >= 30000, "You can't withdraw before 10000 blocks"); | |
uint256 total_pre = blocks_since_stake / 30000; | |
uint256 reward = (staker.amount * percentage_per_30000_blocks * total_pre) / 100; | |
return reward; | |
} | |
// calulate_reward_test | |
function calculate_reward_test(uint256 staked_amount, uint256 blocks_since_stake) public view returns(uint256) { | |
uint256 total_pre = blocks_since_stake / 30000; | |
uint256 reward = (staked_amount * percentage_per_30000_blocks * total_pre) / 100; | |
return reward; | |
} | |
// view function to check the balance of the contract | |
function check_balance() public view returns(uint256) { | |
return address(this).balance; | |
} | |
// function to withdraw aveseum | |
function withdraw() public { | |
Staker storage staker = stakers[msg.sender]; | |
uint256 reward = calculate_reward(msg.sender); | |
payable(msg.sender).transfer(reward + staker.amount); | |
staker.reward += reward; | |
staker.amount = 0; | |
staker.block_number = 0; | |
} | |
function premature_withdraw() public { | |
Staker storage staker = stakers[msg.sender]; | |
payable(msg.sender).transfer(staker.amount); | |
staker.amount = 0; | |
staker.block_number = 0; | |
staker.reward = 0; | |
} | |
function stake() public payable { | |
require(msg.value > 0, "You need to send some aves"); | |
require(msg.value < limit * 10 ** 18, "You can't stake more than 100000 aves"); | |
require(stop == false, "Staking is stopped"); | |
Staker storage staker = stakers[msg.sender]; | |
staker.amount += msg.value; | |
staker.block_number = block.number; | |
staker.staker_address = msg.sender; | |
staker.reward = 0; | |
} | |
// function to check reward for msg.sender | |
function check_reward() public view returns(uint256) { | |
return calculate_reward(msg.sender); | |
} | |
// function to check the amount of aves staked | |
function check_staked() public view returns(uint256) { | |
return stakers[msg.sender].amount; | |
} | |
// function to check the block number of the staker | |
function check_block_number() public view returns(uint256) { | |
return stakers[msg.sender].block_number; | |
} | |
// change owner | |
function change_owner(address _new_owner) public { | |
require(msg.sender == owner, "You are not the owner"); | |
owner = _new_owner; | |
} | |
function withdraw_aves(uint256 _amount) public { | |
require(msg.sender == owner, "You are not the owner"); | |
payable(msg.sender).transfer(_amount); | |
} | |
function stop_staking() public { | |
require(msg.sender == owner, "You are not the owner"); | |
stop = true; | |
} | |
function start_staking() public { | |
require(msg.sender == owner, "You are not the owner"); | |
stop = false; | |
} | |
// | |
// MAKE THE contract recieve aves for the funds of stakers | |
// when we recive do nothing | |
receive() external payable { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment