Solidity Programming Essentials - Second Edition

Book description

A comprehensive guide sprinkled with lots of hands-on code samples to get you up and running with Solidity and writing your smart contracts on Blockchain and Ethereum

Key Features

  • Learn proven smart contract implementation challenges and solve them using Solidity
  • Go deeper into Solidity to write effective upgradable and maintainable smart contracts using best practices
  • Get to grips with the latest version of Solidity with updated codes and examples

Book Description

Solidity is a high-level language for writing smart contracts, and the syntax has large similarities with JavaScript, thereby making it easier for developers to learn, design, compile, and deploy smart contracts on large blockchain ecosystems including Ethereum and Polygon among others. This book guides you in understanding Solidity programming from scratch.

The book starts with step-by-step instructions for the installation of multiple tools and private blockchain, along with foundational concepts such as variables, data types, and programming constructs. You'll then explore contracts based on an object-oriented paradigm, including the usage of constructors, interfaces, libraries, and abstract contracts. The following chapters help you get to grips with testing and debugging smart contracts. As you advance, you'll learn about advanced concepts like assembly programming, advanced interfaces, usage of recovery, and error handling using try-catch blocks. You'll also explore multiple design patterns for smart contracts alongside developing secure smart contracts, as well as gain a solid understanding of writing upgradable smart concepts and data modeling. Finally, you'll discover how to create your own ERC20 and NFT tokens from scratch.

By the end of this book, you will be able to write, deploy, and test smart contracts in Ethereum.

What you will learn

  • Write efficient, effective, and secure smart contracts
  • Code, compile, and test smart contracts in an object-oriented way
  • Implement assembly code in Solidity
  • Adopt upgradable and haltable ownership and security design patterns
  • Understand exception handling and debugging in Solidity
  • Create new ERC20 and NFT tokens from the ground up

Who this book is for

This Ethereum book is primarily aimed at beginners who want to get started with Solidity Programming for developing an Ethereum smart contract. No prior knowledge of EVM is required, but knowing the basics of any programming language will help you follow along.

Table of contents

  1. Solidity Programming Essentials Second Edition
  2. Contributors
  3. About the author
  4. About the reviewer
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Download the color images
    6. Conventions used
    7. Get in touch
    8. Share Your Thoughts
  6. Part 1: The Fundamentals of Solidity and Ethereum
  7. Chapter 1: An Introduction to Blockchain, Ethereum, and Smart Contracts
    1. Technical requirements
    2. What is a blockchain?
    3. The need for blockchain
    4. Understanding cryptography
      1. Hashing
      2. Digital signatures
      3. Reviewing blockchain and Ethereum architecture
      4. Relationship between blocks
      5. How transactions and blocks are related to each other
    5. Consensus
      1. Proof of work
      2. Proof of stake
    6. Ethereum nodes
      1. EVM
      2. Mining nodes
      3. Ethereum validators
    7. Ethereum accounts
      1. Externally owned accounts
      2. Contract accounts
      3. Ether, gas, and transactions
    8. Blocks
    9. An end-to-end transaction
    10. Smart contract
      1. Writing smart contracts
    11. The internals of smart contract deployment
    12. Summary
    13. Questions
    14. Further reading
  8. Chapter 2: Installing Ethereum and Solidity
    1. Technical requirements
    2. Ethereum networks
      1. Main network
      2. Test network
      3. Private network
      4. Consortium network
    3. Installing and configuring Geth
      1. Installing Geth on macOS
      2. Installing Geth on Windows
    4. Creating a private network
    5. Installing Ganache
    6. Installing the Solidity compiler
    7. Installing the web3 framework
    8. Installing and using MetaMask
    9. Summary
    10. Questions
    11. Further reading
  9. Chapter 3: Introducing Solidity
    1. Technical requirements
    2. The Ethereum Virtual Machine
    3. Understanding Solidity and Solidity files
      1. Pragma
      2. Comments
      3. Importing Solidity code
      4. Contracts
    4. The structure of a contract
      1. State variables
      2. Structure
      3. Modifiers
      4. Events
      5. Enumeration
      6. Functions
    5. Exploring data types in Solidity
      1. Value types
      2. Reference types
    6. Storage and memory data locations
      1. Rule one
      2. Rule two
      3. Rule three
      4. Rule four
      5. Rule five
      6. Rule six
      7. Rule seven
      8. Rule eight
    7. Using literals
    8. Understanding integers
    9. Understanding Boolean
    10. The byte data type
    11. Understanding arrays
      1. Fixed arrays
      2. Dynamic arrays
      3. Special arrays
      4. Array properties
    12. Knowing more about the structure of an array
    13. Enumerations
    14. Understanding the address data type
    15. Working with mappings
    16. Summary
    17. Questions
    18. Further reading
  10. Chapter 4: Global Variables and Functions
    1. Technical requirements
    2. Variable scoping
    3. Type conversion
      1. Implicit conversion
      2. Explicit conversion
    4. Block and transaction global variables
      1. Transaction- and message-related global variables
      2. The difference between tx.origin and msg.sender
    5. Cryptographic global variables
    6. Address global variables
    7. Contract global variables
    8. Recovering addresses using ecrecover
    9. Summary
    10. Questions
    11. Further reading
  11. Chapter 5: Expressions and Control Structures
    1. Technical requirements
    2. Understanding Solidity expressions
    3. Understanding the if and if...else decision controls
    4. Exploring while loops
    5. Understanding the do...while loop
    6. Understanding breaks
    7. Understanding continue
    8. Understanding return
    9. Summary
    10. Questions
    11. Further reading
  12. Part 2: Writing Robust Smart Contracts
  13. Chapter 6: Writing Smart Contracts
    1. Technical requirements
    2. Smart contracts
    3. Writing a smart contract
    4. Creating contracts
      1. Using the new keyword
      2. Using the address of a contract
    5. Contract constructor
    6. Contract composition
    7. Inheritance
      1. Single inheritance
      2. Multilevel inheritance
      3. Hierarchical inheritance
      4. Multiple inheritance
    8. Encapsulation
    9. Polymorphism
      1. Function polymorphism
      2. Contract polymorphism
    10. Method overriding
    11. Abstract contracts
    12. Interfaces
      1. Advanced interfaces
    13. Library
      1. Importing a library
    14. Summary
    15. Questions
    16. Further reading
  14. Chapter 7: Solidity Functions, Modifiers, and Fallbacks
    1. Technical requirements
    2. Function input and output
    3. Modifiers
    4. Visibility scope
    5. View, constant, and pure functions
    6. Address-related functions
      1. The address send method
      2. The address transfer method
      3. The address call method
      4. The address callcode method
      5. The address delegatecall method
      6. The address staticcall method
    7. The fallback and receive functions
    8. Summary
    9. Questions
    10. Further reading
  15. Chapter 8: Exceptions, Events, and Logging
    1. Technical requirements
    2. Exception handling
      1. Require
      2. Assert
      3. Revert
    3. Try-catch in Solidity
    4. Events and logging
    5. Summary
    6. Questions
    7. Further reading
  16. Chapter 9: Basics of Truffle and Unit Testing
    1. Technical requirements
    2. Application development life cycle management
    3. Introducing Truffle
    4. Development with Truffle
    5. Testing with Truffle
    6. Interactively working with Truffle
    7. Summary
    8. Questions
    9. Further reading
  17. Chapter 10: Debugging Contracts
    1. Technical requirements
    2. Overview of debugging
    3. The Remix editor
    4. Using events
    5. Using a block explorer
    6. Summary
    7. Questions
    8. Further reading
  18. Part 3: Advanced Smart Contracts
  19. Chapter 11: Assembly Programming
    1. Technical requirements
    2. An introduction to Solidity and its advantages
    3. Getting started with Assembly programming
    4. Scopes and blocks
    5. Returning values
    6. Working with memory slots
    7. Working with storage slots
    8. Calling contract functions
    9. Determining contract addresses
    10. Summary
    11. Questions
    12. Further reading
  20. Chapter 12: Upgradable Smart Contracts
    1. Technical requirements
    2. Learning what constitutes upgradability
    3. Understanding dependency injection
      1. Providing instance addresses during contract deployment
      2. Providing instance addresses following contract deployment
    4. Reviewing problematic smart contracts
    5. Implementing simple solutions with inheritance
    6. Implementing simple solutions with composition
    7. Implementing advanced solutions using proxy contracts
    8. Writing upgradable contracts with upgradable storage
    9. Summary
    10. Questions
    11. Further reading
  21. Chapter 13: Writing Secure Contracts
    1. Technical requirements
    2. SafeMath and under/overflow attacks
    3. Reentrancy attack
    4. The EtherPot contract
    5. The Hacker contract
    6. Solutions to the reentrancy problem
    7. Security best practices
    8. Summary
    9. Questions
    10. Further reading
  22. Chapter 14: Writing Token Contracts
    1. Technical requirements
    2. Introducing tokens
    3. ERC20 Tokens
      1. ERC20 standard
      2. ERC20 functionality
      3. ERC20 events
    4. Non-fungible tokens
      1. ERC721
    5. The ERC721 implementation
      1. EIP223
      2. ERC165
    6. Summary
    7. Questions
    8. Further reading
  23. Chapter 15: Solidity Design Patterns
    1. Technical requirements
    2. Introducing entity modeling
      1. Ethereum storage
      2. Data types in Ethereum
    3. Understanding data modeling in Solidity
      1. Nested versus reference fields
    4. Exploring types of relationships
      1. One-to-one relationships
      2. One-to-many relationships
      3. Many-to-many relationships
    5. Reviewing the rules for embedding structures
      1. Data cohesion
      2. Out-of-bounds nested structure
      3. Static data within a nested structure
      4. The nested structure will not change in the near future
      5. Containment relationship
      6. Having few relationships
    6. Performing data modeling using an example
      1. Structures
      2. State variables
      3. Adding Employees
      4. Retrieving a single Employees record
      5. Updating Employees
      6. Retrieving all Employees
    7. Ownership in smart contracts
    8. Exploring ownership in Solidity
      1. Modifier
    9. Establishing ownership of a smart contract
      1. Multiownership
      2. Transfer of ownership
      3. MultiSig contracts
      4. Transfer of the ownership of assets within a smart contract
    10. Stoppable/haltable smart contract pattern
    11. Summary
    12. Questions
    13. Further reading
  24. Assessments
    1. Chapter 1, Introduction to Blockchain, Ethereum, and Smart Contracts 
    2. Chapter 2, Installing Ethereum and Solidity 
    3. Chapter 3, Introducing Solidity 
    4. Chapter 4, Global Variables and Functions 
    5. Chapter 5, Expressions and Control Structures 
    6. Chapter 6, Writing Smart Contracts 
    7. Chapter 7, Functions, Modifiers, and Fallbacks 
    8. Chapter 8, Exceptions, Events, and Logging 
    9. Chapter 9, Truffle Basics and Unit Testing 
    10. Chapter 10, Debugging Contracts 
    11. Chapter 11, Assembly Programming
    12. Chapter 12, Upgradable Smart Contracts 
    13. Chapter 13, Writing Secure Contracts 
    14. Chapter 14, Writing Token Contracts
    15. Chapter 15, Solidity Design Patterns 
    16. Why subscribe?
  25. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Share Your Thoughts

Product information

  • Title: Solidity Programming Essentials - Second Edition
  • Author(s): Ritesh Modi
  • Release date: June 2022
  • Publisher(s): Packt Publishing
  • ISBN: 9781803231181