Solidity Programming Essentials: A guide to building smart contracts and tokens using the widely used Solidity language 2nd Edition Ritesh Modi instant download
Solidity Programming Essentials: A guide to building smart contracts and tokens using the widely used Solidity language 2nd Edition Ritesh Modi instant download
https://ebookmeta.com/product/building-games-with-ethereum-smart-
contracts-intermediate-projects-for-solidity-developers-kedar-
iyer/
https://ebookmeta.com/product/beginning-ethereum-smart-contracts-
programming-with-examples-in-python-solidity-and-javascript-2nd-
edition-wei-meng-lee/
https://ebookmeta.com/product/blockchain-and-ethereum-smart-
contract-solution-development-dapp-programming-with-solidity-1st-
edition-weijia-zhang/
https://ebookmeta.com/product/between-romanticism-and-modernism-
four-studies-in-the-music-of-the-later-nineteenth-century-carl-
dahlhaus/
Studies in Inductive Logic and Probability Volume 2
Richard C. Jeffrey (Editor)
https://ebookmeta.com/product/studies-in-inductive-logic-and-
probability-volume-2-richard-c-jeffrey-editor/
https://ebookmeta.com/product/power-electronics-1st-edition-
daniel-w-hart/
https://ebookmeta.com/product/freezer-burn-a-military-sci-fi-
series-waymaker-wars-book-2-rachel-aukes/
https://ebookmeta.com/product/teaching-for-successful-
intelligence-to-increase-student-learning-and-achievement-2nd-
edition-robert-j-sternberg-elena-l-grigorenko/
https://ebookmeta.com/product/the-ethics-of-hooking-up-casual-
sex-and-moral-philosophy-on-campus-1st-edition-james-rocha/
Tempted by the Forbidden Mate 1st Edition Ellie Pond
https://ebookmeta.com/product/tempted-by-the-forbidden-mate-1st-
edition-ellie-pond/
Solidity
Programming
Essentials
Second Edition
Ritesh Modi
BIRMINGHAM—MUMBAI
Solidity Programming Essentials
Second Edition
Copyright © 2022 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or
transmitted in any form or by any means, without the prior written permission of the publisher,
except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the
information presented. However, the information contained in this book is sold without warranty,
either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors,
will be held liable for any damages caused or alleged to have been caused directly or indirectly by
this book.
Packt Publishing has endeavored to provide trademark information about all of the companies
and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing
cannot guarantee the accuracy of this information.
Associate Group Product Manager: Richa Tripathi
Publishing Product Manager: Gebin George
Senior Editor: Rohit Singh
Content Development Editor: Kinnari Chohan
Technical Editor: Maran Fernandes
Copy Editor: Safis Editing
Project Coordinator: Manisha Singh
Proofreader: Safis Editing
Indexer: Subalakshmi Govindan
Production Designer: Shankar Kalbhor
Marketing Coordinator: Sonakshi Bubbar
First published: April 2018
Second edition: May 2022
Production reference: 1200522
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.
ISBN 978-1-80323-118-1
www.packt.com
Contributors
About the author
Ritesh Modi is a technologist with more than 18 years of experience. He holds a master's
degree in science in AI/ML from LJMU. He has been recognized as a Microsoft Regional
Director for his contributions to building tech communities, products, and services. He
has published more than 10 tech books in the past and is a cloud architect, speaker, and
leader who is popular for his contributions to data centers, Azure, Kubernetes, blockchain,
cognitive services, DevOps, AI, and automation.
"I want to thank the people who have been close to me and supported me,
especially my wife, Sangeeta, my daughter, Avni, and my parents."
About the reviewer
Kevin Bluer is a lead blockchain engineer at ConsenSys, focusing on open source
development tooling such as Truffle and Ganache. Before Web3, he had over 15 years
of experience in software development, working with companies across the industrial
spectrum, including Microsoft, JPMorgan Chase, and Viacom. He is also an experienced
trainer and mentor, having worked with thousands of developers around the globe. Kevin
is also an active investor, photographer, and runner.
Table of Contents
Preface
2
Installing Ethereum and Solidity
Technical requirements 36 Creating a private network 42
Ethereum networks 36 Installing Ganache 46
Main network 37 Installing the Solidity compiler 50
Test network 37 Installing the web3 framework 51
Private network 38
Installing and using MetaMask 52
Consortium network 38
Summary59
Installing and configuring Geth 38 Questions59
Installing Geth on macOS 39 Further reading 59
Installing Geth on Windows 39
3
Introducing Solidity
Technical requirements 62 Storage and memory data
The Ethereum Virtual Machine 62 locations79
Understanding Solidity and Rule one 79
Solidity files 63 Rule two 79
Rule three 80
Pragma63
Rule four 80
Comments64
Rule five 80
Importing Solidity code 65
Rule six 82
Contracts66
Rule seven 83
The structure of a contract 67 Rule eight 84
State variables 69
Using literals 85
Structure70
Modifiers 71 Understanding integers 86
Events73 Understanding Boolean 87
Enumeration74 The byte data type 88
Functions74 Understanding arrays 90
Exploring data types in Solidity 76 Fixed arrays 91
Dynamic arrays 91
Value types 76
Special arrays 93
Reference types 77
Array properties 95
Table of Contents vii
4
Global Variables and Functions
Technical requirements 108 The difference between tx.origin and
msg.sender 114
Variable scoping 108
Type conversion 109 Cryptographic global variables 115
Implicit conversion 110 Address global variables 116
Explicit conversion 110 Contract global variables 116
Block and transaction Recovering addresses using
global variables 112 ecrecover117
Transaction- and message-related Summary122
global variables 114 Questions123
Further reading 123
5
Expressions and Control Structures
Technical requirements 126 Understanding the
Understanding Solidity do...while loop 131
expressions126 Understanding breaks 134
Understanding the if and if... Understanding continue 135
else decision control 128 Understanding return 136
Exploring while loops 129 Summary137
Questions137
Further reading 137
viii Table of Contents
7
Solidity Functions, Modifiers, and Fallbacks
Technical requirements 170 The address call method 181
Function input and output 170 The address callcode method 185
The address delegatecall method 185
Modifiers 172
The address staticcall method 186
Visibility scope 175
View, constant, and pure The fallback and receive
functions176 functions187
Address-related functions 179 Summary191
The address send method 179 Questions191
The address transfer method 181 Further reading 192
Table of Contents ix
8
Exceptions, Events, and Logging
Technical requirements 194 Try-catch in Solidity 209
Exception handling 194 Events and logging 214
Require197 Summary218
Assert 202 Questions219
Revert 204
Further reading 219
9
Basics of Truffle and Unit Testing
Technical requirements 222 Interactively working with
Application development life Truffle 232
cycle management 222 Summary234
Introducing Truffle 223 Questions235
Development with Truffle 225 Further reading 235
Testing with Truffle 230
10
Debugging Contracts
Technical requirements 238 Using a block explorer 244
Overview of debugging 238 Summary247
The Remix editor 238 Questions247
Using events 244 Further reading 247
x Table of Contents
12
Upgradable Smart Contracts
Technical requirements 270 Implementing simple solutions
Learning what constitutes with inheritance 277
upgradability270 Implementing simple solutions
Understanding dependency with composition 281
injection272 Implementing advanced
Providing instance addresses during solutions using proxy contracts 285
contract deployment 273 Writing upgradable contracts
Providing instance addresses following with upgradable storage 290
contract deployment 274
Summary292
Reviewing problematic smart Questions292
contracts275 Further reading 292
13
Writing Secure Contracts
Technical requirements 294 Reentrancy attack 296
SafeMath and under/overflow The EtherPot contract 297
attacks294 The Hacker contract 299
Table of Contents xi
14
Writing Token Contracts
Technical requirements 308 The ERC721 implementation 323
Introducing tokens 308 EIP223 330
ERC20 Tokens 309 ERC165334
15
Solidity Design Patterns
Technical requirements 340 Data cohesion 349
Introducing entity modeling 340 Out-of-bounds nested structure 349
Static data within a nested structure 349
Ethereum storage 341
The nested structure will not change in
Data types in Ethereum 341
the near future 349
Understanding data modeling Containment relationship 349
in Solidity 343 Having few relationships 350
Nested versus reference fields 343
Performing data modeling
Exploring types of relationships346 using an example 350
One-to-one relationships 347 Structures350
One-to-many relationships 347 State variables 351
Many-to-many relationships 348 Adding Employees 351
Retrieving a single Employees record 352
Reviewing the rules for Updating Employees 352
embedding structures 348 Retrieving all Employees 353
xii Table of Contents
á vivir en el olvido
y á morir en paz con Dios.