Solidity 从入门到实战(完结)
pragma solidity ^0.4.0;
//完成了从创建众筹事件、捐赠、提款的全部操作
contract zhongchou{
//捐赠者的对象struct funder{
address funderaddress;//捐赠者的地址uint Tomoney; //捐赠者捐赠的金钱}//受益人的对象struct needer{
address Needaddress;//受益人的地址uint goal;//受益人的目标值uint amount;//当前已经募集到多少钱uint funderAccount;//捐赠者的idmapping(uint => funder) map; //映射,将捐赠者的id与捐赠者绑定在一起}uint neederAmount; //受益人的id号,作为受益人的唯一标识mapping(uint => needer) needmap;//通过mapping将受益人的id与受益人绑定在一起//实现一个众筹的事件function NewNeeder(address _Needaddress,uint _goal){
neederAmount++;needmap[neederAmount] = needer(_Needaddress,_goal,0,0);}//address _adderss:捐赠者的地址// uint _neederAmount: 受益人的idfunction contribute(address _adderss, uint _neederAmount) payable{
needer storage _needer = needmap[_neederAmount];//聚集到的资金增加_needer.amount +=msg.value;//捐赠人数增加_needer.funderAccount++;//将捐赠人的id与shouyi _needer.map[_needer.funderAccount] = funder(_adderss, msg.value);}//_neederAmount:收益人的idfunction IScompelete(uint _neederAmount){
//获取受益人对象needer storage _needer = needmap[_neederAmount];//如果受益人金额》受益人的目标金额if(_needer.amount >= _needer.goal){
//转账_needer.Needaddress.transfer(_neederAmount);}}function test() view returns(uint,uint,uint){
return (needmap[1].goal,needmap[1].amount,needmap[1].funderAccount);}
}
创建众筹事件
捐赠
提款