Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimized #2

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions contracts/base.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ contract base is permission{
owner = msg.sender;
isOwner[msg.sender] = true;
}


}
59 changes: 36 additions & 23 deletions contracts/details.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,24 @@ contract details is enroll{

// set of functions revolving aroung patient's record

struct institutions{
address instiAddress;
bool status;
}
struct medicalReport{
string cid;
uint256 consultationCnt;
uint256 confirmationCnt;
mapping (address => bool)confirmedBy;
mapping (address => uint256)refferedTo; // 0 - initial state , 1 - reffered by doctor , 2 - approved by patient
}

struct patientRecord{
string personalInfoHash;
mapping(uint256=>medicalReport) medicalInfoHash;
institutions[] permitted;
address[] permitted;
uint256 credit;
uint256 totalRecord;
}

mapping (address => patientRecord) public record;
mapping (address => bool) public confirmedBy;
// mapping (address => bool) public confirmedBy;


// patient sets their personal info
Expand All @@ -43,14 +41,24 @@ contract details is enroll{
function getPersonalInfoHash(string memory _id)
public
view
isAllowed(worksAt[msg.sender], _id)
returns(string memory)
// isAllowed(_id)
returns(string memory x)
{
address _address = Id[_id];
require(isPatient[_address], "not a patient" );
return record[_address].personalInfoHash;
require(isPatient[_address] && isAllowed(_id), "not a patient" );
x = record[_address].personalInfoHash;
return x;
}
//
function getMedRecordCnt(string memory _id)
public
view
returns(uint256 x){
address _address = Id[_id];
require(isPatient[_address] && isAllowed(_id),"not a patient's id");
x = record[_address].totalRecord;
return x;
}


// anyone can see patient's credit score
function getCreditScore(string memory _id) public view returns(uint256){
Expand All @@ -66,6 +74,7 @@ contract details is enroll{
uint256 total = record[msg.sender].medicalInfoHash[_reportId].consultationCnt;
return total;
}

// total number of doctors who agreed with the medical report
function totalConfirmation(uint256 _reportId)public view returns(uint256){
require(isPatient[msg.sender],"not a patient" );
Expand All @@ -74,21 +83,25 @@ contract details is enroll{
}

// checks if the sender works at a insti approved by the patient
modifier isAllowed(address _address , string memory _id ){

function isAllowed(string memory _id )public view returns(bool){
address _patientAddress = Id[_id];
require(isHospital[_address] || isLab[_address] , "not allowed");
bool ok = false;
for(uint256 i=0; i < record[_patientAddress].permitted.length; i++){
address instiAddress = record[_patientAddress].permitted[i].instiAddress;
bool _status = record[_patientAddress].permitted[i].status;
if(instiAddress == _address && _status){
ok = true;
break;
require(isPatient[_patientAddress],"not a patient's id");
if(msg.sender == _patientAddress){
require(msg.sender == _patientAddress,"try your id");
return true;
}else{
require(isDoctor[msg.sender] || isTech[msg.sender], "not a worker");
address _address = worksAt[msg.sender];
require(isHospital[_address] || isLab[_address] , "not allowed");
for(uint256 i=0; i < record[_patientAddress].permitted.length; i++){
address instiAddress = record[_patientAddress].permitted[i];
if(instiAddress == _address){
return true;
}
}

}
require(ok,"not allowed");
_;
return false;

}

Expand Down
48 changes: 4 additions & 44 deletions contracts/enroll.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8;

contract enroll{
import "./helper.sol";
contract enroll is helper{

// set of functions for the enrollment of participants in the network

Expand All @@ -26,16 +26,7 @@ contract enroll{
require(isOwner[msg.sender],"sender is not owner");
_;
}

modifier onlyHospital(){
require(isHospital[msg.sender],"sender is not the hospital admin");
_;
}

modifier onlyLab(){
require(isLab[msg.sender],"sender is not lab admin");
_;
}

modifier onlyPatient(){
require(isPatient[msg.sender],"sender is not patient");
_;
Expand All @@ -61,38 +52,7 @@ contract enroll{

}

function compareStrings(string memory a, string memory b) public pure returns (bool) {
return (keccak256(abi.encodePacked((a))) == keccak256(abi.encodePacked((b))));
}

// function to convert integers to strings
function uint2str( uint256 _i)internal pure returns (string memory str)
{
if (_i == 0)
{
return "0";
}
uint256 j = _i;
uint256 length;
while (j != 0)
{
length++;
j /= 10;
}
bytes memory bstr = new bytes(length);
uint256 k = length;
j = _i;
while (j != 0)
{
bstr[--k] = bytes1(uint8(48 + j % 10));
j /= 10;
}
str = string(bstr);
}

function concat(string memory a,string memory b) public pure returns (string memory){
return string(abi.encodePacked(a,b));
}



// owner add the hosptials , lab and patients to the network
Expand Down
38 changes: 38 additions & 0 deletions contracts/helper.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8;

contract helper{

function compareStrings(string memory a, string memory b) public pure returns (bool) {
return (keccak256(abi.encodePacked((a))) == keccak256(abi.encodePacked((b))));
}

// function to convert integers to strings
function uint2str( uint256 _i)internal pure returns (string memory str)
{
if (_i == 0)
{
return "0";
}
uint256 j = _i;
uint256 length;
while (j != 0)
{
length++;
j /= 10;
}
bytes memory bstr = new bytes(length);
uint256 k = length;
j = _i;
while (j != 0)
{
bstr[--k] = bytes1(uint8(48 + j % 10));
j /= 10;
}
str = string(bstr);
}

function concat(string memory a,string memory b) public pure returns (string memory){
return string(abi.encodePacked(a,b));
}
}
73 changes: 46 additions & 27 deletions contracts/permission.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,67 @@ import "./details.sol";
contract permission is details{

// patient adds or removes the insti who can update their medical report
function appprove(string memory _id) public onlyPatient {
address _address = Id[_id];
require(isHospital[_address] || isLab[_address], "not a valid address");

for (uint256 i = 0; i < record[msg.sender].permitted.length; i++) {
if (record[msg.sender].permitted[i].instiAddress == _address) {
require(
record[msg.sender].permitted[i].status == false,
"already approved"
);
}
}

institutions memory tmp;
// address instiAddre = Id[_address];
tmp.instiAddress = _address;
tmp.status = true;
// labList[msg.sender].push(tmp);
record[msg.sender].permitted.push(tmp);
record[msg.sender].credit++;
}
// patient remove the approved hospitals and labs
function remove(string memory _id)
// patient adds or removes the insti who can update their medical report
function modifyStatus(string memory _id,string memory status)
public
onlyPatient
onlyPatient
{
address _address = Id[_id];
// checks weather the entered id of lab or hospital
require(isLab[_address] || isHospital[_address],"Not a valid address");
bool ok = false;
// confirms if the given address is of a hospital or a lab
require(isLab[_address] || isHospital[_address] ,"Not a valid address");
bool bad = false;
for(uint256 i=0; i<record[msg.sender].permitted.length; i++){
if(record[msg.sender].permitted[i].instiAddress == _address){
if(record[msg.sender].permitted[i] == _address && compareStrings(status,"approve")){
bad = true;
break;
}

if(record[msg.sender].permitted[i] == _address && compareStrings(status,"remove")){
uint256 n = record[msg.sender].permitted.length;
record[msg.sender].permitted[i] = record[msg.sender].permitted[n-1];
record[msg.sender].credit--;
record[msg.sender].permitted.pop();
ok = true;
break;
}
}
if(!bad && compareStrings(status,"approve")){
record[msg.sender].permitted.push(_address);
record[msg.sender].credit++;
ok = true;
}
require(ok, "invalid request");


}
function reffer(
string memory _id,
string memory _pId,
uint256 rId
)public
onlyDoctor
{
address _patientAddress = Id[_pId];
address _docAddress = Id[_id];
require(isPatient[_patientAddress] && isDoctor[_docAddress], "not valid params");
require(isAllowed(_pId),"not allowed");
record[_patientAddress].medicalInfoHash[rId].refferedTo[_docAddress] = 1;
}

function approveRefferedDoctor(
string memory _id,
uint256 rId
)public
onlyPatient
{
address _docAddress = Id[_id];
require(isDoctor[_docAddress], "not a doctor");
require(record[msg.sender].medicalInfoHash[rId].refferedTo[_docAddress] == 1, "not a valid request");
record[msg.sender].medicalInfoHash[rId].refferedTo[_docAddress] = 2;
}




}
40 changes: 13 additions & 27 deletions contracts/updateReport.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,18 @@ contract updateReport is details{
// lab technician at approved lab can add a patient's med info
function setMedInfoHash(
string memory _mHash,
string memory _id

string memory _id
)public
isAllowed(worksAt[msg.sender],_id)
{
address _patientAddress = Id[_id];

require(isPatient[_patientAddress], "not a patient" );
require(isTech[msg.sender],"not a labTech");
require(isAllowed(_id) && isTech[msg.sender],"not a labTech");

uint256 last = record[_patientAddress].totalRecord;
last += 1;
record[_patientAddress].medicalInfoHash[last].cid = _mHash;
record[_patientAddress].medicalInfoHash[last].consultationCnt = 0;
record[_patientAddress].medicalInfoHash[last].confirmationCnt = 0;
record[_patientAddress].totalRecord += last;
}
// patient or doctors at approved insti can see a patient medical info
function getMedInfoHash(string memory _id,uint256 _reportId)
Expand All @@ -32,42 +29,31 @@ contract updateReport is details{
address _patientAddress = Id[_id];
require(isPatient[_patientAddress], "not a patient" );
require(isHospital[worksAt[msg.sender]] || (isPatient[msg.sender] && msg.sender == _patientAddress) , "not allowed");
bool ok = false;
if(_patientAddress == msg.sender){
ok = true;
}else{
for(uint256 i=0; i < record[_patientAddress].permitted.length; i++){
address instiAddress = record[_patientAddress].permitted[i].instiAddress;
bool _status = record[_patientAddress].permitted[i].status;
if(instiAddress == worksAt[msg.sender] && _status){
ok = true;
break;
}

}
}
require(ok,"not a valid request");
require(isAllowed(_id),"not a valid request");
return record[_patientAddress].medicalInfoHash[_reportId].cid;
}

// doctors at approved hospitals can confirm the disease status
function confirmDisease(
string memory _id,
uint256 _reportId,
string memory status)
bool status)
public
isAllowed(worksAt[msg.sender],_id)
onlyDoctor
{
address _patientAddress = Id[_id];
require(isAllowed(_id) ||
record[_patientAddress].medicalInfoHash[_reportId].refferedTo[msg.sender] == 2,"not allowed");
require(isPatient[_patientAddress], "not a patient" );
require(isDoctor[msg.sender],"not a doctor");
require(!confirmedBy[msg.sender] , "have confirmed already");
require(!record[_patientAddress].medicalInfoHash[_reportId].confirmedBy[msg.sender] , "have confirmed already");
require(record[_patientAddress].totalRecord >= _reportId,"invalid report Id");
confirmedBy[msg.sender] = true;
record[_patientAddress].medicalInfoHash[_reportId].confirmedBy[msg.sender] = true;
record[_patientAddress].medicalInfoHash[_reportId].consultationCnt++;
if(compareStrings(status ,"true")){
if(status){
record[_patientAddress].medicalInfoHash[_reportId].confirmationCnt++;
}

}


}
Loading