Skip to content

Instantly share code, notes, and snippets.

@partylikeits1983
Last active May 23, 2021 02:55
Show Gist options
  • Save partylikeits1983/1974d9afd492e6740e1068cae62d49da to your computer and use it in GitHub Desktop.
Save partylikeits1983/1974d9afd492e6740e1068cae62d49da to your computer and use it in GitHub Desktop.
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