Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Solidity Programming Essentials
Solidity Programming Essentials

Solidity Programming Essentials: A guide to building smart contracts and tokens using the widely used Solidity language , Second Edition

eBook
€8.99 €25.99
Paperback
€31.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Table of content icon View table of contents Preview book icon Preview Book

Solidity Programming Essentials

Chapter 1: An Introduction to Blockchain, Ethereum, and Smart Contracts

The last decade has already seen an extraordinary evolution of the technology and computing ecosystem. Technological innovation and its impact have been noticeable across the spectrum, from the Internet of Things (IoT) and Artificial Intelligence (AI) to blockchains. Each of them has had a disruptive force within multiple industries, and blockchains are one of the most disruptive technologies today – so much so that blockchains have the potential to change almost every industry. Blockchains are revolutionizing almost all industries and domains while bringing forward newer business models. Blockchain is not a new technology; however, it has gained momentum over the last couple of years. It is a big leap forward in terms of thinking about decentralized and distributed applications. It is about the current architectural landscape and strategies for moving toward immutable distributed databases.

In this chapter, you will quickly learn and understand the basic and foundational concepts of blockchains and Ethereum. We will also discuss some of the important concepts that make blockchains and Ethereum work. Also, we will touch briefly on the topic of smart contracts and how to author them using Solidity.

Please note that this chapter explains important blockchain concepts briefly. It does not explain all concepts in great detail, as that would require a complete book by itself. Since Ethereum is an implementation of a blockchain, both the terms will be used interchangeably in this book.

This chapter will focus on introducing the following topics:

  • What a blockchain is and why it is used
  • Understanding cryptography
  • Blockchain and Ethereum architecture
  • Ether, gas, and transactions
  • Consensus
  • Nodes
  • Mining
  • Understanding accounts, transactions, and blocks
  • Smart contracts

By the end of this chapter, you will know all about the basics of blockchain, Ethereum, and smart contracts, which will prepare the base for the next chapters.

Technical requirements

To execute the instructions given in this chapter, you can use a machine with any operating system (Mac, Windows, or Linux) with a browser installed on it. Code for this chapter can be found on GitHub at https://github.com/PacktPublishing/Solidity-Programming-Essentials-Second-Edition/tree/main/Chapter01.

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.

The need for blockchain

The main objective of blockchain is to accept transactions from accounts, update the current state, and maintain this state till another transaction updates it again. This entire process can be divided into two phases in blockchain. There is a decoupling in between when a transaction is accepted by Ethereum and when the transaction is executed and written to the ledger. This decoupling is quite important for decentralization and distributed architecture to work as expected.

Blockchain helps primarily in the following three different ways:

  • Trust: Blockchain helps in creating applications that are decentralized and collectively owned by multiple people. Nobody within this group has the power to change or delete previous transactions. Even if someone tries to do so, it will not be accepted by other stakeholders.
  • Autonomy: There is no single owner for blockchain-based applications. No one controls the blockchain, but everyone participates in its activities. This helps in creating solutions that cannot be manipulated or corrupted.
  • Intermediaries: Blockchain-based applications can help remove intermediaries from existing processes. Generally, there is a central body, such as vehicle registration and license issuing, that acts as a registrar for registering vehicles as well as issuing driver licenses. Without blockchain-based systems, there is no central body, and if a license is issued or a vehicle is registered after a blockchain mining process, that will remain a fact for an epoch (an epoch is a period of time – say, 5 seconds) without the need of any central authority vouching for it.

Blockchain is heavily dependent on cryptography technologies, as we will discuss in the following section.

Understanding cryptography

Cryptography is the science of converting plain, simple text into secret, hidden, and meaningful text, and vice versa. It also helps in transmitting and storing data that cannot be easily deciphered using owned keys.

There are two types of cryptography in computing:

  • Symmetric cryptography: This refers to the process of using a single key for both encryption and decryption. It means the same key should be available for multiple people if they want to exchange messages using this form of cryptography.
  • Asymmetric cryptography: This refers to the process of using two keys for encryption and decryption. Any key can be used to encrypt and decrypt. Messages encrypted with a public key can be decrypted using a private key, and messages encrypted by a private key can be decrypted using a public key. Let's understand this with the help of an example. Tom uses Alice's public key to encrypt messages and sends it to Alice. Alice can use her private key to decrypt the message and extract its content. Messages encrypted with Alice's public key can only be decrypted by Alice, as only she holds her private key and no one else. This is the general use case of asymmetric keys. There is another use that we will see in the Digital signatures section.

Hashing

Hashing is the process of transforming any input data into fixed-length random character data, and it is not possible to regenerate or identify the original data from the resultant hash. Hashes are also known as fingerprints of input data. It is next to impossible to derive input data based on its hash value. Hashing ensures that even a slight change in input data will completely change the output data, and no one can ascertain the change in the original data.

Another important property of hashing is that no matter the size of input string data, the length of its output is always fixed. For example, using the SHA-256 hashing algorithm and function with any length of input will always generate 256-bit output data. This can especially become useful when large amounts of data can be stored as 256-bit output data. Ethereum uses the hashing technique quite extensively. It hashes every transaction, hashes the hash of two transactions at a time, and ultimately generates a single root transaction hash for every transaction within a block.

Another important property of hashing is that it is not mathematically feasible to identify two different input strings that will output the same hash. Similarly, it is not possible to find the input computationally and mathematically from the hash itself.

Ethereum uses Keccak256 as its hashing algorithm. The following screenshot shows an example of hashing. The Ritesh Modi input generates a hash, as shown in the following screenshot:

Figure 1.2 – A hashing example using the SHA-256 algorithm

Figure 1.2 – A hashing example using the SHA-256 algorithm

Even a small modification of input generates a completely different hash, as shown in the following screenshot:

Figure 1.3 – A completely different hash output based on a small change in the original input

Figure 1.3 – A completely different hash output based on a small change in the original input

Digital signatures

Earlier, we discussed cryptography using asymmetric keys. One of the important uses for asymmetric keys is in the creation and verification of a digital signature. Digital signatures are very similar to a signature done by an individual on a piece of paper. Similar to a paper signature, a digital signature helps in identifying an individual. It also helps in ensuring that messages are not tampered with while in transit. Let's understand digital signatures with the help of an example.

Alice wants to send a message to Tom. How can Tom identify and ensure that the message has come from Alice only and that the message has not been changed or tampered with in transit? Instead of sending a raw message/transaction, Alice creates a hash of the entire payload and encrypts the hash with her private key. She appends the resultant digital signature to the hash and transmits it to Tom. When the transaction reaches Tom, he extracts the digital signature and decrypts it using Alice's public key to find the original hash. He also extracts the original hash from the rest of the message and compares both the hashes. If the hashes match, it means that it actually originated from Alice and that it has not been tampered with.

Digital signatures are used to sign transaction data by the owner of the asset or cryptocurrency, such as Ether. With a basic understanding of cryptography, it's time to introduce Ethereum and blockchain at a high level.

Reviewing blockchain and Ethereum architecture

Blockchain is an architecture comprising multiple components, and what makes blockchain unique is the way these components function and interact with each other. Ethereum allows you to extend its functionality with the help of smart contracts. (Smart contracts will be addressed in detail throughout this book.)

Some of the important Ethereum components are the Ethereum Virtual Machine (EVM), miner, block, transaction, consensus algorithm, account, smart contract, mining, Ether, and gas. We are going to discuss each of these components in this chapter.

A blockchain network consists of multiple nodes belonging to miners and some nodes that do not mine but help in the execution of smart contracts and transactions. These are known as EVMs. Each node is connected to another node on the network. These nodes use a peer-to-peer protocol to talk to each other. They, by default, use port 30303 to talk among themselves.

Each miner maintains an instance of a ledger. A ledger contains all blocks in the chain. With multiple miners, it is quite possible that each miner's ledger instance might have different blocks to another. The miners synchronize their blocks on an ongoing basis to ensure that every miner's ledger instance is the same as the other. Details about ledgers, blocks, and transactions are discussed in detail in subsequent sections in this chapter.

The EVM executes smart contracts and helps bring about changes to the global state. Smart contracts help in extending Ethereum by writing custom business functionality into it. These smart contracts can be executed as part of a transaction, and it follows the process of mining as discussed earlier.

A person with an account on a network can send a message for the transfer of Ether from their account to another or can send a message to invoke a function within a contract. Ethereum does not distinguish them as far as transactions are considered. The transaction must be digitally signed with an account holder's private key. This is to ensure that the identity of the sender can be established while verifying the transaction and changing the balances of multiple accounts. Let's take a look at the components of Ethereum in the following diagram:

Figure 1.4 – The relationship between blockchain and mining

Figure 1.4 – The relationship between blockchain and mining

The previous diagram illustrates some of the important components in Ethereum. The externally owned accounts are responsible for initiating transactions on Ethereum. The transactions that are executed within the Ethereum nodes are finally written as blocks on the blockchain. These blocks have header sections that help in chaining the blocks.

Relationship between blocks

In blockchain and Ethereum, every block is related to another block. There is a parent-child relationship between two blocks. There can be only one child to a parent and a child can have a single parent. This helps in forming a chain in blockchain, as shown. Blocks will be explained in a later section in this chapter:

Figure 1.5 – The relationship between blocks using a block hash

Figure 1.5 – The relationship between blocks using a block hash

In this diagram, we can see three blocks apart from the Genesis BlockBlock 1, Block 2, and Block 3. Block 1 is the parent of Block 2, and Block 2 is the parent of Block 3. The relationship is established by storing the parent block's hash in a child's block header. Block 2 stores the hash of Block 1 in its header and Block 3 stores the hash of Block 2 in its header. So, the question arises – who is the parent of the first block? Ethereum has a concept of the genesis block, also known as the first block. This block is created automatically when the chain is first initiated. You can say that a chain is initiated with the first block, the genesis block, and the formation of this block is driven through the genesis.json file.

The next chapter will show you how to use the genesis.json file to create the first block while initializing the blockchain.

How transactions and blocks are related to each other

Now that we know that blocks are related to each other, you will be interested in knowing how transactions are related to blocks. Ethereum stores transactions within blocks. Each block has an upper gas limit, and each transaction needs a certain amount of gas to be consumed as part of its execution. The cumulative gas from all transactions that are not yet written in a ledger cannot surpass the block gas limit. This ensures that all transactions do not get stored within a single block. As soon as the gas limit is reached, other transactions are removed from the block and mining begins thereafter. The gas concept will be covered in a subsequent section in this chapter. This section should be revisited after reading about gas.

The transactions are hashed and stored in the block. The hashes of two transactions are taken and hashed further to generate another hash. This process eventually provides a single hash from all transactions stored within the block. This hash is known as the transaction Merkle root hash and is stored in a block's header.

A change in any transaction will result in a change in its hash and, eventually, a change in the root transaction hash. It will have a cumulative effect because the hash of the block will change, and the child block has to change its hash because it stores its parent hash. This helps in making transactions immutable. This is also shown in the following diagram:

Figure 1.6 – The Merkle root representing all transaction hashes

Figure 1.6 – The Merkle root representing all transaction hashes

Blocks and transactions are core to blockchain, but the question remains about the process of adding them to the chain. A generated block should be agreed upon and acceptable to all the nodes within a network. The process of coming to an agreement on a block and subsequently either adding it to the chain or rejecting it is based on the process known as consensus.

Consensus

Consensus is one of the most important concepts in the world of blockchain. As we know, a blockchain comprises multiple nodes that are running the same software and storing the same data. There should be some agreement between the nodes to accept a given state as a known agreeable state. With thousands of nodes running together, intertwined using a Peer-to-Peer (P2P) network, they should agree on what is stored historically on the blocks and what should be included as transactions on the new block. It is quite possible that a node goes rogue and adds transactions that are not valid. The consensus mechanism or agreements among nodes would ensure that those transactions are not accepted by other ones. A rogue actor has to minimally control a majority of the nodes to make everyone agree on the dubious transactions.

There are various ways to implement and achieve consensus among nodes. Bitcoin is implemented using the Proof-of-Work (PoW) consensus algorithm. Ethereum also implements the PoW consensus algorithm as part of Ethereum 1.0. The new upcoming Ethereum 2.0 is changing its consensus algorithm to the Proof-of-Stake (PoS) consensus algorithm.

One of the main drawbacks of the PoW consensus algorithm is that is it quite costly to generate and agree on blocks due to its mining process, which will be explained later. Mining involves heavy usage of electricity and that has negative consequences toward environmental hazards and cost. Both PoW and PoS will be explained in the next subsections.

Proof of work

A miner is responsible for writing transactions to the Ethereum chain. A miner's job is very similar to that of an accountant. An accountant is responsible for writing and maintaining a ledger; similarly, a miner is solely responsible for writing a transaction to an Ethereum ledger. A miner is interested in writing transactions to a ledger because of the reward associated with it. Miners get two types of reward – a reward for writing a block to the chain and cumulative gas fees from all transactions in the block. There are generally many miners available within a blockchain network, each trying and competing to write transactions. However, only one miner can write the block to the ledger, and the rest will not be able to write the current block.

The miner responsible for writing the block is determined by way of a puzzle. The challenge is given to every miner, and they try to solve the puzzle using their computing power. The miner who solves the puzzle first writes the block containing transactions to their own ledger and sends the block and nonce value to other miners for verification. Once verified and accepted, the new block is written to all ledgers belonging to miners. In this process, the winning miner also receives Ether as a reward. Every mining node maintains its own instance of the Ethereum ledger, and the ledger is ultimately the same across all miners. It is the miner's job to ensure that their ledger is updated with the latest blocks. The following are the three important functions performed by miners or mining nodes:

  • Mine or create a new block with a transaction and write this to the Ethereum ledger.
  • Advertise and send a newly mined block to other miners.
  • Accept new blocks mined by other miners and keep their own ledger instance up to date.

Mining nodes refer to nodes that belong to miners. These nodes are part of the same network where the EVM is hosted. At some point in time, miners will create a new block, collect all transactions from the transaction pool, and add them to the newly created block. Finally, this block is added to the chain. There are additional concepts such as consensus and the solving of a target puzzle before writing the block, which will be explained in the following section.

Miners are always looking forward to mining new blocks and are also listening actively to receive new blocks from other miners. They are listening for new transactions stored in the transaction pool. Miners also broadcast the new transactions to other connected nodes after validation. A miner collects all transactions available within the transaction pool, subject to constraints such as block size and block gas limits, and creates a block with them. This activity is done by all miners.

The miner constructs a new block and adds all transactions to it. Before adding these transactions, it will check whether any of the transactions are not already written in a block that it might receive from other miners. If so, it will discard those transactions.

The miner will add its own coinbase transaction for getting rewards for mining the block.

The next task for a miner is to generate the block header and perform the following tasks:

  1. The miner takes hashes of two transactions at a time to generate a new hash till they get a single hash from all transactions. The hash is referred to as a root transaction hash or Merkle root transaction hash. This hash is added to the block header.
  2. The miner also identifies the hash of the previous block. The previous block will become a parent to the current block, and its hash will also be added to the block header.
  3. The miner calculates the state and receipts of the transaction root hashes and adds them to the block header.
  4. A nonce and timestamp are also added to the block header.
  5. A block hash consisting of both a block header and body is generated.
  6. The mining process starts where the miner keeps changing the nonce value and tries to find a hash that will satisfy as an answer to the given puzzle. Keep in mind that everything mentioned here is executed by every miner in the network.
  7. Eventually, one of the miners will be able to solve the puzzle and advertise the result to other miners in the network. The other miners will verify the answer and, if found correct, further verify every transaction, accept the block, and append it to their ledger instance.

This entire process is also known as PoW wherein a miner provides proof that they have worked on computing the final answer that is satisfactory as a solution to the puzzle. The header block and its content are shown in the following diagram:

Figure 1.7 – PoW generating a Merkle root and using transactions

Figure 1.7 – PoW generating a Merkle root and using transactions

Let's now discuss the next mechanism.

Proof of stake

Ethereum 2.0 will launch a new consensus mechanism known as PoS. It is another algorithm to reach consensus within a distributed network architecture. While PoW is computationally heavy, PoS does not perform any compute-heavy activities. Under this mechanism, interested stakeholders can stake their Ether (a minimum of 32 ETH) with the network. Once the Ethers are staked, they are locked, and these stakeholders become validators. These validators have their nodes running, and their job is to attest to new blocks for their validity. Attestation here means that the validator is vouching for the correctness of the block and its constituent transactions.

After a minimum number of validators attest, the block is finalized and becomes a permanent part of the chain. The network will at random also choose a validator to create a new block for every epoch. The validators are not competing with each other anymore as in the case of the PoW consensus mechanism. This makes PoS more environmentally friendly by being energy efficient and also anyone with 32 Ethers can potentially become a validator. Now that we understand the different consensus mechanisms, it's time to visit the different types of nodes supported by Ethereum.

Ethereum nodes

Nodes represent the computers that are connected using a P2P protocol to form an Ethereum network. There are the following three types of nodes in Ethereum:

  • The EVM
  • Mining nodes (Ethereum 1.0)
  • Validators (Ethereum 2.0)

Please note that this distinction is made to clarify concepts of Ethereum. In most scenarios, there is no dedicated EVM. Instead, all nodes act as miners as well as EVM nodes.

EVM

Think of an EVM as the execution runtime for smart contracts. EVMs are primarily responsible for providing a runtime that can execute code written in smart contracts. It can access accounts, both contract and externally owned, and its own storage data. It does not have access to the overall ledger but does have limited information about the current transaction.

EVMs are the execution components in Ethereum. The purpose of an EVM is to execute code in a smart contract line by line. However, when a transaction is submitted, the transaction is not executed immediately. Instead, it is added to a transaction pool. These transactions are not yet written to the Ethereum ledger.

Mining nodes

The mining nodes are responsible for generating, validating, and adding blocks to the chain. Mining nodes can be a full node, a light node, or an archive node. A full node has all the blocks (headers and transactions) since the genesis block and complete global state and can participate in generating and validating the blocks. Light nodes have blocks with headers only and are dependent on connected full nodes for any information they need. They require more bandwidth because of their chatty nature with full nodes but need less computing and storage. They are also faster while synchronizing blocks from full nodes. Archival nodes are again full nodes, but their usage is more for querying a node for historical information and reporting.

Ethereum validators

Validator nodes are again nodes responsible for generating new blocks for the chain. They do not run any mining process, and they collect all available transactions from the transaction pool and create a block. They broadcast the blocks to other validators for attestation. As part of the attestation process, validators execute each transaction and modify their state while adding a block to their chain.

Ethereum accounts

Accounts are the main building blocks for the Ethereum ecosystem. It is an interaction between accounts that Ethereum wants to store as transactions in its ledger. There are two types of accounts available in Ethereum – externally owned accounts and contract accounts. Each account, by default, has a property named balance that helps in querying the current balance of Ether.

Externally owned accounts

Externally owned accounts are accounts that are owned by people on Ethereum. Accounts are not referred to by name in Ethereum. When an externally owned account is created on Ethereum by an individual, a public/private key is generated. The private key is kept safe with the individual while the public key becomes the identity of this externally owned account. This public key is generally of 256 characters; however, Ethereum uses the first 160 characters to represent the identity of an account.

If Bob, for example, creates an account on an Ethereum network, whether private or public, he will have his private key available to himself while the first 160 characters of his public key will become his identity. Other accounts on the network can then send Ether or other cryptocurrencies based on Ether to this account.

An account on Ethereum looks like the one shown in the following screenshot:

Figure 1.8 – An externally owned account identifier

Figure 1.8 – An externally owned account identifier

An externally owned account can hold Ether in its balance and does not have any code associated with it. It can execute transactions with other externally owned accounts, and it can also execute transactions by invoking functions within contracts.

Contract accounts

Contract accounts are very similar to externally owned accounts. They are identified using their public address. They do not have a private key. They can hold Ether similar to externally owned accounts; however, they contain code for smart contracts consisting of functions and state variables.

Externally owned accounts are responsible for initiating transactions and each transaction in Ethereum requires gas, which is the cost of the transaction to be provided by the sender of the transaction. Gas has a relationship to its currency known as Ether. The next section will discuss the concepts related to Ether, gas, and transactions.

Ether, gas, and transactions

Ether is the currency of Ethereum. Every activity on Ethereum that modifies its state charges Ether as a fee. And miners who are successful in generating and writing a block in a chain are also rewarded Ether. Ether can easily be converted to dollars or other currencies through crypto exchanges.

Ethereum has a metric system of denominations known as units of Ether. The smallest denomination or base unit of Ether is called wei. The following is a list of the named denominations and their value in wei, which is available at https://github.com/ethereum/web3.js/blob/0.15.0/lib/utils/utils.js#L40:

var unitMap = {
   'wei' : '1'
   'kwei': '1000',
   'ada': '1000',
   'femtoether': '1000',
   'mwei': '1000000',
   'babbage': '1000000',
   'picoether': '1000000',
   'gwei': '1000000000',
   'shannon': '1000000000',
   'nanoether': '1000000000',
   'nano': '1000000000',
   'szabo': '1000000000000',
   'microether': '1000000000000',
   'micro': '1000000000000',
   'finney': '1000000000000000',
   'milliether': '1000000000000000',
   'milli': '1000000000000000',
   'ether': '1000000000000000000',
   'kether': '1000000000000000000000',
   'grand': '1000000000000000000000',
   'einstein': '1000000000000000000000',
   'mether': '1000000000000000000000000',
   'gether': '1000000000000000000000000000',
   'tether': '1000000000000000000000000000000'
};

Gas

In the previous section, it was mentioned that fees are paid using Ether for any transaction execution that leads to a state change in Ethereum. Ether is traded on public exchanges, and its price fluctuates daily. If Ether is used for paying fees, then the cost of using the same service can be high on a certain day while being low on other days. People will wait for the price of Ether to fall to execute their transactions. This is not ideal for a platform such as Ethereum. Gas helps in alleviating this problem. This is the internal currency of Ethereum. The execution and resource utilization costs are predetermined in Ethereum in terms of gas units. For each unit of gas, a price can be ascribed, known as the gas price. For example, for each unit of gas, a price of 10 gwei can be assigned. When both the number of gas units and gas price are multiplied together, it results in the gas cost. The gas price can be adjusted to a lower price when the price of Ether increases and a higher price when the price of Ether decreases.

Transactions

A transaction is an agreement between a buyer and a seller, a supplier and a consumer, or a provider and a consumer that there will be an exchange of assets, products, or services for currency, cryptocurrency, or some other asset, either in the present or in the future. Ethereum helps in executing the transaction. The following are the three types of transactions that can be executed in Ethereum:

  • The transfer of Ether from one account to another: The accounts can be externally owned accounts or contract accounts. The following are the possible cases:
    • An externally owned account sending Ether to another externally owned account in a transaction
    • An externally owned account sending Ether to a contract account in a transaction
    • A contract account sending Ether to another contract account in a transaction
    • A contract account sending Ether to an externally owned account in a transaction
  • Deployment of a smart contract: An externally owned account can deploy a contract using a transaction in an EVM.
  • Using or invoking a function within a contract: Executing a function in a contract that changes state is considered a transaction in Ethereum. If executing a function does not change a state, it does not require a transaction.

A transaction has some of the following important properties related to it:

  • The from account property denotes the account that originates the transaction and represents an account that is ready to send some gas or Ether. Both gas and Ether concepts were discussed earlier in this chapter. The from account can be externally owned or a contract account.
  • The to account property refers to an account that is receiving Ether or benefits in lieu of an exchange. For transactions related to the deployment of the contract, the to field is empty. It can be externally owned or a contract account.
  • The value account property refers to the amount of Ether that is transferred from one account to another.
  • The input account property refers to the compiled contract bytecode and is used during contract deployment in an EVM. It is also used for storing data related to smart contract function calls along with their parameters. A typical transaction in Ethereum where a contract function is invoked is shown here. In the following screenshot, note the input field containing the function call to contract, along with its parameters:
Figure 1.9 – The transaction properties in a block

Figure 1.9 – The transaction properties in a block

  • The blockHash account property refers to the hash of the block to which this transaction belongs.
  • The blockNumber account property is the block to which this transaction belongs.
  • The gas account property refers to the amount of gas supplied by the sender who is executing this transaction.
  • The gasPrice account property refers to the price per gas the sender was willing to pay in wei (we learned about wei in the Ether section earlier in this chapter). Total gas is computed at gas units x gas price.
  • The hash account property refers to the hash of the transaction.
  • The nonce account property refers to the number of transactions made by the sender prior to the current transaction.
  • The transactionIndex account property refers to the serial number of the current transactions in the block.
  • The value account property refers to the amount of Ether transferred in wei.
  • The v, r, and s account properties relate to digital signatures and the signing of the transaction.

A typical transaction in Ethereum, where an externally owned account sends some Ether to another externally owned account, is shown here. Note that the input field is not used here. Since two Ethers were sent in the transaction, the value field is showing the value accordingly in wei, as shown in the following screenshot:

Figure 1.10 – Block properties in a block

Figure 1.10 – Block properties in a block

One method to send Ether from an externally owned account to another externally owned account is shown in the following code snippet using the web3 JavaScript framework, which will be covered later in this book:

web.eth.sendTransaction({from: web.eth.accounts[0], to: "0x9d2a327b320da739ed6b0da33
c3809946cc8cf6a", value: web.
toWei(2, 'ether')})

A typical transaction in Ethereum where a contract is deployed is shown in the following screenshot. In the following screenshot, note the input field containing the bytecode of the contract:

Figure 1.11 – A sample transaction in a block

Figure 1.11 – A sample transaction in a block

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Learn proven smart contract implementation challenges and solve them using Solidity
  • Go deeper into Solidity to write effective upgradable and maintainable smart contracts using best practices
  • Get to grips with the latest version of Solidity with updated codes and examples

Description

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.

Who is this book for?

This Ethereum book is primarily aimed at beginners who want to get started with Solidity Programming for developing an Ethereum smart contract. No prior knowledge of EVM is required, but knowing the basics of any programming language will help you follow along.

What you will learn

  • Write efficient, effective, and secure smart contracts
  • Code, compile, and test smart contracts in an object-oriented way
  • Implement assembly code in Solidity
  • Adopt upgradable and haltable ownership and security design patterns
  • Understand exception handling and debugging in Solidity
  • Create new ERC20 and NFT tokens from the ground up
Estimated delivery fee Deliver to Slovakia

Premium delivery 7 - 10 business days

€25.95
(Includes tracking information)

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Jun 10, 2022
Length: 412 pages
Edition : 2nd
Language : English
ISBN-13 : 9781803231181
Category :
Languages :
Concepts :
Tools :

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Estimated delivery fee Deliver to Slovakia

Premium delivery 7 - 10 business days

€25.95
(Includes tracking information)

Product Details

Publication date : Jun 10, 2022
Length: 412 pages
Edition : 2nd
Language : English
ISBN-13 : 9781803231181
Category :
Languages :
Concepts :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 104.97
Building Python Microservices with FastAPI
€35.99
Solidity Programming Essentials
€31.99
Blockchain Development for Finance Projects
€36.99
Total 104.97 Stars icon
Banner background image

Table of Contents

20 Chapters
Part 1: The Fundamentals of Solidity and Ethereum Chevron down icon Chevron up icon
Chapter 1: An Introduction to Blockchain, Ethereum, and Smart Contracts Chevron down icon Chevron up icon
Chapter 2: Installing Ethereum and Solidity Chevron down icon Chevron up icon
Chapter 3: Introducing Solidity Chevron down icon Chevron up icon
Chapter 4: Global Variables and Functions Chevron down icon Chevron up icon
Chapter 5: Expressions and Control Structures Chevron down icon Chevron up icon
Part 2: Writing Robust Smart Contracts Chevron down icon Chevron up icon
Chapter 6: Writing Smart Contracts Chevron down icon Chevron up icon
Chapter 7: Solidity Functions, Modifiers, and Fallbacks Chevron down icon Chevron up icon
Chapter 8: Exceptions, Events, and Logging Chevron down icon Chevron up icon
Chapter 9: Basics of Truffle and Unit Testing Chevron down icon Chevron up icon
Chapter 10: Debugging Contracts Chevron down icon Chevron up icon
Part 3: Advanced Smart Contracts Chevron down icon Chevron up icon
Chapter 11: Assembly Programming Chevron down icon Chevron up icon
Chapter 12: Upgradable Smart Contracts Chevron down icon Chevron up icon
Chapter 13: Writing Secure Contracts Chevron down icon Chevron up icon
Chapter 14: Writing Token Contracts Chevron down icon Chevron up icon
Chapter 15: Solidity Design Patterns Chevron down icon Chevron up icon
Assessments Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.6
(8 Ratings)
5 star 37.5%
4 star 25%
3 star 12.5%
2 star 12.5%
1 star 12.5%
Filter icon Filter
Top Reviews

Filter reviews by




Aditya Aug 02, 2022
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Only Available book,focused Solidity in depth...unlike every other book which focuses on everything about dapp but not explaining the solidity only.
Amazon Verified review Amazon
POE Jul 01, 2022
Full star icon Full star icon Full star icon Full star icon Full star icon 5
If you are like me and want to get into the world of Blockchain, this book is a great starting point. The author breaks down the concepts and terminology of Blockchain early in the book, so the rest of it will make more sense. Ethereum, an open source blockchain technology for digital currency, is explained along with related concepts of hashing and encryption. The more complex topics of Solidity and smart contracts are covered in great detail. These contracts seem to be the main point of the book as the majority of the chapters detail specific aspects of smart contracts. The author even included sections on assembly programming, and Non-Fungible Tokens (NFTs).
Amazon Verified review Amazon
VIRAJ PATVA Oct 06, 2022
Full star icon Full star icon Full star icon Full star icon Full star icon 5
The book covers all the aspects for Solidity Developer at one place. When I was new to solidity programming I faced a issue that I wasn't able to get good content curated at a place. This book very helpful to start career as Solidity Developer.The book didn't cover Web3.js / Ether.js that is required for Dapp development. Anyways author can write out other book on Dapp Development
Amazon Verified review Amazon
luca r. Oct 09, 2022
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
This book contains all the essentials in order to get you from 0 to hero. I have taken multiple solidty courses and this by far gives the best foundations in order to understand bigger and more complex projects. Since solidty and blockchain programming have been recently in the rise, if this interests you I would 10/10 recommend having this book.
Amazon Verified review Amazon
Josh Oct 05, 2022
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
As someone that has written smart contracts before, this book covers all the basics and some advanced topics. The descriptions are not too clear in some parts, but the code examples are there to help. It's programming so you're going to learn best by doing and using the writing as a guide.That being said I wouldn't rely on this (or any other source) as a complete guide to learning Solidity. It's a more advanced language so it's better to start with another language before picking this up.Overall not bad. The author introduces the reasons behind some common practices that will make you a better developer and ensures you're ready to write a wide range of contracts with practice of the concepts introduced in the book. Would recommend.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact customercare@packt.com with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at customercare@packt.com using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on customercare@packt.com with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on customercare@packt.com within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on customercare@packt.com who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on customercare@packt.com within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela