-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathOrganChain.sol
197 lines (174 loc) · 5.54 KB
/
OrganChain.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
pragma solidity >=0.4.22 <0.6.0;
contract OrganChain {
//structures
struct recipient{
address patientid;
address hospitalid;
string ipfshash;
string EMRhash;
uint priority;
bytes32 organ;
bytes32 bloodgroup;
bytes32 rhfactor;
bool matchfound;
bool exist;
}
struct donor{
address donorid;
string ipfshash;
string EMRhash;
bytes32 organ;
bytes32 bloodgroup;
bytes32 rhfactor;
bool matchfound;
bool exist;
}
struct transplant{
address recipient;
address donor;
bool exist;
}
//global variables
address[] recipientarr;
address[] donorarr;
address[] transplantarr;
//Mappings
mapping(address => recipient) Recipients;
mapping(address => donor) Donors;
mapping(address => transplant) Transplants;
mapping(address => address[]) hospital_recipient;
//modifier
modifier checkrecipientexist(address addr){
require(!Recipients[addr].exist,"recipient already added");
_;
}
modifier checkdonorexist(address addr){
require(!Donors[addr].exist,"recipient already added");
_;
}
//donor functions
function adddonor(
address donori,
string memory hash,
string memory hash2,
bytes32 organ_name,
bytes32 bgroup,
bytes32 factor) public checkdonorexist(donori){
Donors[donori]=donor(donori,hash,hash2,organ_name,bgroup,factor,false,true);
donorarr.push(donori);
}
function getdonor(address donoradd) public view
returns (address,
string memory,
bytes32,
bytes32,
bytes32){
if(!Donors[donoradd].matchfound)
return(
Donors[donoradd].donorid,
Donors[donoradd].ipfshash,
Donors[donoradd].organ,
Donors[donoradd].bloodgroup,
Donors[donoradd].rhfactor);
else
getdonorwithtransplant(donoradd);
}
function getdonorwithtransplant(address donoradd) public view
returns (address,
string memory,
bytes32,
bytes32,
bytes32,
address) {
for(uint i=0;i<transplantarr.length;i++)
{
if(donoradd==Transplants[transplantarr[i]].donor)
return(Donors[donoradd].donorid,
Donors[donoradd].ipfshash,
Donors[donoradd].organ,
Donors[donoradd].bloodgroup,
Donors[donoradd].rhfactor,
Transplants[transplantarr[i]].recipient);
}
}
//recipient functions
function addrecipient(
address hospi,
address patient,
string memory hash,
string memory hash2,
uint _priority,
bytes32 organ_name,
bytes32 bgroup,
bytes32 factor) public checkrecipientexist(patient){
Recipients[patient]=recipient(patient,hospi,hash,hash2,_priority,organ_name,bgroup,factor,false,true);
recipientarr.push(patient);
hospital_recipient[hospi].push(patient);
}
function getrecipient(address reciadd) public view
returns(address,
address,
string memory,
bytes32,
bytes32,
bytes32){
return(
Recipients[reciadd].patientid,
Recipients[reciadd].hospitalid,
Recipients[reciadd].ipfshash,
Recipients[reciadd].organ,
Recipients[reciadd].bloodgroup,
Recipients[reciadd].rhfactor);
}
function getrecipientcount(address hospiadd) public view returns(uint256)
{
return(hospital_recipient[hospiadd].length);
}
function getrecipientdetail(address hospiaddr, uint256 m) public view
returns(address,
string memory,
uint,
bytes32,
bytes32
){
if(!Recipients[hospital_recipient[hospiaddr][m]].matchfound)
{
return(
Recipients[hospital_recipient[hospiaddr][m]].patientid,
Recipients[hospital_recipient[hospiaddr][m]].ipfshash,
Recipients[hospital_recipient[hospiaddr][m]].priority,
Recipients[hospital_recipient[hospiaddr][m]].organ,
Recipients[hospital_recipient[hospiaddr][m]].bloodgroup);
}
}
//transplant matching
function transplantmatch(address recipientad) public
returns(address) {
for(uint i=0;i<donorarr.length;i++)
{
if( (Recipients[recipientad].organ==Donors[donorarr[i]].organ)
&& (Recipients[recipientad].bloodgroup==Donors[donorarr[i]].bloodgroup)
&& (Recipients[recipientad].rhfactor==Donors[donorarr[i]].rhfactor) )
{ Transplants[recipientad]=transplant(recipientad,donorarr[i],true);
transplantarr.push(recipientad);
Recipients[recipientad].matchfound=true;
Donors[donorarr[i]].matchfound=true;
return (donorarr[i]);
}
}
}
//patient record
function EMR(address patientaddr) public view
returns (string memory) {
for(uint i=0;i<donorarr.length;i++)
{
if(patientaddr==donorarr[i])
return(Donors[donorarr[i]].EMRhash);
}
for(uint j=0;j<recipientarr.length;j++)
{
if(patientaddr==recipientarr[j])
return(Recipients[recipientarr[j]].EMRhash);
}
}
}