Solidity Programming Essentials
eBook - ePub

Solidity Programming Essentials

Ritesh Modi

  1. 222 Seiten
  2. English
  3. ePUB (handyfreundlich)
  4. Über iOS und Android verfügbar
eBook - ePub

Solidity Programming Essentials

Ritesh Modi

Angaben zum Buch
Buchvorschau
Inhaltsverzeichnis
Quellenangaben

Über dieses Buch

Learn the most powerful and primary programming language for writing smart contracts and find out how to write, deploy, and test smart contracts in Ethereum.

Key Features

  • Get you up and running with Solidity Programming language
  • Build Ethereum Smart Contracts with Solidity as your scripting language
  • Learn to test and deploy the smart contract to your private Blockchain

Book Description

Solidity is a contract-oriented language whose syntax is highly influenced by JavaScript, and is designed to compile code for the Ethereum Virtual Machine. Solidity Programming Essentials will be your guide to understanding Solidity programming to build smart contracts for Ethereum and blockchain from ground-up.

We begin with a brief run-through of blockchain, Ethereum, and their most important concepts or components. You will learn how to install all the necessary tools to write, test, and debug Solidity contracts on Ethereum. Then, you will explore the layout of a Solidity source file and work with the different data types. The next set of recipes will help you work with operators, control structures, and data structures while building your smart contracts. We take you through function calls, return types, function modifers, and recipes in object-oriented programming with Solidity. Learn all you can on event logging and exception handling, as well as testing and debugging smart contracts.

By the end of this book, you will be able to write, deploy, and test smart contracts in Ethereum. This book will bring forth the essence of writing contracts using Solidity and also help you develop Solidity skills in no time.

What you will learn

  • Learn the basics and foundational concepts of Solidity and Ethereum
  • Explore the Solidity language and its uniqueness in depth
  • Create new accounts and submit transactions to blockchain
  • Get to know the complete language in detail to write smart contracts
  • Learn about major tools to develop and deploy smart contracts
  • Write defensive code using exception handling and error checking
  • Understand Truffle basics and the debugging process

Who this book is for

This book is for anyone who would like to get started with Solidity Programming for developing an Ethereum smart contract. No prior knowledge of EVM is required.

Häufig gestellte Fragen

Wie kann ich mein Abo kündigen?
Gehe einfach zum Kontobereich in den Einstellungen und klicke auf „Abo kündigen“ – ganz einfach. Nachdem du gekündigt hast, bleibt deine Mitgliedschaft für den verbleibenden Abozeitraum, den du bereits bezahlt hast, aktiv. Mehr Informationen hier.
(Wie) Kann ich Bücher herunterladen?
Derzeit stehen all unsere auf Mobilgeräte reagierenden ePub-Bücher zum Download über die App zur Verfügung. Die meisten unserer PDFs stehen ebenfalls zum Download bereit; wir arbeiten daran, auch die übrigen PDFs zum Download anzubieten, bei denen dies aktuell noch nicht möglich ist. Weitere Informationen hier.
Welcher Unterschied besteht bei den Preisen zwischen den Aboplänen?
Mit beiden Aboplänen erhältst du vollen Zugang zur Bibliothek und allen Funktionen von Perlego. Die einzigen Unterschiede bestehen im Preis und dem Abozeitraum: Mit dem Jahresabo sparst du auf 12 Monate gerechnet im Vergleich zum Monatsabo rund 30 %.
Was ist Perlego?
Wir sind ein Online-Abodienst für Lehrbücher, bei dem du für weniger als den Preis eines einzelnen Buches pro Monat Zugang zu einer ganzen Online-Bibliothek erhältst. Mit über 1 Million Büchern zu über 1.000 verschiedenen Themen haben wir bestimmt alles, was du brauchst! Weitere Informationen hier.
Unterstützt Perlego Text-zu-Sprache?
Achte auf das Symbol zum Vorlesen in deinem nächsten Buch, um zu sehen, ob du es dir auch anhören kannst. Bei diesem Tool wird dir Text laut vorgelesen, wobei der Text beim Vorlesen auch grafisch hervorgehoben wird. Du kannst das Vorlesen jederzeit anhalten, beschleunigen und verlangsamen. Weitere Informationen hier.
Ist Solidity Programming Essentials als Online-PDF/ePub verfügbar?
Ja, du hast Zugang zu Solidity Programming Essentials von Ritesh Modi im PDF- und/oder ePub-Format sowie zu anderen beliebten Büchern aus Ciencia de la computación & Programación en JavaScript. Aus unserem Katalog stehen dir über 1 Million Bücher zur Verfügung.

Information

Introducing Solidity

From this chapter, we will embark on a journey: learning the Solidity language. The previous two chapters introduced blockchains, Ethereum, and their toolsets. Some important concepts related to blockchains, which are essential for having a better understanding and writing efficient code in Solidity, were also discussed. There are multiple languages that target EVM. Some of them are deprecated and others are used with varying degrees of acceptance. Solidity is by far the most popular language for EVM. From this chapter onward, the book will focus on Solidity and its concepts, as well as constructs to help write efficient smart contracts.
In this chapter, we will jump right into understanding Solidity, its structure, data types, and variables. We will cover the following topics in this chapter:
  • Solidity and Solidity files
  • Structure of a contract
  • Data types in Solidity
  • Storage and memory data locations
  • Literals
  • Integers
  • Boolean
  • The byte data type
  • Arrays
  • Structure of an array
  • Enumeration
  • Address
  • Mappings

Ethereum Virtual Machine

Solidity is a programming language targeting Ethereum Virtual Machine (EVM). Ethereum blockchain helps extend its functionality by writing and executing code known as smart contracts. We will get into the details of smart contracts in subsequent chapters, but for now, it is enough to know that smart contracts are similar to object-oriented classes written in Java or C++.
EVM executes code that is part of smart contracts. Smart contracts are written in Solidity; however, EVM does not understand the high-level constructs of Solidity. EVM understands lower-level instructions called bytecode.
Solidity code needs a compiler to take its code and convert it into bytecode that is understandable by EVM. Solidity comes with a compiler to do this job, known as the Solidity compiler or solc. We downloaded and installed the Solidity compiler in the last chapter using the Node.js npm command.
The entire process is shown in the following diagram, from writing code in Solidity to executing it in EVM:
We have already explored our first Solidity code in the last chapter, when writing our HelloWorld contract.

Solidity and Solidity files

Solidity is a programming language that is very close to JavaScript. Similarities between JavaScript and C can be found within Solidity. Solidity is a statically-typed, case-sensitive, and object-oriented programming (OOP) language. Although it is object-oriented, it supports limited objected orientation features. What this means is that variable data types should be defined and known at compile time. Functions and variables should be written in OOP same way as they are defined. In Solidity, Cat is different from CAT, cat, or any other variation of cat. The statement terminator in Solidity is the semicolon: ;.
Solidity code is written in Solidity files that have the extension .sol. They are human-readable text files that can be opened as text files in any editor including Notepad.
A Solidity file is composed of the following four high-level constructs:
  • Pragma
  • Comments
  • Import
  • Contracts/library/interface

Pragma

Pragma is generally the first line of code within any Solidity file. pragma is a directive that specifies the compiler version to be used for current Solidity file.
Solidity is a new language and is subject to continuous improvement on an on-going basis. Whenever a new feature or improvement is introduced, it comes out with a new version. The current version at the time of writing was 0.4.19.
With the help of the pragma directive, you can choose the compiler version and target your code accordingly, as shown in the following code example:
pragma Solidity ^0.4.19;
Although it is not mandatory, it is a good practice to declare the pragma directive as the first statement in a Solidity file.
The syntax for the pragma directive is as follows:
pragma Solidity <<version number>> ;
Also notice the case-sensitivity of the directive. Both pragma and Solidity are in small letters, with a valid version number and statement terminated with a semicolon.
The version number comprises of two numbers—a major build and a minor build number.
The major build number in the preceding example is 4 and the minor build number is 19. Generally, there are fewer or no breaking changes within minor versions but there could be significant changes between major versions. You should choose a version that best suits your requirements.
The ^ character, also known as caret, is optional in version numbers but plays a significant role in deciding the version number based on the following rules:
  • The ^ character refers to the latest version within a major version. So, ^0.4.0 refers to the latest version within build number 4, which currently would be 0.4.19.
  • The ^ character will not target any other major build apart from the one that is provided.
  • The Solidity file will compile only with a compiler with 4 as the major build. It will not compile with any other major build.
As a good practice, it is better to compile Solidity code with an exact compiler version rather than using ^. There are changes in newer version that could deprecate your code while using ^ in pragma. For example, the throw statement got deprecated and newer constructs such as assert, require, and revert were recommended for use in newer versions. You do not want to get surprised on a day when your code starts behaving differently.

Comments

Any programming language provides the facility to comment code and so does Solidity. There are the following three types of comment in Solidity:
  • Single-line comments
  • Multiline comments
  • Ethereum Natural Specification (Natspec)
Single-line comments are denoted by a double forward slash //, while multiline comments are denoted using /* and */. Natspec has two formats: /// for single-line and a combination of /** for beginning and */ for end of multiline comments. Natspec is used for documentation purposes and it has its own specification. The entire specification is available at https://github.com/ethereum/wiki/wiki/Ethereum-Natural-Specification-Format.
Let's take a look at Solidity comments in the following code:
// This is a single-line comment in Solidity
/* This is a multiline comment
In Solidity. Use this when multiple consecutive lines
Should be commented as a whole */
In Remix, the pragma directive and comments are as shown in the following screenshot:

The import statement

The import keyword helps import other Solidity files and we can access its code within the current Solidity file and code. This helps us write modular Solidity code.
The syntax for using import is as follows:
import <<filename>> ;
File names can be fully explicit or implicit paths. The forward slash / is used for separating directories from other directories and files while . is used to refer to the current directory and .. is used to refer to the parent directory. This is very similar to the Linux bash way of referring to a file. A typical import statement is shown here. Also, note the semicolon towards the end of the statement in the following code:
import 'CommonLibrary.sol';

Contracts

Apart from pragma, import, and comments, we can define contracts, libraries, and interfaces at the global or top level. We will explore contracts, libraries, and interfaces in depth in subsequent chapters. This chapter assumes that you understand that multiple contracts, libraries, and interfaces can be declared within the same Solidity file. The library, contract, and interface keywords shown in the following screenshot are case-sensitive in nature:

Structure of a contract

The primary purpose of Solidity is to...

Inhaltsverzeichnis

  1. Title Page
  2. Copyright and Credits
  3. Packt Upsell
  4. Contributors
  5. Preface
  6. Introduction to Blockchain, Ethereum, and Smart Contracts
  7. Installing Ethereum and Solidity
  8. Introducing Solidity
  9. Global Variables and Functions
  10. Expressions and Control Structures
  11. Writing Smart Contracts
  12. Functions, Modifiers, and Fallbacks
  13. Exceptions, Events, and Logging
  14. Truffle Basics and Unit Testing
  15. Debugging Contracts
  16. Other Books You May Enjoy
Zitierstile für Solidity Programming Essentials

APA 6 Citation

Modi, R. (2018). Solidity Programming Essentials (1st ed.). Packt Publishing. Retrieved from https://www.perlego.com/book/695265/solidity-programming-essentials-pdf (Original work published 2018)

Chicago Citation

Modi, Ritesh. (2018) 2018. Solidity Programming Essentials. 1st ed. Packt Publishing. https://www.perlego.com/book/695265/solidity-programming-essentials-pdf.

Harvard Citation

Modi, R. (2018) Solidity Programming Essentials. 1st edn. Packt Publishing. Available at: https://www.perlego.com/book/695265/solidity-programming-essentials-pdf (Accessed: 14 October 2022).

MLA 7 Citation

Modi, Ritesh. Solidity Programming Essentials. 1st ed. Packt Publishing, 2018. Web. 14 Oct. 2022.