solidity智能合约[43]-众筹案例

综合案例:众筹

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
pragma solidity ^0.4.23;

contract campaign{
//捐赠者的对象
struct funder{
address funderaddress; //捐赠者的地址
uint Tomoney; //捐赠者捐赠的金钱
}

//受益人的对象
struct needer{
address Neederaddress; //受益人的地址
uint goal;//受益人的目标值
uint amount; //当前的已经募集到了多少钱
uint funderAcoount; //捐赠者的id
mapping(uint => funder) map; //映射,将捐赠者的id与捐赠者绑定在一起,从而能够得知,是谁给当前的受益人捐钱了。
}

uint neederAmount; //受益人的id数
mapping(uint=> needer) needmap; //通过mapping将受益人的id与受益人绑定在额一起,从而能够能耗的管理受益人

//实现一个众筹的事件
function NewNeeder(address _Neederaddress, uint _goal ){
//将受益人id与受益人绑定
neederAmount++;
needmap[neederAmount] = needer(_Neederaddress,_goal,0,0);
}

//@param _address 捐赠者的地址 ,
//@param _neederAmount 受益人的id
function contribute( address _address, uint _neederAmount) payable{
//通过id获取到受益人对象
needer storage _needer = needmap[_neederAmount];
//聚集到的资金增加
_needer.amount += msg.value;
// 捐赠人数增加
_needer.funderAcoount++;
//将受益人id与受益人绑定
_needer.map[ _needer.funderAcoount] = funder(_address , msg.value );
}

//当募集到的资金满足条件,就会给给受益人的地址转账
//@param _neederAmount 受益人的id
function ISconpelete( uint _neederAmount){
needer storage _needer = needmap[_neederAmount];
if(_needer.amount >=_needer.goal ){
_needer.Neederaddress.transfer(_needer.amount);
}
}

function test() view returns(uint,uint,uint){
return (needmap[1].goal, needmap[1].amount,needmap[1].funderAcoount);
}
}