ethereum(以太坊)(十二)--应用(一)__集资(构造函数/映射)

Posted on 2018-11-19 18:03  eilinge  阅读(302)  评论(0编辑  收藏  举报
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.受益人可以相同
2.struct+mapping:根据索引存储大量结构体