-
Notifications
You must be signed in to change notification settings - Fork 0
/
Adoption.sol
129 lines (95 loc) · 3.41 KB
/
Adoption.sol
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Personnal Project, For APS1050 2022 Winter Term
// New Function 1: Adoption Records
// New Function 2: Cancel Adoption
// New Function 3: Give a "like" to a particular dog
// New Function 4: Donate to Pet Shop
// New Function 5: Display donor and amount information
pragma solidity ^0.5.0;
contract Adoption {
address[16] public adopters;
// Adopting a pet
uint public pet_Id;
// mapping(uint => uint) public likemap;
function adopt(uint petId) public returns (uint) {
require(petId >= 0 && petId <= 15);
adopters[petId] = msg.sender;
return petId;
}
// Retrieving the adopters
function getAdopters() public view returns (address[16] memory) {
return adopters;
}
function() external payable {
}
// New Function 1: Adoption Records
event LoglistAdoption(uint _num, address _contributer);
function getListAdoption() public {
for(uint i=0; i<16; i++){
emit LoglistAdoption(i, adopters[i]);
}
}
//////////////////////////////////////////////////////////////////////
// New Function 2: Cancel Adoption
function cancelAdoption() public returns (bool){
for(uint i=0; i<16; i++){
if (msg.sender == adopters[i]) {
adopters[i] = 0x0000000000000000000000000000000000000000;
return true;
}
}
return false;
}
//////////////////////////////////////////////////////////////////////
// New Function 3: Give a "like" to a particular dog
uint [16] public like;
// Record users' likes for dogs
function likeIncrement(uint _petId) public{
++like[_petId];
}
// test function of like
function test_likeIncremen() public{
likeIncrement(1);
}
//////////////////////////////////////////////////////////////////////
// New Function 4: Donate to Pet Shop
uint public totalMoney;
uint public numofcontributer;
//address public owner = msg.sender;
// address owner = this;
address payable AddrPetShop = 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4;
struct contributerToPetShop{
address addrToShop;
uint amountToShop;
}
mapping(uint => contributerToPetShop) public contributerList;
// Donate to Pet Shop
function contributeToPetShop() public payable returns (bool) {
if( msg.value == 0 ) // Avoid invalid transactions
return false;
numofcontributer++;
totalMoney += msg.value;
contributerList[numofcontributer] = contributerToPetShop(msg.sender, msg.value);
return true;
}
// Transferring the amount in the contract account to the Pet Shop account
function transferToPetShop()public payable returns (bool) {
if(totalMoney == 0)
return false;
AddrPetShop.transfer(totalMoney);
totalMoney = 0;
return true;
}
// Get the address and balance of the blockchain where the current contract is located
// function getConstractAddressBalance() public returns(address ConstractAddress, uint ConstractBalance){
// return (this,this.balance);
// }
//////////////////////////////////////////////////////////////////////
// New Function 5: Display donor and amount information
event LoglistContribute(uint _num, address _contributer, uint _amountToShop);
function getListContribute() public {
for(uint i=1; i<=numofcontributer; i++){
emit LoglistContribute(i, contributerList[i].addrToShop, contributerList[i].amountToShop);
}
}
//////////////////////////////////////////////////////////////////////
}