SMART CONTRACTS
WHAT IS A SMART CONTRACT?
A DIGITAL VENDING MACHINE
1money + snack selection = snack dispensed21pragma solidity 0.8.7;2
3contract VendingMachine {4
5 // Declare state variables of the contract6 address public owner;7 mapping (address => uint) public cupcakeBalances;8
9 // When 'VendingMachine' contract is deployed:10 // 1. set the deploying address as the owner of the contract11 // 2. set the deployed smart contract's cupcake balance to 10012 constructor() {13 owner = msg.sender;14 cupcakeBalances[address(this)] = 100;15 }16
17 // Allow the owner to increase the smart contract's cupcake balance18 function refill(uint amount) public {19 require(msg.sender == owner, "Only the owner can refill.");20 cupcakeBalances[address(this)] += amount;21 }22
23 // Allow anyone to purchase cupcakes24 function purchase(uint amount) public payable {25 require(msg.value >= amount * 1 btcc, "You must pay at least 1 BTCC per cupcake");26 require(cupcakeBalances[address(this)] >= amount, "Not enough cupcakes in stock to complete this purchase");27 cupcakeBalances[address(this)] -= amount;28 cupcakeBalances[msg.sender] += amount;29 }30}31
Show all CopyPERMISSIONLESS
COMPOSABILITY
LIMITATIONS
MULTISIG CONTRACTS
Last updated