class: title # Deep Dive Into Solidity --- ## Agenda 1. Brief Review of Solidity 2. Mapping 3. Structs 4. Enums 5. Events --- class: title ## Solidity Review --- ### Default Types - uint - bytes - string - boolean - mapping --- ### Control Structures - Modifiers - Functions - If statements - Require statements --- ## Property of Solidity Functions - public - private - return value - use of modifiers --- ## Purpose of Modifiers - providing access control for functions --- ## Mappings - allow developers to "map" a unique identifier to x - `x` can be of any type - useful for uniquely identifying users in smart contract - always of the form `{key: value}` pair - `key` is always unique - `value` is always contains information mapping to a respective key --- ## Mappings - An Example `{ key: value }` - `key`: address - `value`: characteristics --- class: title ## Mappings: A Demonstration --- ## State Representation - Examples - Token/Voting Contract - OPEN, PAUSED, CLOSED - Game Contract: IN PROGRESS, TIE, WIN --- ## Enums - short and sweet way to define a finite amount of state ### Enums Demo --- ## Mapping and Enums - Persisting data while the contract is in a certain date - Rejecting certain state changes depending on requirements of a contract ### Mappings and Enums: A Demo --- ## Lab 3 - Enums - Create a smart contract that has at least 3 states - Create f unction that allows the state to change - Save some persistent data that is relevant to the state on the blockchain --- ## Keeping Track of User Information ### Voting Contract - Assume: We have a contract that supports community voting. - What are some waysthat we can make sure a user cannot vote twice? --- ## Structs - Allows developers to keep duplicate data structures for some number of users - Are allowed to be customly named - Internal of the structs must be finite and defined by the developera ## Use Case - Game Contract: persisting information about a player such as their address or their scares - Voting: Persistent information about a voter --- ## Structs and Mappings - Useful to have the `value` of a mapping as a struct - Allows a contract to store more information --- ## Events - similar to `console.log()` in Javascript - logs are almost always associated with block number ### Why do we need events? - Blockchain = list of transactions - Events = at x transaction, SOMETHING happened - Example: Transfer events being emitted once tokens are transferred --- ## Usefulness of Events - Web3 and Ethers Integration - Events are the most important aspect in terms of integrating your smart contract with an application - Etherscan Integration - Allows eherscan to keep track of money movements - Testing --- ## Lab: Structs, Mappings, Events Determine a business use case and create a smart contract that meets the following criteria: - Uses structs - Uses Mappings - Emits at least 3 events Explain your use case and your solution to a TA or the professor.