# Towards an Optimized Blockchain-Based Secure Medical Prescription-Management System
## 3 Blockchain-Based Prescription Management DApp Overview
### Main Process of Blockchain-Based Prescription-Management System
```
Input: walletAddr, name, email, phone, city; Output: Emit an event declaring that the new pharmacist has been registered newUserId; Data:  walletAddr: the Ethereum address of the pharmacist name: the name of the pharmacist email: the email of the pharmacist phone: the phone number of the pharmacist city: the city where the pharmacy is located newUserId: a new ID for the new registered pharmacist Initialization:  userlds: a map that associates Ethereum addresses with integers (userlds) users: table of all registered pharmacists ifadmin == msg.senderthen ifwalletAddr does not existthen  Associate the user wallet address with a new ID Store the new pharmacist details Generate a password, and send it to the pharmacist's email  end if else  Revert contract state, and show an error (the pharmacist is not registered yet)  end if  end if else  error: Only admin can initialize  end if
```  
**Algorithm 1**adPharmacist \begin{table}
\begin{tabular}{l l} \hline \hline
**Function** & **Description** \\ \hline addPharmacist() & register pharmacist in the platform \\ \hline addPatient() & register patient in the platform \\ \hline addMADrug() & register approved drug in the market-authorized drug list \\ \hline addPresc() & register new prescription \\ \hline updatePresc() & update an already registered prescription \\ \hline getPresc() & check if a prescription is already registered and retrieve its details \\ \hline getUser() & obtain user information \\ \hline getPresc() & consult specific prescription information if registered \\ \hline \hline \end{tabular}
\end{table} 
Table 1: Examples of functions in the smart contract. ```
Input: patientWalletAddr, patientName, patientAge, patientCity, patientMail, patientPhone; Output: Emit an event declaring that the new patient has been registered; ifpatientWalletAddr does not existthen  Store the new patient details  Generate QR code, and send it to patient's email  end if else: Revert contract state, and show an error;
```  
**Algorithm 2**addPatient  
```
1. addPresc0 Input:presRef, PatientName, DrName, DrCity, TotalDrugs, drugsDetails[drugRef, totalQuantity, servedQuantity], issueDate Output:Emit an event declaring that a new prescription has been registered  Specific QR code generated  Store prescription's details iftotalQuantity=servedQuantityor ithas been 3 months since the issueDatethen  Lock the prescription; 2. getPresc0 Input:prescRef Output:Return details of prescription  Calculate remaining quantity  remainingQuantity \(\leftarrow\) totalQuantity - servedQuantity; 3. getDrug0 Input:prescRef, drugRef Output:Return details of drug 4. updatePresc0 Input:prescRef, drugRef, newServedQuantity ifPrescriptionis unlocked,andnewServedQuantity\(\leq\)remainingQuantitythen  Store the new served quantity;  Update remaining quantity; ifremainingQuantity=0 or ithas been 3 months since the issueDatethen  Lock the prescription;  end if else  Revert contract state, and show an error;  end
```  
**Algorithm 3**Implemented functionalities