'''
pragma solidity ^0.4.2;
contract token { function transfer(address receiver, uint amount){ } }
contract Crowdsale {
address public beneficiary;//受益人
uint public fundingGoal; //目標(biāo)金額
uint public amountRaised;
uint public deadline; //截止時(shí)間
uint public price; //每個(gè)價(jià)格
token public tokenReward;
mapping(address => uint256) public balanceOf;//每個(gè)地址的眾籌數(shù)目把敢,map類型
bool fundingGoalReached = false;
event GoalReached(address beneficiary, uint amountRaised); //事件晌块,眾籌目標(biāo)達(dá)到
event FundTransfer(address backer, uint amount, bool isContribution);//事件,發(fā)送資金發(fā)送時(shí)候
bool crowdsaleClosed = false;
/* data structure to hold information about campaign contributors */
/* at initialization, setup the owner */
function Crowdsale(
address ifSuccessfulSendTo, //眾籌成功后河质,轉(zhuǎn)移目標(biāo)地址
uint fundingGoalInEthers, //投資目標(biāo)
uint durationInMinutes, //持續(xù)時(shí)間
uint etherCostOfEachToken, //每個(gè)的最小投資額度
token addressOfTokenUsedAsReward //當(dāng)前受益地址
) {
beneficiary = ifSuccessfulSendTo;
fundingGoal = fundingGoalInEthers * 1 ether;
deadline = now + durationInMinutes * 1 minutes;
price = etherCostOfEachToken * 1 ether;
tokenReward = token(addressOfTokenUsedAsReward); //受益地址,數(shù)量合約
}
/* The function without name is the default function that is called whenever anyone sends funds to a contract */
function () payable {//合約默認(rèn)執(zhí)行函數(shù)抹缕,在有人給合約地址發(fā)送資金時(shí)執(zhí)行
if (crowdsaleClosed) throw;//眾籌關(guān)閉時(shí),拋出異常
uint amount = msg.value;
balanceOf[msg.sender] = amount; //計(jì)入統(tǒng)計(jì),發(fā)送者發(fā)送的數(shù)量
amountRaised += amount; //累計(jì)收到的金額
tokenReward.transfer(msg.sender, amount / price);
FundTransfer(msg.sender, amount, true); //發(fā)送資金變動(dòng)事件通知
}
// modifier可以方便的驗(yàn)證輸入信息
modifier afterDeadline() { if (now >= deadline) _; }//modifier 修飾符堕仔,函數(shù)啟動(dòng)前執(zhí)行。_表示函數(shù)體占位符毒涧,此處表示afterDeadline修飾的函數(shù)本體贮预。
//檢查是否眾籌成功,達(dá)到目標(biāo)金額
/* checks if the goal or time limit has been reached and ends the campaign */
function checkGoalReached() afterDeadline {
if (amountRaised >= fundingGoal){ //收集總金額大于目標(biāo)金額
fundingGoalReached = true; //修改眾籌金額達(dá)成標(biāo)志為成功
GoalReached(beneficiary, amountRaised); //觸發(fā)金額達(dá)到事件
}
crowdsaleClosed = true; //眾籌標(biāo)識(shí)關(guān)閉
}
//安全撤出, 有afterDeadline的話契讲,只有到眾籌結(jié)束時(shí)間才可以執(zhí)行
function safeWithdrawal() afterDeadline {
if (!fundingGoalReached) {//如果眾籌眾籌金額未達(dá)成
uint amount = balanceOf[msg.sender];
balanceOf[msg.sender] = 0;
if (amount > 0) {
if (msg.sender.send(amount)) {//金額退回資金發(fā)送者
FundTransfer(msg.sender, amount, false);
} else {
balanceOf[msg.sender] = amount;//記錄放入balanceOfmap里面
}
}
}
if (fundingGoalReached && beneficiary == msg.sender) {//眾籌金額達(dá)成仿吞,且發(fā)送者(查詢者)是受益人
if (beneficiary.send(amountRaised)) {//金額發(fā)送給受益人
FundTransfer(beneficiary, amountRaised, false);
} else {
//If we fail to send the funds to beneficiary, unlock funders balance
fundingGoalReached = false; ////眾籌金額未達(dá)成
}
}
}
}
'''