函数解释:
newCampaign:创建公司受益人以及筹款目标
contribute:进行捐赠,并将捐赠名单计入结构体
CheckGoalReached:若达到众筹目标,清空amout,并将goal转给受益者
调用次序说明:
合约代码:
pragma solidity ^0.4.11;
contract CrowdFunding{
struct Funder{
address addr;
uint amout;
}
struct Campaign{
address beneficiary;
uint fundingGoal;
uint numFunders;
uint amount;
//when initialize the Campaign
//ignore the mapping
mapping(uint => Funder) funders;
}
uint numCampaigns;
mapping(uint => Campaign) campaigns;
function newCampaign(address beneficiary, uint goal)
public returns (uint campaignID){
campaignID = numCampaigns++;
campaigns[campaignID] = Campaign(beneficiary, goal, 0, 0);
}
function contribute(uint campaignID) public payable{
Campaign storage c = campaigns[campaignID];
c.funders[c.numFunders++] = Funder(msg.sender, msg.value);
c.amount += msg.value;
}
function checkGoalReached(uint campaignID) public returns (bool reached){
Campaign storage c = campaigns[campaignID];
if(c.amount < c.fundingGoal)
return false;
uint amount = c.amount;
c.amount = 0;
c.beneficiary.transfer(amount);
return true;
}
}
总结:
模拟众筹