Skip to content

Instantly share code, notes, and snippets.

@Shritesh99
Created February 26, 2019 16:40
Show Gist options
  • Save Shritesh99/385edf0e462d0f0629f13762e0aa09ae to your computer and use it in GitHub Desktop.
Save Shritesh99/385edf0e462d0f0629f13762e0aa09ae to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.5.1+commit.c8a2cb62.js&optimize=false&gist=
pragma solidity >=0.4.22 <0.6.0;
// Solidity Version 0.4.22 <0.6.0
// 0xbc848c44a72D878aA935CccFe3e307c4e2DB0146 head
// 0x3184b6541c82fAa56B9d2360E2953d89f1Db8d82 Chairperson
// Election contract
contract Elections{
///////////////////////////////////////////////////////////////////////////////////////
// Party Struct
struct Party{
uint id;
bool exists;
}
////////////////////////////////////////////////////////////////////////////////////////
// Global variables
address superChairPerson;
address[] chairpersons;
address[] campaigns;
uint[] partyIDs;
mapping(uint => Party) parties;
/////////////////////////////////////////////////////////////////////////////////////////
// Modifieres
modifier restricted(){
require(msg.sender == superChairPerson,"Sender not authorized.");
_;
}
modifier checkParty(uint id){
require(parties[id].exists,"Party does not exists.");
_;
}
////////////////////////////////////////////////////////////////////////////////////////
// Constructor
constructor(address superChairpersonAddress) public {
superChairPerson = superChairpersonAddress;
}
//////////////////////////////////////////////////////////////////////////////////////////////
// SuperChairperson Queries (geter and setter)
function getSuperChairperson() public view returns(address) {
return superChairPerson;
}
function setSuperChairperson(address superChairPersonNew)public restricted{
superChairPerson = superChairPersonNew;
}
//////////////////////////////////////////////////////////////////////////////////////////////
// Chairperson Queries (geter and setter)
function getChairpersons() public view restricted returns(address[] memory) {
return chairpersons;
}
function updateChairPerson(address old,address newAdd)public restricted{
chairpersons.push(newAdd);
removeChairperson(old);
}
function removeChairperson(address chairpersonAddress)public restricted{
for (uint j =0;j<chairpersons.length;j++){
if(chairpersons[j] == chairpersonAddress){
for (uint i = j; i<chairpersons.length-1; i++){
chairpersons[i] = chairpersons[i+1];
}
delete chairpersons[chairpersons.length-1];
chairpersons.length--;
break;
}
}
}
////////////////////////////////////////////////////////////////////////////////////////
// Party Queries
function addParty(uint id) public restricted{
parties[id] = Party(id,true);
partyIDs.push(id);
}
function updateParty(uint id, uint newID)public restricted checkParty(id){
parties[newID] = parties[id];
removeParty(id);
}
function getParties()public view restricted returns(uint[] memory){
return (partyIDs);
}
function removeParty(uint id) public restricted checkParty(id){
for (uint j =0;j<partyIDs.length;j++){
if(partyIDs[j] == id){
for (uint i = j; i<partyIDs.length-1; i++){
partyIDs[i] = partyIDs[i+1];
}
delete partyIDs[partyIDs.length-1];
partyIDs.length--;
break;
}
}
delete parties[id];
}
function getNumOfParties() public view restricted returns(uint) {
return partyIDs.length;
}
function getPartyVotesByCampaign(uint id,address campaignAddr)public view restricted checkParty(id)returns(uint){
ElectionCampaign campaign = ElectionCampaign(campaignAddr);
return (campaign.getPartyVotes(id));
}
function getPartyVotes(uint id)public restricted view checkParty(id) returns(uint){
uint votes;
for(uint i = 0; i<campaigns.length; i++){
votes += getPartyVotesByCampaign(id, campaigns[i]);
}
return votes;
}
function getPartyCandidatesByCampaign(uint id,address campaignAddr)public view restricted checkParty(id) returns(uint){
ElectionCampaign campaign = ElectionCampaign(campaignAddr);
return (campaign.getPartyCandidate(id));
}
function getPartyCandidatesCount(uint id)public view restricted checkParty(id) returns(uint){
uint count;
for(uint i = 0; i<campaigns.length; i++){
if(ElectionCampaign(campaigns[i]).getPartyCandidate(id) != 0)
count ++;
}
return (count);
}
function getPartyCandidates(uint id)public view restricted checkParty(id) returns(uint[] memory){
uint size = getPartyCandidatesCount(id);
uint[] memory ar = new uint[](size);
uint count = 0;
for(uint i = 0;i<campaigns.length;i++){
if(ElectionCampaign(campaigns[i]).getPartyCandidate(id) != 0){
ar[count] = ElectionCampaign(campaigns[i]).getPartyCandidate(id);
count++;
}
}
return ar;
}
////////////////////////////////////////////////////////////////////////////////////////
// Campaign Queries
function crateElectionCampaign(address chairperson) public restricted returns(address){
chairpersons.push(chairperson);
ElectionCampaign campaign = new ElectionCampaign(chairperson,superChairPerson);
campaigns.push(address(campaign));
return (address(campaign));
}
function getElectionCampaigns() public restricted view returns(address[] memory){
return (campaigns);
}
function removeElectionCampaigns(address adr) public restricted{
for (uint j =0;j<campaigns.length;j++){
if(campaigns[j] == adr){
for (uint i = j; i<campaigns.length-1; i++){
campaigns[i] = campaigns[i+1];
}
delete campaigns[campaigns.length-1];
campaigns.length--;
break;
}
}
}
}
////////////////////////////////////////////////////////////////////////////////////////
// Election Campaign contract
contract ElectionCampaign{
///////////////////////////////////////////////////////////////////////////////////////
// Candidate Stuct
struct Candidate{
uint id;
uint partyID;
uint votes;
bool exist;
}
////////////////////////////////////////////////////////////////////////////////////////
//Voter Stuct
struct Voter {
uint id;
uint toCandidate;
bool voted;
bool exist;
}
////////////////////////////////////////////////////////////////////////////////////////
// Hashmap Stuct
struct PartyMapping {
uint id;
bool exist;
}
///////////////////////////////////////////////////////////////////////////////////////////
// Global variables
address chairperson;
address superChairperson;
uint votersCount;
uint[] candidateIDs;
mapping (uint => Candidate)public candidates;
mapping (uint => Voter)public voters;
mapping (uint => PartyMapping)public partyCandidates;
///////////////////////////////////////////////////////////////////////////////////////////
// Modifieres
modifier restricted(){
require(msg.sender == chairperson || msg.sender == superChairperson,"Sender not authorized.");
_;
}
modifier checkCandidate(uint candidateID){
require(candidates[candidateID].exist,"Candidate does not exist.");
_;
}
modifier checkVoted(uint id){
require(voters[id].voted,"Voter does not voted");
_;
}
modifier checkVoterExist(uint id){
require(voters[id].exist,"Voter exists");
_;
}
modifier checkNotVoterExist(uint id){
require(voters[id].exist,"Voter exists");
_;
}
////////////////////////////////////////////////////////////////////////////////////////////
// Constructor
constructor(address chairpersonAddress, address superChairpersonAddress) public {
chairperson = chairpersonAddress;
superChairperson = superChairpersonAddress;
}
//////////////////////////////////////////////////////////////////////////////////////////////
// SuperChairperson Queries
function getSuperChairperson() public view returns(address){
return superChairperson;
}
//////////////////////////////////////////////////////////////////////////////////////////////
// Chairperson Queries
function getChairperson() public view returns(address) {
return chairperson;
}
function setChairperson(address chairpersonNew)public restricted{
chairperson = chairpersonNew;
}
/////////////////////////////////////////////////////////////////////////////////////////////
// Candidate Queries
function addCandidate(uint id, uint partyID) public restricted{
if(!candidates[id].exist){
candidates[id] = Candidate(id,partyID,0,true);
candidateIDs.push(id);
partyCandidates[partyID] = PartyMapping(id,true);
}
}
function updateCandidate(uint id, uint newID) public restricted checkCandidate(id){
candidates[newID] = candidates[id];
candidateIDs.push(newID);
removeCandidate(id);
}
function getCandidates() public view restricted returns(uint[] memory){
return (candidateIDs);
}
function removeCandidate(uint id) public restricted checkCandidate(id){
for (uint j =0;j<candidateIDs.length;j++){
if(candidateIDs[j] == id){
for (uint i = j; i<candidateIDs.length-1; i++){
candidateIDs[i] = candidateIDs[i+1];
}
delete candidateIDs[candidateIDs.length-1];
candidateIDs.length--;
break;
}
delete candidates[id];
}
}
function getCandidateParty(uint id)public view restricted checkCandidate(id) returns(uint){
return candidates[id].partyID;
}
function updateCandidateParty(uint id,uint partyID)public restricted checkCandidate(id){
candidates[id].partyID = partyID;
}
function getNumOfCandidates() public view restricted returns(uint) {
return candidateIDs.length;
}
function getCandidateVotes(uint id) public view restricted checkCandidate(id) returns(uint){
return (candidates[id].votes);
}
///////////////////////////////////////////////////////////////////////////////////////////////
// Party Queries
function getPartyVotes(uint partyID)public view restricted returns(uint){
if(partyCandidates[partyID].exist){
if(candidates[partyCandidates[partyID].id].exist){
return (candidates[partyCandidates[partyID].id].votes);
}else return 0;
}else return 0;
}
function getPartyCandidate(uint partyID)public view restricted returns(uint){
if(partyCandidates[partyID].exist){
return partyCandidates[partyID].id;
}else{
return 0;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////
// Voter Queries
function getNumOfVoters() public view restricted returns(uint) {
return votersCount;
}
function getVote(uint id)public view checkVoted(id) returns(uint){
return (voters[id].toCandidate);
}
function registerVoter(uint id) public checkNotVoterExist(id){
voters[id] = Voter(id,0,false,true);
}
function unRegisterVoter(uint id) public checkNotVoterExist(id){
voters[id].exist = false;
}
////////////////////////////////////////////////////////////////////////////////////////////////
// Actual Vote Function
function vote(uint id, uint candidateID) public restricted checkCandidate(candidateID){
require(!voters[id].voted,"Voter already voted");
voters[id].voted = true;
voters[id].toCandidate = candidateID;
candidates[candidateID].votes++;
votersCount++;
}
}
// Solidity Version
pragma solidity >=0.4.26;
contract Medicines{
// Global Variables
address admin;
Medicine[] medicines;
uint[] ids;
// Modifiers
modifier checkAdmin(){
require(msg.sender == admin,"You are not authorized.");
_;
}
// Constructor
constructor() public{
admin = msg.sender;
}
// Admin Functions
function getSuperAdmin() public view returns(address){
return admin;
}
function setSuperAdmin(address superAdmin1) public checkAdmin{
admin = superAdmin1;
}
// Medicine Functions
function addMedicine(uint id, uint q1, address sgAdmin1, address bhAdmin1, address phcAdmin1, address shcAdmin1, address anaAdmin1)public{
medicines.push(new Medicine(id,q1,sgAdmin1,bhAdmin1,phcAdmin1,shcAdmin1,anaAdmin1));
ids.push(id);
}
function getMedicines()public view returns(uint[] memory){
return ids;
}
function getMedicineAddress()public view returns(Medicine[] memory){
return medicines;
}
}
contract Medicine{
// Events
event Quantity(uint);
// Global Variables
address anaAdmin;
address hmAdmin;
address shcAdmin;
address phcAdmin;
address sgAdmin;
address bhAdmin;
uint status;
uint id;
uint q;
// Constructor
constructor(uint id1, uint q1, address sgAdmin1, address bhAdmin1, address phcAdmin1, address shcAdmin1, address anaAdmin1) public{
hmAdmin = msg.sender;
id = id1;
q=q1;
sgAdmin=sgAdmin1;
bhAdmin=bhAdmin1;
phcAdmin=phcAdmin1;
shcAdmin=shcAdmin1;
anaAdmin=anaAdmin1;
status = 1;
}
// Admin Functions
function getSuperAdmin() public view returns(address){
return hmAdmin;
}
// Status Functions
function getStatus()public view returns(uint){
return status;
}
// SG Functions
function approveSG()public {
require(msg.sender == sgAdmin,"You are not Authorized");
require(status==1,"Already approved");
status=2;
}
// BH Functions
function approveBH()public {
require(msg.sender == bhAdmin,"You are not Authorized");
require(status==2,"Already approved");
status=3;
}
// PHC Functions
function approvePHC()public {
require(msg.sender == phcAdmin,"You are not Authorized");
require(status==3,"Already approved");
status=4;
}
// SHC Functions
function approveSHC()public {
require(msg.sender == shcAdmin,"You are not Authorized");
require(status==4,"Already approved");
status=5;
}
// ANA Functions
function approveANA(uint q1)public {
require(msg.sender == anaAdmin,"You are not Authorized");
require(status==5,"Already approved");
emit Quantity(q1);
}
}
// Solidity Version
pragma solidity >=0.4.26;
// Contract dOrgan
contract dOrgan{
// Events
event donorDead(address);
// Structs
struct Reciever{
address patient;
bool exist;
bool verify;
uint priority;
}
struct Donor{
address donor;
bool approve;
bool exist;
bool verify;
bool live;
uint kidnies;
}
struct Transplant{
address reciever;
address donor;
bool exist;
}
// Global Variables
address admin;
address[] recieverArr;
address[] liveDonorArr;
address[] deadDonorArr;
address[] transplantArr;
address[] waitlistArr;
// Mappings
mapping(address => Reciever) recievers;
mapping(address => Donor) liveDonors;
mapping(address => Donor) deadDonors;
mapping(address => Reciever) waitlist;
mapping(address => Transplant) transplants;
// Modifiers
modifier verifyAdmin(){
require(admin == msg.sender,"Your are not authorized");
_;
}
modifier checkTransplantedExist(address addr){
require(!transplants[addr].exist,"Already done Transplant");
_;
}
modifier checkVerifyReciever(address addr){
require(!recievers[addr].verify,"Not verified by Admin");
_;
}
modifier checkVerifyLiveDonor(address addr){
require(!liveDonors[addr].verify,"Not verified by Admin");
_;
}
modifier checkVerifyDeadDonor(address addr){
require(!deadDonors[addr].verify,"Not verified by Admin");
_;
}
modifier checkRecieverExist(address addr){
require(!recievers[addr].exist,"Already added reciever");
_;
}
modifier checkLiveDonorExist(address addr){
require(!liveDonors[addr].exist,"Already added donor");
_;
}
modifier checkDeadDonorExist(address addr){
require(!deadDonors[addr].exist,"Already added donor");
_;
}
modifier verifyTransplantExist(address addr){
require(transplants[addr].exist,"Transplant not exist");
_;
}
modifier verifyRecieverExist(address addr){
require(recievers[addr].exist,"Reciever not exist");
_;
}
modifier verifyDeadDonorExist(address addr){
require(deadDonors[addr].exist,"Donor not exist");
_;
}
modifier verifyLiveDonorExist(address addr){
require(liveDonors[addr].exist,"Donor not exist");
_;
}
modifier approveLiveDonor(address liveDonor1){
require(liveDonors[liveDonor1].approve,"Donor didn't approved.");
_;
}
modifier approveDeadDonor(address deadDonor1){
require(deadDonors[deadDonor1].approve,"Relative of donor didn't approved.");
_;
}
// Constructor
constructor() public{
admin = msg.sender;
}
//Admin Getters & Setters
function getAdmin() public view returns(address){
return admin;
}
function setAdmin(address admin1) public verifyAdmin{
admin = admin1;
}
/*
Suppose a Patient detected by ckd stage 5 then, it requires immediate Transplant
so, he has two ways, first to go for the Live Donor(i.e. Relatives) or wait in the
waiting list (i.e. when some one is dead) to get someone's Kidney.
*/
// Patient functions
function addReciever(address addr, uint priority1)public checkRecieverExist(addr){
recievers[addr] = Reciever(addr,true,false,priority1);
recieverArr.push(addr);
}
function getRecievers() public view returns(address[] memory){
return recieverArr;
}
function removeReciever(address addr) public verifyRecieverExist(addr){
for(uint i = 0; i<recieverArr.length;i++){
if(recieverArr[i] == addr) delete recieverArr[i];
}
delete recievers[addr];
}
function updateReciever(address oldAddr, address newAddr) public verifyRecieverExist(oldAddr){
addReciever(newAddr, recievers[oldAddr].priority);
removeReciever(oldAddr);
}
function addRecieverToWaitlist(address addr)public verifyRecieverExist(addr){
waitlistArr.push(addr);
waitlist[addr] = recievers[addr];
}
function getWaitlist() public view returns(address[] memory){
return waitlistArr;
}
function removeRecieverFromWaitlist(address addr)public verifyRecieverExist(addr){
for(uint i = 0; i<waitlistArr.length;i++){
if(waitlistArr[i] == addr) delete waitlistArr[i];
}
delete waitlist[addr];
}
function getPriority(address addr)public view verifyAdmin verifyRecieverExist(addr) returns(uint){
return recievers[addr].priority;
}
function changePriority(address addr, uint priority1)public verifyAdmin verifyRecieverExist(addr){
recievers[addr].priority = priority1;
}
function verifyRecieverByAdmin(address addr)public verifyAdmin verifyRecieverExist(addr){
recievers[addr].verify = true;
}
function getVerificationRecieverByAdmin(address addr)public verifyRecieverExist(addr){
recievers[addr].verify = true;
}
/*
Suppose that patient opt for transplantation from a living donor(i.e. Relatives)
then the donor(person) has to approve that its Kidney is being transplanted.
*/
// Live Donor Functions
function addLiveDonor(address addr) public checkLiveDonorExist(addr){
liveDonors[addr] = Donor(addr, false,true,false,true,1);
liveDonorArr.push(addr);
}
function getLiveDonors() public view returns(address[] memory){
return liveDonorArr;
}
function removeLiveDonor(address addr) public verifyLiveDonorExist(addr){
for(uint i = 0; i<liveDonorArr.length;i++){
if(liveDonorArr[i] == addr) delete liveDonorArr[i];
}
delete liveDonors[addr];
}
function updateLiveDonor(address oldAddr, address newAddr) public verifyLiveDonorExist(oldAddr){
addLiveDonor(newAddr);
removeLiveDonor(oldAddr);
}
function approveLiveTransplant(address addr)public verifyLiveDonorExist(addr){
liveDonors[addr].approve = true;
}
function removeLiveTransplantApproval(address addr)public verifyLiveDonorExist(addr) approveLiveDonor(addr){
liveDonors[addr].approve = false;
}
function verifyLiveDonorByAdmin(address addr)public verifyAdmin verifyLiveDonorExist(addr){
liveDonors[addr].verify = true;
}
function getVerificationLiveDonorByAdmin(address addr)public verifyLiveDonorExist(addr){
liveDonors[addr].verify = true;
}
/*
Suppose that patient opt for transplantation from a dead donor(i.e. a person who has died)
then the donor's relatives has to approve that its kidneies are being transplanted.
*/
// Dead Donor Functions
function addDeadDonor(address addr) public checkDeadDonorExist(addr){
deadDonors[addr] = Donor(addr, false,true,false,false,2);
deadDonorArr.push(addr);
}
function getDeadDonors() public view returns(address[] memory){
return deadDonorArr;
}
function removeDeadDonor(address addr) public verifyDeadDonorExist(addr){
for(uint i = 0; i<deadDonorArr.length;i++){
if(deadDonorArr[i] == addr) delete deadDonorArr[i];
}
delete deadDonors[addr];
}
function updateDeadDonor(address oldAddr, address newAddr) public verifyDeadDonorExist(oldAddr){
removeDeadDonor(oldAddr);
addDeadDonor(newAddr);
}
/*
If approved then his kidneies go to the nemerous people in demmand of kidney in waitlist.
*/
function approveDeadTransplant(address addr)public verifyDeadDonorExist(addr){
deadDonors[addr].approve = true;
emit donorDead(addr); // ML Predictions Here
}
function removeDeadTransplantApproval(address addr)public verifyDeadDonorExist(addr) approveDeadDonor(addr){
deadDonors[addr].approve = false;
}
function verifyDeadDonorByAdmin(address addr)public verifyAdmin verifyDeadDonorExist(addr){
deadDonors[addr].verify = true;
}
function getVerificationDeadDonorByAdmin(address addr)public verifyDeadDonorExist(addr){
deadDonors[addr].verify = true;
}
function getKidnies(address addr)public view verifyDeadDonorExist(addr) returns(uint){
return deadDonors[addr].kidnies;
}
/*
Final Transplant.
*/
// Transplant functions
function transplantLiveDonor(address reciever1,address donor1) public checkTransplantedExist(reciever1) verifyAdmin verifyRecieverExist(reciever1) verifyLiveDonorExist(donor1) approveLiveDonor(donor1) checkVerifyReciever(reciever1) checkVerifyLiveDonor(donor1) {
transplants[reciever1] = Transplant(reciever1,donor1,true);
transplantArr.push(reciever1);
removeReciever(reciever1);
removeLiveDonor(donor1);
delete liveDonors[donor1];
}
function transplantDeadDonor(address reciever1,address donor1) public checkTransplantedExist(reciever1) verifyAdmin verifyRecieverExist(reciever1) verifyDeadDonorExist(donor1) approveDeadDonor(donor1) checkVerifyReciever(reciever1) checkVerifyDeadDonor(donor1) {
if(deadDonors[donor1].kidnies > 0){
transplants[reciever1] = Transplant(reciever1,donor1,true);
deadDonors[donor1].kidnies --;
transplantArr.push(reciever1);
removeReciever(reciever1);
removeLiveDonor(donor1);
removeRecieverFromWaitlist(reciever1);
delete waitlist[reciever1];
}
if(deadDonors[donor1].kidnies == 0){
delete deadDonors[donor1];
}
}
function getTransplant(address reciever1)public view returns(address){
return transplants[reciever1].donor;
}
function getTransplants() public view returns(address[] memory){
return transplantArr;
}
}
pragma solidity >=0.4.26;
contract dEmergency {
address private creator;
struct Hospital{
string location;
string mobileNumber;
uint hospitalCapacity;
uint hospitalAvailability;
uint speciality;
}
mapping(address => Hospital) HospitalInfo;
mapping(string => address) HospitalLoc;
mapping(address => address) PatientHospital;
event InformHospital(address hospital, string location, string mobileNumber);
event InformPatient(address hospital,string location, string mobileNumber);
constructor() public payable {
creator = msg.sender;
}
function addHospital(address _hospital,string memory _location,string memory _mobileNumber,uint _hospitalCapacity,uint _hospitalAvailability,uint _speciality) public {
require(msg.sender == creator);
require(HospitalInfo[_hospital].hospitalCapacity ==0);
HospitalInfo[_hospital] = Hospital(_location, _mobileNumber, _hospitalCapacity, _hospitalAvailability,_speciality);
HospitalLoc[_location]=_hospital;
}
function addPatient(address _patient,string memory location,string memory _mobileNumber,uint _speciality,string memory name) public returns(bool) {
address _hospital = HospitalLoc[location];
require(HospitalInfo[_hospital].hospitalCapacity != 0);
if(HospitalInfo[_hospital].hospitalAvailability > 0)
{
HospitalInfo[_hospital].hospitalAvailability -= 1;
PatientHospital[_patient]=_hospital;
//events triggered,for dagger
emit InformPatient(_hospital,HospitalInfo[_hospital].location, HospitalInfo[_hospital].mobileNumber);
emit InformHospital(_hospital, location, _mobileNumber);
return true;
}
else
{
return false;
}
}
function discharge(address _hospital,address _patient) public {
require(msg.sender == creator);
HospitalInfo[_hospital].hospitalAvailability += 1;
PatientHospital[_patient]=address(0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment