Skip to content

Instantly share code, notes, and snippets.

@alecchampaign
Last active December 1, 2017 02:58
Show Gist options
  • Save alecchampaign/5d7fe9158be92b7f83d5091633e0112b to your computer and use it in GitHub Desktop.
Save alecchampaign/5d7fe9158be92b7f83d5091633e0112b to your computer and use it in GitHub Desktop.
A simple fund raising contract.
pragma solidity ^0.4.18;
/** Simple fund raising contract */
contract Donation
{
address beneficiary;
uint donations;
uint goal;
uint amountRaised;
// Authenticates contract creator
modifier beneficiaryOnly(address _beneficiary) {
require(msg.sender == _beneficiary);
_;
}
// Events
event donationSent(uint _amount, address _donator);
event donationPeriodEnded(uint _amount);
// Constructor
function Donation (uint _goal) public payable {
beneficiary = msg.sender;
donations = msg.value;
goal = _goal;
}
// Returns the current amount of ether raised
function getAmountRaised() public payable returns(uint) {
amountRaised = donations;
return(amountRaised);
}
// Returns the current goal
function getGoal() constant public returns(uint) {
return(goal);
}
// Returns the beneficiary of the fundraiser.
function getBeneficiary() constant public returns(address) {
return(beneficiary);
}
/** Send a donation */
function sendDonation() public payable {
require(msg.value + donations <= goal && msg.sender != address(0));
donations += msg.value;
donationSent(msg.value, msg.sender);
}
/** End the donation period without killing the contract */
function endDonationPeriod() public payable beneficiaryOnly(beneficiary) {
beneficiary.transfer(donations);
amountRaised = donations;
donations = 0;
goal = 0;
donationPeriodEnded(amountRaised);
}
/** Kill the contract */
function killContract() public payable beneficiaryOnly(beneficiary) {
selfdestruct(beneficiary);
}
// Fallback function
function () public payable {
revert();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment