Created
January 5, 2022 11:32
-
-
Save alancpazetto/dd98b5536249f17991bfe70091adf162 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.7+commit.e28d00a7.js&optimize=false&runs=200&gist=
This file contains hidden or 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
// SPDX-License-Identifier: GPL-3.0 | |
pragma solidity >=0.7.0 <0.9.0; | |
contract CatalogItem { | |
string public name; | |
uint256 public price; | |
constructor(string memory _name, uint256 _price) { | |
name = _name; | |
price = _price; | |
} | |
} | |
contract Restaurant { | |
string public name; | |
CatalogItem[] public catalogItems; | |
constructor(string memory _name) { | |
name = _name; | |
} | |
function addCatalogItem(string memory _name, uint256 _price) public returns(CatalogItem) { | |
CatalogItem _item = new CatalogItem(_name, _price); | |
catalogItems.push(_item); | |
return _item; | |
} | |
} | |
contract Consumer { | |
string name; | |
} | |
contract Order { | |
Restaurant public restaurant; | |
Consumer public consumer; | |
uint256 public total; | |
constructor(uint256 _total) { | |
total = _total; | |
} | |
} | |
contract Account { | |
uint8 public accountType; | |
Order[] public orders; | |
mapping(address => Order) public accountOrders; | |
constructor(uint8 _accountType) { | |
accountType = _accountType; | |
} | |
function getType() private view returns(uint8) { | |
return accountType; | |
} | |
function isRestaurant() public view returns(bool) { | |
return getType() == 1; | |
} | |
function isConsumer() public view returns(bool) { | |
return getType() == 2; | |
} | |
function addOrder(Order _order) public { | |
address orderAddress = address(_order); | |
require(address(accountOrders[orderAddress]) == address(0x0), "Order already exists"); | |
accountOrders[orderAddress] = _order; | |
orders.push(_order); | |
} | |
function getOrders() public view returns(Order[] memory) { | |
return orders; | |
} | |
} | |
/** | |
* @title FoodToken | |
* @dev TODO | |
*/ | |
contract FoodChain { | |
// struct Restaurant { | |
// string name; | |
// } | |
// struct Consumer { | |
// string name; | |
// } | |
// struct CatalogItem { | |
// string name; | |
// uint price; | |
// } | |
mapping(address => Account) public chainAccounts; | |
function createAcount(uint8 _accountType) public { | |
address sender = msg.sender; | |
require(address(chainAccounts[sender]) == address(0x0), "Account already created"); | |
Account newAccount = new Account(_accountType); | |
chainAccounts[sender] = newAccount; | |
} | |
function getAccountType() public view returns(uint8) { | |
return chainAccounts[msg.sender].accountType(); | |
} | |
function transact(address _consumer, address _restaurant, uint256 _total) public { | |
Order newOrder = new Order(_total); | |
Account consumer = chainAccounts[_consumer]; | |
Account restaurant = chainAccounts[_restaurant]; | |
consumer.addOrder(newOrder); | |
restaurant.addOrder(newOrder); | |
} | |
function getMyOrders() public view returns(Order[] memory) { | |
address sender = msg.sender; | |
return chainAccounts[sender].getOrders(); | |
} | |
// // Restaurant[] public restaurantsArray; | |
// mapping(address => Restaurant) public restaurantsArray; | |
// function createRestaurant(string memory _name) public returns(address) { | |
// Restaurant restaurant = new Restaurant(_name); | |
// restaurantsArray[address(restaurant)] = restaurant; | |
// return address(restaurant); | |
// } | |
// function getRestaurant(address _restAddress) public view returns(Restaurant) { | |
// return Restaurant(address(restaurantsArray[_restAddress])); | |
// } | |
// function createItemToRestaurant(address _restAddress, string memory _name, uint256 _price) public returns(address) { | |
// Restaurant restaurant = getRestaurant(_restAddress); | |
// return address(restaurant.addCatalogItem(_name, _price)); | |
// } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment