Skip to content

Instantly share code, notes, and snippets.

@amarachiugwu
Last active August 1, 2022 19:30
Show Gist options
  • Select an option

  • Save amarachiugwu/6e758031caaaf4457f61ba20d7f9a3cf to your computer and use it in GitHub Desktop.

Select an option

Save amarachiugwu/6e758031caaaf4457f61ba20d7f9a3cf to your computer and use it in GitHub Desktop.
// Collect funds in a payable `stake()` function and track individual `balances` with a mapping:
// ( Make sure to add a `Stake(address,uint256)` event and emit)
// After some `deadline` allow anyone to call an `execute()` function
// If the `threshold` was not met, allow everyone to call a `withdraw()` function
// Add a `withdraw()` function to let users withdraw their balance
// Add a `timeLeft()` view function that returns the time left before the deadline
// Add the `receive()` special function that receives eth and calls stake()
// receive() payable external {
// stake();
// }
// SPDX-License-Identifier: MIT
pragma solidity 0.8.0;
contract Staking {
// emit an event when an end users stakes
event Stake(address user, uint amount);
// a deadline when passed allows end users to stake
uint256 deadline = block.timestamp + 30 seconds;
// track individual balances
mapping(address => uint) balances;
// Collect funds here like deposit
function stake() public payable {
balances[msg.sender] += msg.value;
}
// returns the balance of a user in the pool
function userBalance(address _user) external view returns(uint) {
return balances[_user];
}
// special receive function that receives eth and calls stake()
receive() external payable {
stake();
emit Stake(msg.sender, msg.value);
}
// allows a user to withdral only when the deadline is passed
function withdraw() public {
require(deadline < block.timestamp, "Deadline not exceeded");
require(balances[msg.sender] > 0, "No stake");
payable(msg.sender).transfer(balances[msg.sender]);
}
// function that returns the time left before the deadline
function timeLeft () public view returns(uint t) {
// t is automaticall returned
// because it is initialized in the return block
t = deadline - block.timestamp;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment