Created
May 23, 2018 13:50
-
-
Save jennazenk/59e26196b8706c8805def73b8bdab3a2 to your computer and use it in GitHub Desktop.
Vesting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.17; | |
import "./dependencies/safeMath.sol"; | |
import "./dependencies/DBC.sol"; | |
import "./dependencies/Owned.sol"; | |
import "./dependencies/ERC20.sol"; | |
contract Vesting is DBC, Owned { | |
using safeMath for uint; | |
// FIELDS | |
// Constructor fields | |
ERC20 public MELON_CONTRACT; // Melon as ERC20 contract | |
// Methods fields | |
uint public totalVestedAmount; // Quantity of vested Melon in total | |
uint public vestingStartTime; // Timestamp when vesting is set | |
uint public vestingPeriod; // Total vesting period in seconds | |
address public beneficiary; // Address of the beneficiary | |
// CONSTANT METHODS | |
function isBeneficiary() constant returns (bool) { return msg.sender == beneficiary; } | |
function isVestingStarted() constant returns (bool) { return totalVestedAmount != 0; } | |
function withdrawnMelon() constant returns (uint) { | |
return totalVestedAmount.sub(MELON_CONTRACT.balanceOf(this)); | |
} | |
/// @notice Calculates the quantity of Melon asset that's currently withdrawable | |
/// @return withdrawable Quantity of withdrawable Melon asset | |
function calculateWithdrawable() constant returns (uint withdrawable) { | |
uint timePassed = now.sub(vestingStartTime); | |
if (timePassed < vestingPeriod) { | |
uint vested = totalVestedAmount.mul(timePassed).div(vestingPeriod); | |
withdrawable = vested.sub(withdrawnMelon()); | |
} else { | |
withdrawable = totalVestedAmount.sub(withdrawnMelon()); | |
} | |
} | |
// NON-CONSTANT METHODS | |
/// @param ofMelonAsset Address of Melon asset | |
function Vesting(address ofMelonAsset) { | |
MELON_CONTRACT = ERC20(ofMelonAsset); | |
} | |
/// @param ofBeneficiary Address of beneficiary | |
/// @param ofMelonQuantity Address of Melon asset | |
/// @param ofVestingPeriod Vesting period in seconds from vestingStartTime | |
function setVesting(address ofBeneficiary, uint ofMelonQuantity, uint ofVestingPeriod) | |
pre_cond(!isVestingStarted()) | |
{ | |
assert(MELON_CONTRACT.transferFrom(msg.sender, this, ofMelonQuantity)); | |
vestingStartTime = now; | |
totalVestedAmount = ofMelonQuantity; | |
vestingPeriod = ofVestingPeriod; | |
beneficiary = ofBeneficiary; | |
} | |
/// @notice Withdraw | |
function withdraw() | |
pre_cond(isBeneficiary()) | |
pre_cond(isVestingStarted()) | |
{ | |
uint withdrawable = calculateWithdrawable(); | |
assert(MELON_CONTRACT.transfer(beneficiary, withdrawable)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment