I have a solidity contract with two struct and a mapping table,the question is how I can get data of the struct Parts from hash value?
Here's the code that I am using:
struct Part{
address manufacturer;
string serial_number;
string part_type;
string creation_date;
}
struct Product{
address manufacturer;
string serial_number;
string product_type;
string creation_date;
bytes32[] parts;
}
mapping(bytes32 => Part) public parts;
mapping(bytes32 => Product) public products;
function getParts(bytes32 product_hash) public returns (bytes32[6] memory) {}
}
contract ChangeOwnership {
enum OperationType {PART, PRODUCT}
mapping(bytes32 => address) public currentPartOwner;
mapping(bytes32 => address) public currentProductOwner;
event TransferPartOwnership(bytes32 indexed p, address indexed account);
event TransferProductOwnership(bytes32 indexed p, address indexed account);
ProductManagement private pm;
constructor(address prod_contract_addr) public {
//contratto pm ausiliario, lo uso per verificare se la part(bottiglia) o il product(cassa/spedizione) esiste
pm = ProductManagement(prod_contract_addr);
}
//funzione per aggiungere la proprietà di una part(bottiglia) o prodotto(cassa/spedizione) una volta che sono state prodotte
//successivamente troviamo la funzione per il cambio di propietà
function addOwnership(uint op_type, bytes32 p_hash) public returns (bool) {
if(op_type == uint(OperationType.PART)){
address manufacturer;
(manufacturer, , , ) = pm.parts(p_hash);
require(currentPartOwner[p_hash] == address(0), "Part was already registered");
require(manufacturer == msg.sender, "Part was not made by requester");
currentPartOwner[p_hash] = msg.sender;
//emit si utilizza in solidity per emettere l'evento
emit TransferPartOwnership(p_hash, msg.sender);
} else if (op_type == uint(OperationType.PRODUCT)){
address manufacturer;
(manufacturer, , , ) = pm.products(p_hash);
require(currentProductOwner[p_hash] == address(0), "Product was already registered");
require(manufacturer == msg.sender, "Product was not made by requester");
currentProductOwner[p_hash] = msg.sender;
emit TransferProductOwnership(p_hash, msg.sender);
}
}
function changeOwnership(uint op_type, bytes32 p_hash, address to) public returns (bool) {
//comtrollo se l'elemento esiste ed appartiene all utente che ha chiesto il change ownership
if(op_type == uint(OperationType.PART)){
require(currentPartOwner[p_hash] == msg.sender, "Part is not owned by requester");
currentPartOwner[p_hash] = to;
emit TransferPartOwnership(p_hash, to);
} else if (op_type == uint(OperationType.PRODUCT)){
require(currentProductOwner[p_hash] == msg.sender, "Product is not owned by requester");
currentProductOwner[p_hash] = to;
emit TransferProductOwnership(p_hash, to);
//Change part ownership too
bytes32[6] memory part_list = pm.getParts(p_hash);
for(uint i = 0; i < part_list.length; i++){
currentPartOwner[part_list[i]] = to;
emit TransferPartOwnership(part_list[i], to);
}
}
}
}