Last active
October 23, 2017 18:23
-
-
Save szerintedmi/2af8759dd0136f5d167921525a184ebd to your computer and use it in GitHub Desktop.
solidity example contract
This file contains hidden or 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.18; | |
contract Endowment { | |
address public beneficiary; | |
address public owner; | |
uint public lastRedeem; | |
uint constant public period = 1 minutes; | |
uint public installmentAmount; | |
function Endowment(address _beneficiary, uint _installmentAmount) payable { | |
require(_installmentAmount > 0); | |
require(_beneficiary != 0); | |
owner = msg.sender; | |
beneficiary = _beneficiary; | |
installmentAmount = _installmentAmount; | |
} | |
function () payable public { | |
} | |
event redeemed(); | |
function redeem() public { | |
require(msg.sender == beneficiary); | |
require (this.balance > 0); | |
uint toSend = getRedeemable(); | |
require(toSend > 0); | |
lastRedeem = now / period; | |
beneficiary.transfer(toSend); | |
redeemed(); | |
} | |
function getRedeemable() public view returns (uint256 redeemableAmount) { | |
uint installments; | |
if( lastRedeem == 0) { | |
installments = 1; // first redeem | |
} else if(now/period <= lastRedeem) { | |
installments = 0; | |
} else { | |
installments = now /period - lastRedeem; | |
} | |
redeemableAmount = installments * installmentAmount; | |
if(this.balance < redeemableAmount) { | |
redeemableAmount = this.balance; | |
} | |
return redeemableAmount; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment