Solidity Programming Essentials
eBook - ePub

Solidity Programming Essentials

Ritesh Modi

Share book
  1. 222 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Solidity Programming Essentials

Ritesh Modi

Book details
Book preview
Table of contents
Citations

About This Book

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.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on ā€œCancel Subscriptionā€ - itā€™s as simple as that. After you cancel, your membership will stay active for the remainder of the time youā€™ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlegoā€™s features. The only differences are the price and subscription period: With the annual plan youā€™ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, weā€™ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Solidity Programming Essentials an online PDF/ePUB?
Yes, you can access Solidity Programming Essentials by Ritesh Modi in PDF and/or ePUB format, as well as other popular books in Computer Science & Programming in JavaScript. We have over one million books available in our catalogue for you to explore.

Information

Year
2018
ISBN
9781788838375
Edition
1

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 of contents