Skip to content

Instantly share code, notes, and snippets.

@szerintedmi
Last active October 23, 2017 18:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save szerintedmi/2af8759dd0136f5d167921525a184ebd to your computer and use it in GitHub Desktop.
Save szerintedmi/2af8759dd0136f5d167921525a184ebd to your computer and use it in GitHub Desktop.
solidity example contract
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