L-C-J
1/5/2019 - 12:50 PM

Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.

Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.5.2+commit.1df8f40c.js&optimize=false&gist=

// 声明solidity版本为0.4.24
pragma solidity >=0.4.22 <0.6.0;
// 创建合约CrowFunding
contract CrowFunding {
//定义众筹赞助方结构体
    struct Investor{
        address payable addr; //赞助地址
        uint count ;  //赞助数量
    }
//定义众筹发起方结构体 
    struct BySponsor{
        address payable addr;       //接收地址
        uint goalCount;     //众筹金额
        uint receiveCount;  //已经众筹到的金额
        uint investorNum;   //众筹次数
        mapping (uint =>Investor) investors; //用于保存多个众筹赞助方信息
    }

    uint bySponsorNum = 0 ;  //定义众筹发起方的ID号
    mapping (uint=>BySponsor) bySponsors; //定义映射用于保存多个合作发起方信息

//定义函数,用于生成众筹发起方对象
    function newBySponsor() payable public {
        // 将合约发起方ID号增加1
        bySponsorNum++;
        /* 创建合约发起方对象
        众筹地址为:当前Accout地址
        众筹金额为:通过Value值设置
        */
        BySponsor memory bySponsor = BySponsor(msg.sender,msg.value,0,0);
        //将该众筹对象保存至映射bySponsors中
        bySponsors[bySponsorNum] = bySponsor;

    }
 /*定义函数,用于查看创建的众筹金额    
 传入参数: bySponsorId   众筹发起方ID
 返回值:   goalCount    创建的众筹金额 
 */
    function getGoalCount(uint bySponsorId) view public returns (uint) {
         // 通过bySponsorId从映射bySponsors中取出对应的bySponsor对象
        BySponsor memory bySponsor = bySponsors[bySponsorId];
        //返回bySponsor对象的goalCount属性值
        return bySponsor.goalCount;
    }

 //定义函数,用于实现众筹赞助方的赞助功能,传入参数为众筹发起方ID号,并定义函数类型为payable
    function sponsor(uint bySponsorId)payable public {
        // 通过bySponsorId从映射bySponsors中取出对应的bySponsor对象
        BySponsor storage bySponsor = bySponsors[bySponsorId];
        //设置合约代码可执行条件是赞助的金额必须>0
        require(msg.value >0);
        // 将赞助金额加入到bySponsor对象的receiveCount属性中
        bySponsor.receiveCount += msg.value;
        //众筹次数累加
        bySponsor.investorNum++;
        //将本次众筹赞助方信息保存至映射investors中
        bySponsor.investors[bySponsor.investorNum] = Investor(msg.sender,msg.value);
        //实现转账,从当前地址(众筹赞助方地址)转入bySponsor(众筹接收方地址),金额为Value中定义的值
        bySponsor.addr.transfer(msg.value);
    }

 //定义函数,用于获取已经众筹到的金额,传入参数为众筹发起方ID号  
    function getReceiveCount(uint bySponsorId) view public returns (uint){
         // 通过bySponsorId从映射bySponsors中取出对应的bySponsor对象
        BySponsor memory bySponsor = bySponsors[bySponsorId];
        //返回bySponsor对象的receiveCount属性值
        return bySponsor.receiveCount;
    }    
//定义函数,用于检查是否众筹金额是否达标,传入参数为众筹发起方ID号 
    function checkComplete(uint bySponsorId) view public returns (bool){
         // 通过bySponsorId从映射bySponsors中取出对应的bySponsor对象
        BySponsor memory bySponsor = bySponsors[bySponsorId];
        // 判断设定的众筹金额是否有效(不为0),并且已经众筹到的金额是否大于等于创建的众筹金额
        if (bySponsor.receiveCount >= bySponsor.goalCount && bySponsor.goalCount >0){
            return true;
        }else {
            return false;
        }
    }
}
pragma solidity >=0.4.22 <0.6.0;
contract Ballot {

    struct Voter {
        uint weight;
        bool voted;
        uint8 vote;
        address delegate;
    }
    struct Proposal {
        uint voteCount;
    }

    address chairperson;
    mapping(address => Voter) voters;
    Proposal[] proposals;

    /// Create a new ballot with $(_numProposals) different proposals.
    constructor(uint8 _numProposals) public {
        chairperson = msg.sender;
        voters[chairperson].weight = 1;
        proposals.length = _numProposals;
    }

    /// Give $(toVoter) the right to vote on this ballot.
    /// May only be called by $(chairperson).
    function giveRightToVote(address toVoter) public {
        if (msg.sender != chairperson || voters[toVoter].voted) return;
        voters[toVoter].weight = 1;
    }

    /// Delegate your vote to the voter $(to).
    function delegate(address to) public {
        Voter storage sender = voters[msg.sender]; // assigns reference
        if (sender.voted) return;
        while (voters[to].delegate != address(0) && voters[to].delegate != msg.sender)
            to = voters[to].delegate;
        if (to == msg.sender) return;
        sender.voted = true;
        sender.delegate = to;
        Voter storage delegateTo = voters[to];
        if (delegateTo.voted)
            proposals[delegateTo.vote].voteCount += sender.weight;
        else
            delegateTo.weight += sender.weight;
    }

    /// Give a single vote to proposal $(toProposal).
    function vote(uint8 toProposal) public {
        Voter storage sender = voters[msg.sender];
        if (sender.voted || toProposal >= proposals.length) return;
        sender.voted = true;
        sender.vote = toProposal;
        proposals[toProposal].voteCount += sender.weight;
    }

    function winningProposal() public view returns (uint8 _winningProposal) {
        uint256 winningVoteCount = 0;
        for (uint8 prop = 0; prop < proposals.length; prop++)
            if (proposals[prop].voteCount > winningVoteCount) {
                winningVoteCount = proposals[prop].voteCount;
                _winningProposal = prop;
            }
    }
}

import "remix_tests.sol"; // this import is automatically injected by Remix.
import "./ballot.sol";

contract test3 {
   
    Ballot ballotToTest;
    function beforeAll () public {
       ballotToTest = new Ballot(2);
    }
    
    function checkWinningProposal () public {
        ballotToTest.vote(1);
        Assert.equal(ballotToTest.winningProposal(), uint(1), "1 should be the winning proposal");
    }
    
    function checkWinninProposalWithReturnValue () public view returns (bool) {
        return ballotToTest.winningProposal() == 1;
    }
}