-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathDistribute.sol
118 lines (88 loc) · 3.5 KB
/
Distribute.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// SPDX-License-Identifier: MIT
pragma solidity ^0.5.0;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/math/SafeMath.sol";
contract Distribute {
using SafeMath for uint256;
event TokensReleased(uint256 amount, uint256 height);
event TokensRevoked(uint256 amount, address to);
// init in constructor
IERC20 public token;
// adjustable by the owner
address public beneficiary;
uint256 public startBlock;
uint256 public stableHeight = 30;
uint256 public blocksPerCycle = 6600;
uint256 public releasedPerBlock;
uint256 public totalReleased;
uint256 public latestReleasedHeight;
address public owner;
modifier onlyOwner() {
require(msg.sender == owner, "only owner");
_;
}
constructor (address tokenAddr) public {
require(tokenAddr != address(0), "token is the zero address");
token = IERC20(tokenAddr);
owner = msg.sender;
}
function setBeneficiary(address addr) onlyOwner public {
require(addr != address(0), "beneficiary is the zero address");
beneficiary = addr;
}
function setStartBlock(uint256 start) onlyOwner public {
require(startBlock == 0, "startBlock can only set once");
startBlock = start;
latestReleasedHeight = startBlock;
}
function setStableHeight(uint256 stable) onlyOwner public {
require(stable < blocksPerCycle, "stable height is longer than blocks per cycle");
stableHeight = stable;
}
function setBlocksPerCycle(uint256 count) onlyOwner public {
require(count > stableHeight, "blocks per cycle is lower than stable height");
blocksPerCycle = count;
}
function setReleasedPerBlock(uint256 amount) onlyOwner public {
releasedPerBlock = amount;
}
function revoke() onlyOwner public {
uint256 currentBalance = token.balanceOf(address(this));
require(currentBalance > 0, "balance is zero");
token.transfer(owner, currentBalance);
emit TokensRevoked(currentBalance, owner);
}
function release() public {
require(beneficiary != address(0), "beneficiary is not set");
(uint256 unreleased, uint256 height) = releasableAmount();
require(unreleased > 0, "Distribute: no tokens are due");
uint256 currentBalance = token.balanceOf(address(this));
if (unreleased > currentBalance) {
unreleased = currentBalance;
require(unreleased > 0, "Distribute: no tokens are due");
}
totalReleased = totalReleased.add(unreleased);
latestReleasedHeight = height;
token.transfer(beneficiary, unreleased);
emit TokensReleased(unreleased, height);
}
function releasableAmount() public view returns (uint256 amount, uint256 height) {
if (startBlock == 0) {
return (0, 0);
}
if (releasedPerBlock == 0 || block.number < latestReleasedHeight) {
return (0, latestReleasedHeight);
}
uint256 cycles = block.number.sub(latestReleasedHeight).div(blocksPerCycle);
height = latestReleasedHeight.add(cycles.mul(blocksPerCycle));
if (cycles > 0 && block.number.sub(height) < stableHeight) {
cycles = cycles.sub(1);
height = height.sub(blocksPerCycle);
}
if (cycles == 0) {
return (0, height);
}
amount = cycles.mul(blocksPerCycle).mul(releasedPerBlock);
return (amount, height);
}
}