Book Image

Solidity Programming Essentials. - Second Edition

By : Ritesh Modi
Book Image

Solidity Programming Essentials. - Second Edition

By: Ritesh Modi

Overview of this book

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.
Table of Contents (21 chapters)
1
Part 1: The Fundamentals of Solidity and Ethereum
7
Part 2: Writing Robust Smart Contracts
13
Part 3: Advanced Smart Contracts

What is a blockchain?

A blockchain is essentially a decentralized, distributed database or ledger, as follows:

  • Decentralization: In simple terms, this means that the application or service continues to be available and usable even if a server or a group of servers on a network crashes or is not available. The service or application is deployed on a network in a way that no server has absolute control over data and execution; rather, each server has a current copy of data and execution logic.
  • Distributed: This means that any server or node on a network is connected to every other node on the network. Rather than having one-to-one or one-to-many connectivity between servers, servers have many-to-many connections with other servers.
  • Database: This refers to the location for storing durable data that can be accessed at any point in time. A database allows the storage and retrieval of data as functionality and also provides management functionalities to manage data efficiently, such as exporting, importing, backup, and restoration.
  • Ledger: This is an accounting term. Think of it as specialized storage and retrieval of data. Think of ledgers that are available to banks – for example, when a transaction is executed with a bank. Let's say that Tom deposits USD 100 in his account; the bank enters this information in a ledger as credit. At some point in the future, Tom withdraws USD 25. The bank does not modify the existing entry and stored data from 100 to 75. Instead, it adds another entry in the same ledger as a debit of USD 25. This is because a ledger is a specialized database that does not allow modification of existing data. It allows you to create and append a new transaction to modify the current balance in the ledger. The blockchain is a database that has the same characteristics as a ledger. It allows newer transactions to be stored in an append-only pattern without any scope to modify past transactions. It is important here to understand that existing data can be modified by using a new transaction, but past transactions cannot be modified. A balance of USD 100 can be modified at any time by executing a new debit or credit transaction, but previous transactions cannot be modified. Take a look at the following diagram for a better understanding:
Figure 1.1 – A blockchain is essentially a chain of blocks

Figure 1.1 – A blockchain is essentially a chain of blocks

Blockchain means a chain of blocks – that is, having multiple blocks chained together, with each block storing transactions in such a way that it is not possible to change these transactions. We will discuss this in later sections when we talk about the storage of transactions and how immutability is achieved in a blockchain.

Because they are decentralized and distributed, blockchain solutions are stable, robust, durable, and highly available. There is no single point of failure. No single node or server is the owner of the data and solution, and everyone participates as a stakeholder.

Not being able to change and modify past transactions makes blockchain solutions highly trustworthy, transparent, and incorruptible.