Solidity Programming Essentials
eBook - ePub

Solidity Programming Essentials

Ritesh Modi

  1. 222 pages
  2. English
  3. ePUB (adapté aux mobiles)
  4. Disponible sur iOS et Android
eBook - ePub

Solidity Programming Essentials

Ritesh Modi

DĂ©tails du livre
Aperçu du livre
Table des matiĂšres
Citations

À propos de ce livre

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.

Foire aux questions

Comment puis-je résilier mon abonnement ?
Il vous suffit de vous rendre dans la section compte dans paramĂštres et de cliquer sur « RĂ©silier l’abonnement ». C’est aussi simple que cela ! Une fois que vous aurez rĂ©siliĂ© votre abonnement, il restera actif pour le reste de la pĂ©riode pour laquelle vous avez payĂ©. DĂ©couvrez-en plus ici.
Puis-je / comment puis-je télécharger des livres ?
Pour le moment, tous nos livres en format ePub adaptĂ©s aux mobiles peuvent ĂȘtre tĂ©lĂ©chargĂ©s via l’application. La plupart de nos PDF sont Ă©galement disponibles en tĂ©lĂ©chargement et les autres seront tĂ©lĂ©chargeables trĂšs prochainement. DĂ©couvrez-en plus ici.
Quelle est la différence entre les formules tarifaires ?
Les deux abonnements vous donnent un accĂšs complet Ă  la bibliothĂšque et Ă  toutes les fonctionnalitĂ©s de Perlego. Les seules diffĂ©rences sont les tarifs ainsi que la pĂ©riode d’abonnement : avec l’abonnement annuel, vous Ă©conomiserez environ 30 % par rapport Ă  12 mois d’abonnement mensuel.
Qu’est-ce que Perlego ?
Nous sommes un service d’abonnement Ă  des ouvrages universitaires en ligne, oĂč vous pouvez accĂ©der Ă  toute une bibliothĂšque pour un prix infĂ©rieur Ă  celui d’un seul livre par mois. Avec plus d’un million de livres sur plus de 1 000 sujets, nous avons ce qu’il vous faut ! DĂ©couvrez-en plus ici.
Prenez-vous en charge la synthÚse vocale ?
Recherchez le symbole Écouter sur votre prochain livre pour voir si vous pouvez l’écouter. L’outil Écouter lit le texte Ă  haute voix pour vous, en surlignant le passage qui est en cours de lecture. Vous pouvez le mettre sur pause, l’accĂ©lĂ©rer ou le ralentir. DĂ©couvrez-en plus ici.
Est-ce que Solidity Programming Essentials est un PDF/ePUB en ligne ?
Oui, vous pouvez accĂ©der Ă  Solidity Programming Essentials par Ritesh Modi en format PDF et/ou ePUB ainsi qu’à d’autres livres populaires dans Ciencia de la computaciĂłn et ProgramaciĂłn en JavaScript. Nous disposons de plus d’un million d’ouvrages Ă  dĂ©couvrir dans notre catalogue.

Informations

Année
2018
ISBN
9781788838375

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...

Table des matiĂšres

  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
Normes de citation pour 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.