首页 > 其他 > 详细

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

时间:2018-11-19 18:29:23      阅读:34      评论:0      收藏:0      [点我收藏+]

标签: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(以太坊)(十二)--集资(构造函数/映射)

标签:ppi   col   --   msg   n)   状态   ide   mount   pre   

原文:https://www.cnblogs.com/eilinge/p/9984497.html

(0)
(0)
   
举报
评论 一句话评论(0
0条  
登录后才能评论!
© 2014 bubuko.com 版权所有 鲁ICP备09046678号-4
打开技术之扣,分享程序人生!
             

鲁公网安备 37021202000002号