Skip to content

Instantly share code, notes, and snippets.

@samuel-esp
Created May 16, 2022 10:39
Show Gist options
  • Save samuel-esp/4197bb92c38b78ecb65ae146dab7d097 to your computer and use it in GitHub Desktop.
Save samuel-esp/4197bb92c38b78ecb65ae146dab7d097 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.8.13+commit.abaa5c0e.js&optimize=false&runs=200&gist=
pragma solidity ^0.8.13;
contract AssetTracker{
struct State{
string description;
address currentOwner;
}
struct Product{
address manufacturer;
string productName;
string productId;
uint256 date;
uint256 totalStates;
mapping (uint256 => State) states;
}
mapping (string => Product) allProducts;
function newItem(string memory _productId, string memory _name, uint _date) public returns (bool){
Product storage createdItem = allProducts[_productId];
createdItem.manufacturer = msg.sender;
createdItem.productName = _name;
createdItem.productId = _productId;
createdItem.date = _date;
createdItem.totalStates = 0;
return true;
}
function addState(string memory _productId, string memory info) public returns (string memory){
State memory newState = State({currentOwner: msg.sender, description: info});
uint256 currentState = allProducts[_productId].totalStates;
allProducts[_productId].states[currentState] = newState;
currentState = currentState + 1;
allProducts[_productId].totalStates = currentState;
return info;
}
function searchProduct(string memory _productId) public view returns (address manufacturer, string memory productName,
string memory productId, uint256 date, uint256 totalStates, State[] memory descriptionsArray){
Product storage searchedProduct = allProducts[_productId];
State[] memory statesArray = new State[](searchedProduct.totalStates);
for (uint256 i=0; i<searchedProduct.totalStates; i++){
statesArray[i] = searchedProduct.states[i];
}
return (searchedProduct.manufacturer, searchedProduct.productName,
searchedProduct.productId, searchedProduct.date, searchedProduct.totalStates, statesArray);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment