投资练习
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract CrowdFundingStorage{
struct Campaign {
address payable receiver;
uint numFunders;
uint fundingGoal;
uint totalAmount;
}
struct Funder{
address addr;
uint amount;
}
uint public numCompagins;
mapping (uint => Campaign) campanies;
mapping (uint => Funder[]) funders;
mapping (uint => mapping(address => bool)) public isPatticipate;
}
contract CrowdFunding is CrowdFundingStorage{
address immutable owner;
constructor (){
owner = msg.sender;
}
modifier judgeParticipate(uint campaignID){
require(isPatticipate[campaignID][msg.sender]==false);
_;
}
modifier judgeOwner(){
require(msg.sender == owner);
_;
}
function newCampaign(address payable receiver, uint goal) external returns(uint campaignID){
campaignID = numCompagins++;
Campaign storage c = campanies[campaignID];
c.receiver = receiver;
c.fundingGoal = goal;
}
function bid(uint campaignID) external payable judgeParticipate(campaignID){
Campaign storage c = campanies[campaignID];
c.totalAmount += msg.value;
c.numFunders += 1;
funders[campaignID].push(Funder({
addr: msg.sender,
amount: msg.value
}));
isPatticipate[campaignID][msg.sender] = true;
}
function withdraw(uint campaignID) external returns (bool){
Campaign storage c = campanies[campaignID];
if(c.totalAmount < c.fundingGoal){
return false;
}
uint amount = c.totalAmount;
c.totalAmount = 0;
c.receiver.transfer(amount);
return true;
}
}