javascript
// SPDX-License-Identifier: MIT
pragma solidity >=0.4.11 <0.9.0;
contract CrowdFunding {
// 定义的新类型包含两个属性。
struct Funder {
address addr;
uint amount;
}
struct Campaign {
address payable beneficiary;
uint fundingGoal;
uint numFunders;
uint amount;
mapping (uint => Funder) funders;
}
uint numCampaigns;
mapping (uint => Campaign) campaigns;
function newCampaign(address beneficiary, uint goal) public returns (uint ) {
uint campaignID = numCampaigns++;
Campaign storage c = campaigns[campaignID];
c.beneficiary=payable(beneficiary);
c.fundingGoal=goal;
c.amount=0;
c.numFunders=0;
return campaignID;
}
function contribute(uint campaignID) public payable {
Campaign storage c = campaigns[campaignID];
c.funders[c.numFunders++] = Funder({addr: msg.sender, amount: 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;
}
function info(uint campaignID) public view returns(uint){
Campaign storage c = campaigns[campaignID];
uint amount = c.amount;
return amount;
}
}