Last active
August 4, 2022 16:51
-
-
Save casweeney/66ac458ae81d89c87dc812c68f38fa79 to your computer and use it in GitHub Desktop.
Staking Contract
This file contains hidden or 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
| // SPDX-License-Identifier: MIT | |
| pragma solidity 0.8.0; | |
| contract Staking { | |
| event Stake(address user, uint amount); | |
| uint256 deadline = block.timestamp + 5 minutes; | |
| mapping(address => uint) balances; | |
| function stake(address _user) public payable { | |
| require(msg.value > 0, "Insuficient funds"); | |
| balances[_user] += msg.value; | |
| } | |
| function userBalance(address _user) external view returns(uint) { | |
| return balances[_user]; | |
| } | |
| receive() external payable { | |
| stake(msg.sender); | |
| emit Stake(msg.sender, msg.value); | |
| } | |
| function withdraw() public { | |
| require(deadline < block.timestamp, "Deadline exceeded"); | |
| require(balances[msg.sender] > 0, "No stake"); | |
| uint amount = balances[msg.sender]; | |
| balances[msg.sender] = 0; | |
| payable(msg.sender).transfer(amount); | |
| } | |
| function changeDeadline(uint _time) public { | |
| deadline = block.timestamp + _time; | |
| } | |
| function showDeadline() external view returns (uint) { | |
| return deadline; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment