|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.0; |
| 3 | + |
| 4 | +import "./IBeerBar.sol"; |
| 5 | +import "./BeerToken.sol"; |
| 6 | +import "./IERC223Recipient.sol"; |
| 7 | + |
| 8 | +import "@openzeppelin/contracts/access/AccessControl.sol"; |
| 9 | + |
| 10 | + |
| 11 | +//contract BeerBar is IBeerBar, <Token-Recipient> |
| 12 | +contract BeerBar is IBeerBar, AccessControl { |
| 13 | + BeerToken public beerTokenContract; |
| 14 | + bytes32 public constant OWNER = keccak256("OWNER"); |
| 15 | + bytes32 public constant BARKEEPER = keccak256("BARKEEPER"); |
| 16 | + bool public _barIsOpen; |
| 17 | + uint public _beerPrice; |
| 18 | + |
| 19 | + mapping(address => uint) orders; |
| 20 | + |
| 21 | + |
| 22 | + constructor () { |
| 23 | + _grantRole(OWNER, msg.sender); |
| 24 | + _grantRole(0x0000000000000000000000000000000000000000000000000000000000000000, msg.sender); |
| 25 | + } |
| 26 | + |
| 27 | + // ROLES |
| 28 | + // * Only owners can add new owners |
| 29 | + // * Owners can only renounce themselves again |
| 30 | + function isOwner(address account) external view returns (bool) { |
| 31 | + return hasRole(OWNER, account); |
| 32 | + } |
| 33 | + |
| 34 | + function addOwner(address account) external { |
| 35 | + require(hasRole(OWNER, msg.sender), "Only owners can add new owners"); |
| 36 | + _grantRole(OWNER, account); |
| 37 | + emit OwnerAdded(account); |
| 38 | + } |
| 39 | + |
| 40 | + function renounceOwner() external { |
| 41 | + require(hasRole(OWNER, msg.sender), "Owners can only renounce themselves again"); |
| 42 | + _revokeRole(OWNER, msg.sender); |
| 43 | + emit OwnerRemoved(msg.sender); |
| 44 | + } |
| 45 | + |
| 46 | + // * Barkeepers have to be set by owners |
| 47 | + // * Barkeepers can be revoked by owners |
| 48 | + // * Barkeepers can renounce themselves |
| 49 | + function isBarkeeper(address account) external view returns (bool) { |
| 50 | + return hasRole(BARKEEPER, account); |
| 51 | + } |
| 52 | + |
| 53 | + function addBarkeeper(address account) external { |
| 54 | + require(hasRole(OWNER, msg.sender), "Barkeepers have to be set by owners"); |
| 55 | + _grantRole(BARKEEPER, account); |
| 56 | + emit BarkeeperAdded(account); |
| 57 | + } |
| 58 | + |
| 59 | + function revokeBarkeeper(address account) external { |
| 60 | + require(hasRole(OWNER, msg.sender), "Barkeepers can be revoked by owners"); |
| 61 | + _revokeRole(BARKEEPER, msg.sender); |
| 62 | + emit BarkeeperRemoved(account); |
| 63 | + } |
| 64 | + |
| 65 | + function renounceBarkeeper() external { |
| 66 | + require(hasRole(BARKEEPER, msg.sender), "Barkeepers can renounce themselves"); |
| 67 | + _revokeRole(BARKEEPER, msg.sender); |
| 68 | + emit BarkeeperRemoved(msg.sender); |
| 69 | + } |
| 70 | + |
| 71 | + // * The bar uses its own tokens as local currency. |
| 72 | + // Connect the bar contract with the token contract. |
| 73 | + // * This can only be done by owners |
| 74 | + function setBeerTokenContractAddress(address addr) external { |
| 75 | + require(hasRole(OWNER, msg.sender), "This can only be done by owners"); |
| 76 | + beerTokenContract = BeerToken(addr); |
| 77 | + } |
| 78 | + // * Show which beer is served |
| 79 | + function beerTokenContractAddress() external view returns(address) { |
| 80 | + return address(beerTokenContract); |
| 81 | + } |
| 82 | + |
| 83 | + // * The bar has opening hours during which beer can be ordered and served |
| 84 | + // * The bar is opened and closed by barkeepers |
| 85 | + function openBar() external { |
| 86 | + require(hasRole(BARKEEPER, msg.sender), " The bar is opened and closed by barkeepers"); |
| 87 | + _barIsOpen = true; |
| 88 | + emit BarOpened(); |
| 89 | + } |
| 90 | + |
| 91 | + function closeBar() external virtual { |
| 92 | + require(hasRole(BARKEEPER, msg.sender), " The bar is opened and closed by barkeepers"); |
| 93 | + _barIsOpen = false; |
| 94 | + emit BarClosed(); |
| 95 | + } |
| 96 | + |
| 97 | + function barIsOpen() external view returns (bool) { |
| 98 | + return _barIsOpen; |
| 99 | + } |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | + // Both, supply and order, have to be implemented in a token fallback function |
| 104 | + function tokenReceived(address _from, uint _value, bytes memory _data) public virtual { |
| 105 | + require(msg.sender == address(beerTokenContract),"Wrong token"); |
| 106 | + if(keccak256(_data) == hex"b308cfbb7d2d38db3a215f9728501ac69445a6afbee328cdeae4e23db54b850a") { |
| 107 | + require(hasRole(OWNER, tx.origin) ,"Only the token owner can supply"); |
| 108 | + emit BeerSupplied(_from, _value); |
| 109 | + } else if(keccak256(_data) == hex"e8e77626586f73b955364c7b4bbf0bb7f7685ebd40e852b164633a4acbd3244c") { |
| 110 | + require(!hasRole(BARKEEPER, msg.sender) && !hasRole(OWNER, msg.sender), "Only customers can order"); |
| 111 | + require(_barIsOpen, "Bar is closed"); |
| 112 | + orders[_from] += _value; |
| 113 | + emit BeerOrdered(_from, _value); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + // * Beer that has been ordered will be served by barkeepers |
| 118 | + // * Beer can only be served while the bar is open |
| 119 | + // * Served beer has to be burned by the token contract |
| 120 | + function serveBeer(address customer, uint amount) external { |
| 121 | + require(_barIsOpen, " Beer can only be served while the bar is open"); |
| 122 | + require(hasRole(BARKEEPER, msg.sender), " Only barkeepers can serve beer"); |
| 123 | + require(orders[customer] >= amount, " The customer did not order that much!"); |
| 124 | + orders[customer] -= amount; |
| 125 | + beerTokenContract.burn(amount); |
| 126 | + } |
| 127 | + |
| 128 | + // * Orders that haven't yet been processed may be canceled by the |
| 129 | + // customer, who will get back the tokens |
| 130 | + // * This triggers the event BeerCanceled |
| 131 | + // * Orders can be canceled at any time |
| 132 | + function cancelOrder(uint amount) external { |
| 133 | + require(orders[msg.sender] >= amount); |
| 134 | + require(beerTokenContract.transfer(msg.sender, amount),"Failed to transfer tokens"); |
| 135 | + orders[msg.sender] -= amount; |
| 136 | + emit BeerCanceled(msg.sender, amount); |
| 137 | + |
| 138 | + } |
| 139 | + |
| 140 | + |
| 141 | + // * Get pending orders for a customer |
| 142 | + function pendingBeer(address customer) external view returns (uint256) { |
| 143 | + return orders[customer]; |
| 144 | + } |
| 145 | + |
| 146 | + // * Beer price can only be changed by owners when the bar is closed |
| 147 | + function setBeerPrice(uint256 price) external { |
| 148 | + require(!_barIsOpen, " Beer price can only be changed when the bar is closed"); |
| 149 | + require(hasRole(OWNER, msg.sender), " Beer price can only be changed by owners "); |
| 150 | + _beerPrice = price; |
| 151 | + } |
| 152 | + |
| 153 | + function getBeerPrice() external view returns(uint256) { |
| 154 | + return _beerPrice; |
| 155 | + } |
| 156 | + |
| 157 | + // * Customers may buy tokens for Ether |
| 158 | + // * If the supplied Ether is not divisible by the beer price |
| 159 | + // the rest is kept as a tip. The caller (like the web interface) |
| 160 | + // can check that the value is a multiple of the beer price. |
| 161 | + function buyToken() external payable { |
| 162 | + require(_beerPrice > 0, " Beer price has not been set"); |
| 163 | + require(beerTokenContract.transfer(msg.sender, (msg.value-(msg.value % _beerPrice))/_beerPrice),"Failed to transfer"); |
| 164 | + //require(beerTokenContract.mint(msg.sender, (msg.value-(msg.value % _beerPrice))/_beerPrice),"Failed to mint"); |
| 165 | + } |
| 166 | + |
| 167 | + // * `amount` (in Wei) of the Ether stored in the contract is transferred to |
| 168 | + // `receiver`, provided `amount` does not exceed the balance of the contract |
| 169 | + // * Only owners are allowed to do this |
| 170 | + function payout(address payable receiver, uint256 amount) external { |
| 171 | + require(hasRole(OWNER, msg.sender), " Only owners are allowed to do this"); |
| 172 | + (bool success, ) = receiver.call{value: amount}(""); |
| 173 | + require(success, "Failed to send Ether"); |
| 174 | + } |
| 175 | + |
| 176 | + |
| 177 | +} |
0 commit comments