Skip to content

Instantly share code, notes, and snippets.

@utkarsh-aryan
Created March 26, 2023 17:24
Show Gist options
  • Save utkarsh-aryan/000e0ccb4ff9228c67769222f44bd1d5 to your computer and use it in GitHub Desktop.
Save utkarsh-aryan/000e0ccb4ff9228c67769222f44bd1d5 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.6.0+commit.26b70077.js&optimize=false&runs=200&gist=
pragma solidity ^0.6.0;
contract SupplyChain {
event Added(uint256 index);
struct State{
string description;
address person;
}
struct Product{
address creator;
string productName;
uint256 productId;
string date;
uint256 totalStates;
mapping (uint256 => State) positions;
}
mapping(uint => Product) allProducts;
uint256 items=0;
function concat(string memory _a, string memory _b) public returns (string memory){
bytes memory bytes_a = bytes(_a);
bytes memory bytes_b = bytes(_b);
string memory length_ab = new string(bytes_a.length + bytes_b.length);
bytes memory bytes_c = bytes(length_ab);
uint k = 0;
for (uint i = 0; i < bytes_a.length; i++) bytes_c[k++] = bytes_a[i];
for (uint i = 0; i < bytes_b.length; i++) bytes_c[k++] = bytes_b[i];
return string(bytes_c);
}
function newItem(string memory _text, string memory _date) public returns (bool) {
Product memory newItem = Product({creator: msg.sender, totalStates: 0,productName: _text, productId: items, date: _date});
allProducts[items]=newItem;
items = items+1;
emit Added(items-1);
return true;
}
function addState(uint _productId, string memory info) public returns (string memory) {
require(_productId<=items);
State memory newState = State({person: msg.sender, description: info});
allProducts[_productId].positions[ allProducts[_productId].totalStates ]=newState;
allProducts[_productId].totalStates = allProducts[_productId].totalStates +1;
return info;
}
function searchProduct(uint _productId) public returns (string memory) {
require(_productId<=items);
string memory output="Product Name: ";
output=concat(output, allProducts[_productId].productName);
output=concat(output, "<br>Manufacture Date: ");
output=concat(output, allProducts[_productId].date);
for (uint256 j=0; j<allProducts[_productId].totalStates; j++){
output=concat(output, allProducts[_productId].positions[j].description);
}
return output;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment