Reader small image

You're reading from  Mastering Blockchain Programming with Solidity

Product typeBook
Published inAug 2019
Reading LevelIntermediate
PublisherPackt
ISBN-139781839218262
Edition1st Edition
Languages
Concepts
Right arrow
Author (1)
Jitendra Chittoda
Jitendra Chittoda
author image
Jitendra Chittoda

Jitendra Chittoda is a blockchain security engineer at ChainSecurity. His day job is to perform security audit on smart contracts and expose security vulnerabilities in Solidity and Scilla contracts. He has also developed a non-custodial, decentralized, P2P lending contracts for ETHLend. The Solidity contracts that he has developed or audited handle over $100 million worth of cryptoassets. He also served as a tech and security advisor in various ICO projects. Before finding his passion for blockchain, he coded in Java for over 11 years. He is the founder and leader of Delhi-NCR-JUG, a non-profit meetup group for Java. He holds a master's degree in computer applications and is regularly invited as a speaker at various conferences and meetups.
Read more about Jitendra Chittoda

Right arrow

Getting Started with Solidity

In the last chapter, we learned about blockchain, smart contracts, and Ethereum. For writing smart contracts, there are multiple languages supported by Ethereum, including Solidity, Vyper, and Low-level Lisp-like Language (LLL).

In this chapter, we will deep dive into Ethereum's most popular contract-oriented language, called Solidity. We will cover Solidity's source code file structure and the different data types supported. We will also understand the different units, global variables, and functions it supports and where these should be used.

We will cover only the parts of Solidity language that are essential for Solidity developers. We will not cover the advance concepts of Solidity language.

The following topics will be covered in this chapter:

  • The structure of a Solidity source code file
  • The structure of a Solidity contract
  • The...

Introduction to the Solidity language

Solidity is high-level language oriented for smart contracts. It is a Turing-complete language, influenced by C++, Python, and JavaScript.

Solidity is a statically typed language that supports inheritance, libraries, and complex user-defined types. Solidity is a bit different from the other languages; for example, Solidity does not have null values. Variables are initialized with their default values. Solidity also does not have any exception-handling mechanism to propagate exceptions.

Solidity support different data types including uint, int, address, and many more. As developers, we must know that Solidity does not have boundary checks for the uint and int data types. This makes these data types vulnerable to overflow or underflow.

At the time of writing this book, a major release of Solidity, version 0.5.0, has...

Solidity data types

The Solidity language is statically typed. When writing a contract or library definition, all of the state variables and local variables must have the data type specified along with its declaration. There are many data types supported by Solidity. You can create your own custom type by using the data types supported by Solidity; for example, you can define your complex struct variable.

Solidity also has a support for the var keyword, used for dynamic data type. However, since Solidity version 0.4.20, the var keyword has been deprecated because of security concerns.

If we look at the Java language, it has primitive data types and reference data types. For reference data types, especially for instantiable class, the default value is null while initialization. However, in Solidity, there is no concept of null values. Instead of null, the...

Assigning variables with units

In Solidity, two types of units, ether and time units, are supported for literal numbers.

Ether units supported to simplify calculations related to ether amount. All of the ether unit amounts are converted into wei. Time units are supported to simplify calculations related to time. All of the time unit numbers are converted into the number of seconds in Unix epoch time.

Specifying ether amounts using ether units

A literal number can take a suffix of wei, finney, szabo, or ether and use it to convert between the sub-denominations of ether, where ether currency numbers without a suffix are assumed to be in wei:

Suffix example In wei
1 wei 1
1 szabo 1e12
1 finney 1e15
1 ether 1e18...

Global special variables and functions

In Solidity, there are some global variables and functions such as block, msg, tx, and gasleft. These functions and variables are accessible globally in all functions and modifiers of your contract. Any information related to the current block, transaction, transaction sender, and transaction initiation parameters can be accessed using these variables and functions. 

Using block and transaction properties

Special variables such as block, msg, now, and tx are globally available in all of your contracts and libraries. Your transaction execution sometimes needs information related to the sender/initiator of the transaction. In some specific calculations related...

Topics for self-study

We have covered the basics of Solidity in this chapter. However, there are some topics that we haven't covered, which you can learn about with some self-study. For this, we recommend the following:

Summary

Solidity is a language that executes instructions by consuming gas, and this gas is paid to the Ethereum network's miners. For Solidity instructions to execute, consumers have to buy ether and pay for using the computation and storage resources of the Ethereum blockchain network. Before the innovation of the contract-oriented Solidity language, there was no other language that required crypto fuel to be paid for each instruction's execution. This makes Solidity a unique Turing-complete language. 

We looked into the basics of the Solidity language in this chapter. We also covered the data types supported in Solidity. Solidity is a smart contract-oriented language that uses a very unique data type, called address. This address data type is the heart of the smart contracts. Almost all of the contracts that you will write will most likely use this data...

Questions

  1. What is the difference between the transfer and send functions available on the address data type?
  2. Why is transfer preferred over the send function call?
  3. When should you use the delegatecall function?
  4. How can you calculate the gas units to be set when doing transaction gas adjustments?
  5. How can you generate a random number in Solidity?
  6. What is the difference between the abi.encode and abi.encodePacked functions?
  7. What is the difference between the assert, revert, and require functions?

Further reading

This book presents an overview on the topics that are a must for any developer learning the Solidity language. Nonetheless, this book also covers more advanced topics to help you to write production-ready Solidity-based smart contracts. If you need to focus only on the Solidity language, you can read Solidity Programming Essentials (https://www.packtpub.com/in/application-development/solidity-programming-essentials) by Ritesh Modi, from Packt.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Mastering Blockchain Programming with Solidity
Published in: Aug 2019Publisher: PacktISBN-13: 9781839218262
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Jitendra Chittoda

Jitendra Chittoda is a blockchain security engineer at ChainSecurity. His day job is to perform security audit on smart contracts and expose security vulnerabilities in Solidity and Scilla contracts. He has also developed a non-custodial, decentralized, P2P lending contracts for ETHLend. The Solidity contracts that he has developed or audited handle over $100 million worth of cryptoassets. He also served as a tech and security advisor in various ICO projects. Before finding his passion for blockchain, he coded in Java for over 11 years. He is the founder and leader of Delhi-NCR-JUG, a non-profit meetup group for Java. He holds a master's degree in computer applications and is regularly invited as a speaker at various conferences and meetups.
Read more about Jitendra Chittoda