Skip to content

Instantly share code, notes, and snippets.

@nate-xyz
Last active April 23, 2022 01:58
Show Gist options
  • Select an option

  • Save nate-xyz/9c2a8b77cc29e7797ad99a712e04221a to your computer and use it in GitHub Desktop.

Select an option

Save nate-xyz/9c2a8b77cc29e7797ad99a712e04221a to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: MIT
// Only allow compiler versions from 0.7.0 to (not including) 0.9.0
pragma solidity >=0.8.0 <0.9.0;
contract Bakery {
// State variables
address public owner;
bool shopIsOpen;
string[] breadNames = ["arepaBread", "bagel", "baguette", "bananaBread", "brioche", "challah", "ciabatta", "cornbread", "damperBread", "englishMuffins", "focaccia", "grissini", "injera", "irishSoda", "knackebrod", "lavash", "matzah", "multigrain", "naan", "obiNonBread", "parathaBread", "pitaBread", "potatoBread", "pumpernickle", "qistibiBread", "rotiBread", "ryeBread", "sourdough", "tortilla", "vanocka", "yufka", "zopf"];
mapping(uint => mapping(address => uint)) breadTypeMap;
// On creation...
constructor () {
// Set the owner as the contract's deployer
owner = msg.sender;
// Set initial stock
for(uint i = 0; i < breadNames.length; i++) {
breadTypeMap[i][address(this)] = 1000;
}
// The shop is initially closed
shopIsOpen = true;
}
// Function to compare two strings
function compareStrings(string memory a, string memory b) private pure returns (bool) {
return (keccak256(abi.encodePacked((a))) == keccak256(abi.encodePacked(b)));
}
function findString(string[] memory arr, string memory searchFor) private pure returns (uint) {
for (uint i = 0; i < arr.length; i++) {
if (compareStrings(arr[i],searchFor)) {
return i;
}
}
revert("Not Found");
}
// Let the owner open and close the shop
function openOrCloseShop() public returns (string memory) {
require(msg.sender == owner, "Only the owner can open or close the store!");
if (shopIsOpen) {
shopIsOpen = false;
return "Shop is now closed.";
} else {
shopIsOpen = true;
return "Shop is now open.";
}
}
// Let the owner restock the shop
function restock(uint amount, string memory breadString) public {
// Only the owner can restock!
require(msg.sender == owner, "Only the owner can restock the machine!");
// Refill the stock based on the type passed in
uint index = findString(breadNames, breadString);
breadTypeMap[index][address(this)] += amount;
}
// Purchase bread from the shop
function purchase(uint amount, string memory breadString) public payable {
require(shopIsOpen == true, "The shop is closed and you may not buy bread.");
require(msg.value >= amount * 1 ether, "You must pay at least 1 ETH per bread basket!");
// Sell a bread basket based on type asked
uint index = findString(breadNames, breadString);
breadTypeMap[index][address(this)] += amount;
breadTypeMap[index][msg.sender] -= amount;
}
// Get stock of a specific type of bread
function getStock(string memory breadString) public view returns (uint) {
// Get stock of a bread based on the type passed in
uint index = findString(breadNames, breadString);
return breadTypeMap[index][address(this)];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment