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

Posted eilinge

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ethereum(以太坊)--集资(构造函数/映射)相关的知识,希望对你有一定的参考价值。

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(以太坊)--集资(构造函数/映射)的主要内容,如果未能解决你的问题,请参考以下文章

(转)写文章 以太坊(Ethereum)创世揭秘 以太坊(Ethereum)创世揭秘

区块链,使用 Go-Ethereum 搭建以太坊私有链

使用 Go-Ethereum 1.7.2搭建以太坊私有链

支付用户的gas:以太坊

以太坊是什么?|猿创征文

[中文] 以太坊(Ethereum )白皮书