-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstakingContract.sol
63 lines (46 loc) · 2.02 KB
/
stakingContract.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
/// @title Stake Contract
/// @author Glory Praise Emmanuel
contract stakeContract {
// @dev Collect funds in a payable `stake()` function and track individual `balances` with a mapping:
mapping(address => uint256) balances;
// Make sure to add a `Stake(address,uint256)` event and emit it
event StakeEvent(address, uint256);
// @dev withdraw modifier
modifier DeadlineCheck {
require(block.timestamp > deadline, "Wait for maturity time na");
_;
}
// After some `deadline` allow anyone to call an `execute()` function
uint deadline = block.timestamp + 5 minutes;
// @dev stake function
function stake(address _address, uint256 _amount) public payable{
require(_amount > 0, "No amount staked biko, stake funds ogben");
balances[_address] += _amount;
emit StakeEvent(_address, _amount);
}
// @dev show amount staked by address
function showStakedAmount(address _address) public view returns( uint) {
return balances[_address];
}
// @dev show total money staked
function stakePoolBalance() public view returns (uint){
return address(this).balance;
}
// Add a `timeLeft()` view function that returns the time left before the deadline for the frontend
function timeLeft() public view returns(uint256) {
return deadline - block.timestamp;
}
// If the `threshold` was not met, allow everyone to call a `withdraw()` function
// Add a `withdraw()` function to let users withdraw their balance
function withdraw(address _address, uint256 _amount) public DeadlineCheck {
require(balances[_address] < 0, "You don't have a stake");
require(balances[_address] < _amount, "You stake less than this na, you be thief?");
payable (_address).transfer(_amount);
}
// Add the `receive()` special function that receives eth and calls stake()
receive() payable external {
stake(msg.sender, msg.value);
}
}