Last active
May 23, 2021 02:55
-
-
Save partylikeits1983/1974d9afd492e6740e1068cae62d49da to your computer and use it in GitHub Desktop.
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.6.11; | |
// https://ethereum.org/en/developers/docs/smart-contracts/ | |
contract VendingMachine { | |
// Declare state variables of the contract | |
address public owner; | |
mapping (address => uint) public cupcakeBalances; | |
// When 'VendingMachine' contract is deployed: | |
// 1. set the deploying address as the owner of the contract | |
// 2. set the deployed smart contract's cupcake balance to 100 | |
constructor() public { | |
owner = msg.sender; | |
cupcakeBalances[address(this)] = 100; | |
} | |
// Allow the owner to increase the smart contract's cupcake balance | |
function refill(uint amount) public { | |
require(msg.sender == owner, "Only the owner can refill."); | |
cupcakeBalances[address(this)] += amount; | |
} | |
// Allow anyone to purchase cupcakes | |
function purchase(uint amount) public payable { | |
require(msg.value >= amount * 1 ether, "You must pay at least 1 ETH per cupcake"); | |
require(cupcakeBalances[address(this)] >= amount, "Not enough cupcakes in stock to complete this purchase"); | |
cupcakeBalances[address(this)] -= amount; | |
cupcakeBalances[msg.sender] += amount; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment