-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathVesting.sol
More file actions
68 lines (59 loc) · 2.13 KB
/
Vesting.sol
File metadata and controls
68 lines (59 loc) · 2.13 KB
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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
/**
* @title Vesting v1
* @dev This contract handles the vesting of Eth for a given beneficiary.
* The vesting schedule is customizable through the {setDuration} function.
*/
contract Vesting {
event EtherReleased(uint256 amount);
address public beneficiary;
uint256 public duration;
uint256 public start;
uint256 public released;
/**
* @dev Set the start timestamp of the vesting wallet.
*/
function setStart(uint256 startTimestamp) public {
require(start == 0, "already set");
start = startTimestamp;
}
/**
* @dev Set the vesting duration of the vesting wallet.
*/
function setDuration(uint256 durationSeconds) public {
require(durationSeconds > duration, "You cant decrease the vesting time!");
duration = durationSeconds;
}
/**
* @dev Release the native token (ether) that have already vested.
*
* Emits a {EtherReleased} event.
*/
function release() public virtual {
uint256 releasable = vestedAmount(block.timestamp) - released;
released += releasable;
emit EtherReleased(releasable);
(bool success,) = payable(beneficiary).call{value: releasable}("");
require(success, "tx failed");
}
/**
* @dev Calculates the amount of ether that has already vested. Default implementation is a linear vesting curve.
*/
function vestedAmount(uint256 timestamp) public view virtual returns (uint256) {
return _vestingSchedule(address(this).balance + released, timestamp);
}
/**
* @dev Virtual implementation of the vesting formula. This returns the amount vested, as a function of time, for
* an asset given its total historical allocation.
*/
function _vestingSchedule(uint256 totalAllocation, uint256 timestamp) internal view virtual returns (uint256) {
if (timestamp < start) {
return 0;
} else if (timestamp > start + duration) {
return totalAllocation;
} else {
return (totalAllocation * (timestamp - start)) / duration;
}
}
}