Created
May 17, 2022 09:45
-
-
Save samuel-esp/0ff717025b4e70ab236bc30bd2f36f47 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=
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.8.13; | |
//dvx | |
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