-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPharmacy_v2.sol
70 lines (61 loc) · 2.44 KB
/
Pharmacy_v2.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
pragma solidity >=0.4.22 <0.9.0;
contract Pharmacy {
// Structure to define the drug properties
struct Drug {
uint id;
string name;
uint quantity;
address owner;
uint price; // Add price property to the Drug struct
}
// Mapping to store the drugs
mapping(uint => Drug) public drugs;
uint public drugsCount;
// Events to emit drug creation and transfer
event DrugCreated(uint id, string name, uint quantity, address owner, uint price); // Add price parameter to DrugCreated event
event DrugTransferred(uint id, address from, address to);
// Function to create a drug
function createDrug(string memory _name, uint _quantity, address _owner, uint _price) public {
drugsCount ++;
drugs[drugsCount] = Drug(drugsCount, _name, _quantity, _owner, _price); // Add price parameter to Drug struct initialization
emit DrugCreated(drugsCount, _name, _quantity, _owner, _price); // Add price parameter to emit statement
}
// Function to transfer a drug
function transferDrug(uint _id, address _to) public {
Drug memory _drug = drugs[_id];
address _from = _drug.owner;
require(_from == msg.sender, "You are not the owner of this drug");
require(_to != address(0), "Invalid address");
_drug.owner = _to;
drugs[_id] = _drug;
emit DrugTransferred(_id, _from, _to);
}
// Function to set the price of a drug
function setDrugPrice(uint _id, uint _price) public {
Drug memory _drug = drugs[_id];
address _owner = _drug.owner;
require(_owner == msg.sender, "You are not the owner of this drug");
_drug.price = _price;
drugs[_id] = _drug;
}
// Function to get the price of a drug
function getDrugPrice(uint _id) public view returns (uint) {
return drugs[_id].price;
}
// Function to get the list of drugs owned by an address
function getDrugsByOwner(address _owner) public view returns (uint[] memory) {
uint[] memory result = new uint[](drugsCount);
uint counter = 0;
for (uint i = 1; i <= drugsCount; i++) {
if (drugs[i].owner == _owner) {
result[counter] = i;
counter ++;
}
}
uint[] memory ownedDrugs = new uint[](counter);
for (uint j = 0; j < counter; j++) {
ownedDrugs[j] = result[j];
}
return ownedDrugs;
}
}