Book Image

Solidity Programming Essentials

Book Image

Solidity Programming Essentials

Overview of this book

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.
Table of Contents (17 chapters)
Title Page
Copyright and Credits
Packt Upsell
Contributors
Preface
Index

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. Following are the three types of transactions that can be executed in Ethereum:

  • Transfer of Ether from one account to another: The accounts can be externally owned accounts or contract accounts. 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 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 is originating 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.
  • Thetoaccount property refers to an account that is receiving Ether or benefits in lieu of an exchange. For transactions related to deployment of contract, thetofield is empty. It can be externally owned or a contract account.
  • Thevalueaccount property refers to the amount of Ether that is transferred from one account to another.
  • Theinputaccount property refers to the compiled contract bytecode and is used during contract deployment in EVM. It is also used for storing data related to smart contract function calls along with its parameters. A typical transaction in Ethereum where a contract function is invoked is shown here. In the following screenshot, notice the inputfield containing the function call to contract along with its parameters:
  • The blockHash account property refers to the hash of block to which this transaction belongs.
  • The blockNumber account property is the block in 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 have already learned about wei in the Ether section in this chapter). Total gas is computed at gas units * 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. Notice the input field is not used here. Since two Ethers were sent in transaction, the value field is showing the value accordingly in wei as shown in the following screenshot:

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

web.eth.sendTransaction({from: web.eth.accounts[0], to: "0x9d2a327b320da739ed6b0da33c3809946cc8cf6a", 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, notice the input field containing the bytecode of contract: