ethereum(以太坊)(十二)--集資(構造函數/映射)
阿新 • • 發佈:2018-11-19
ppi col -- msg n) 狀態 ide mount pre
pragma solidity ^0.4.4; contract funder{ //0xca35b7d915458ef540ade6068dfe2f44e8fa733c //0x14723a09acff6d2a60dcdf7aa4aff308fddc160c :10 //0x583031d1113ad414f02576bd6afabfb302140225 :20 struct Funder{ address addr; uint amount; } struct Campaign{ address beneficiary; //受益人uint fundingGoal; //集資目標 uint numFunders; //出資人數 uint mount; //已集資數 mapping(uint => Funder) Funders; //按照索引存儲出資人信息 } uint numCampaigns; mapping(uint => Campaign) public Campaigns; //按照索引存儲每個受益人的狀態 //向指定受益人出資一定數量的錢 //function newCampaign(address beneficiary,uint goal) public returns(uint campaignID){//function newCampaign(address beneficiary,uint goal) public{ //DeclarationError: Undeclared identifier__campaignID function newCampaign(address beneficiary,uint goal) public{ //var campaignID = numCampaigns++; //Campaigns[campaignID] = Campaign(beneficiary,goal,0,0); Campaigns[numCampaigns]= Campaign(beneficiary,goal,0,0); numCampaigns ++; } function contribute(uint campaignID) public payable{ Campaign storage c = Campaigns[campaignID]; //每個受益人的狀態 c.Funders[c.numFunders++] = Funder({addr:msg.sender,amount:msg.value}); //Funder({addr:msg.sender,amount:msg.value}) 當前轉賬人的地址,和轉賬金額 //c.numFunders++ 統計向當前受益人轉賬的人數 // 0 => 轉賬地址 c.mount += msg.value; //當前受益人的總受益 c.beneficiary.transfer(msg.value);//向受益人的地址轉入ethtereum } function checkGoalReached(uint campaignID) public constant returns(bool reached){ Campaign storage c = Campaigns[campaignID]; if(c.mount < c.fundingGoal){ return false; } return true; } }
遇到的問題:
1.受益人可以相同
ethereum(以太坊)(十二)--集資(構造函數/映射)