码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-11-19 20:21:00      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:campaign   ppi   ons   指定   efi   function   以太坊   UNC   状态   

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

标签:campaign   ppi   ons   指定   efi   function   以太坊   UNC   状态   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!