以太坊solidity实例学习

//判断是否是管理员,然后存储被投资者信息
//投资者能对被投资者任意投资
//判断是否是管理员,然后存储被投资者信息
//投资者能对被投资者任意投资
pragma solidity ^0.4.5;

contract pcl {
    address _owner;
    address a1=0xCA35b7d915458EF540aDe6068dFe2F44E8fa733c;
    address a2=0x4B0897b0513fdC7C541B6d9D7E929C4e5364D2dB;
    address a3=0xdD870fA1b7C4700F2BD7f44238821C26f7392148;
    //0x14723A09ACff6D2A60DcdF7aA4AFf308FDDC160C
    
    function pcl () {
        _owner=msg.sender;
    }
    struct beitouzizhe{     //定义被投资者
        address name;
        uint xuyaoduoshaoqian;
        uint yijingduoshaoqian;
        mapping(address=>uint) wode_touziren;       //定义一个mapping用来保存谁投了我
    }
    mapping (address=>beitouzizhe)all_beitouzizhe;      //用一个mapping来保存所有的被投资者

    function touzi(address touzishui) payable public{
        beitouzizhe storage _t=all_beitouzizhe[touzishui];
        _t.yijingduoshaoqian+=msg.value;
        _t.wode_touziren[msg.sender]+=msg.value;
        _t.name.transfer(msg.value);
    }
    
    function tianjia(address _n,uint _how){
        if (_owner==msg.sender){
            all_beitouzizhe[_n]=beitouzizhe(_n,_how,0);
        }
    }
    
    function jiancha(address _n,address _o) constant returns (uint){
        return all_beitouzizhe[_n].wode_touziren[_o];
    }
}

下面是黎老师提供的标准合约,有点复杂。

pragma solidity ^0.4.4;
contract CrowdFunding {
	 struct Funder {
		 address addr;
		 uint amount; 
	 }
	 struct Campaign {
		 address beneficiary; 
		 uint fundingGoal;
		 uint numFunders; 
		 uint amount; 
		 mapping (uint => Funder) funders;
	 }
	 
	 uint numCampaigns;
	 mapping (uint => Campaign) campaigns; 
	 
	 function newCampaign(address beneficiary, uint goal) public returns (uint campaignID) {
		 campaignID = numCampaigns++; 
		 campaigns[campaignID] = Campaign(beneficiary, goal, 0, 0);
	}
	
	 function contribute(uint campaignID) public payable {
		 Campaign storage c = campaigns[campaignID];
		 c.funders[c.numFunders++] = Funder({addr: msg.sender, amount: msg.value}); 
		 c.amount += msg.value; 
		 c.beneficiary.transfer(msg.value);
	 }
	 
	 function checkGoalReached(uint campaignID) public returns (bool reached){
		 Campaign storage c = campaigns[campaignID];
		 if (c.amount < c.fundingGoal)
		 return false;
		 return true;
		 }
}

因为其中标准合约中投资人是新建了一个uint去管理,uint numFunders; 比如这里还可以查到有多少个投资人,但是个人觉得这些没必要,删减过后比较好理解。

你可能感兴趣的:(区块链学习-以太坊)