/
StakedDistributor.sol
73 lines (59 loc) · 2.03 KB
/
StakedDistributor.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
64
65
66
67
68
69
70
71
72
73
//SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.10;
import '@openzeppelin/contracts/token/ERC20/ERC20.sol';
import './libraries/SafeToken.sol';
import './Distributor.sol';
contract StakedDistributor is Distributor, ERC20 {
using SafeToken for address;
struct Withdrawal {
uint256 amount;
uint256 releaseTime;
}
event Withdraw(address indexed user, uint256 amount);
uint256 public withdrawalPendingTime = 7 * 1 days;
mapping(address => Withdrawal) public withdrawal;
address public immutable sonne;
constructor(
address sonne_,
string memory name,
string memory symbol
) ERC20(name, symbol) {
sonne = sonne_;
}
function mint(uint256 amount) public {
sonne.safeTransferFrom(msg.sender, address(this), amount);
_mint(msg.sender, amount);
}
function burn(uint256 amount) public {
if (amount > 0) {
_burn(msg.sender, amount);
}
Withdrawal storage withdrawal_ = withdrawal[msg.sender];
withdrawal_.amount = withdrawal_.amount + amount;
withdrawal_.releaseTime = block.timestamp + withdrawalPendingTime;
}
function withdraw() public {
Withdrawal storage withdrawal_ = withdrawal[msg.sender];
require(block.timestamp >= withdrawal_.releaseTime, 'StakedDistributor: not released');
uint256 amount = withdrawal_.amount;
withdrawal_.amount = 0;
sonne.safeTransfer(msg.sender, amount);
emit Withdraw(msg.sender, amount);
}
function _afterTokenTransfer(
address from,
address to,
uint256 /* amount */
) internal override {
if (from != address(0)) {
_editRecipientInternal(from, balanceOf(from));
}
if (to != address(0)) {
_editRecipientInternal(to, balanceOf(to));
}
}
/* Admin functions */
function setWithdrawalPendingTime(uint256 withdrawalPendingTime_) public onlyOwner {
withdrawalPendingTime = withdrawalPendingTime_;
}
}