Learn the main language of smart contracts, Solidity, and how you can use its syntax to make your dApps provide value to its users. And establish a healthy respect for each of the steps in the Ethereum dApp development process so you will have more effective software.
Ethereum and blockchain quick facts
Blockchain technology is a fast-growing disruptive technology that enables data to be shared among a set of untrusted parties. Moreover, blockchain makes it possible to share ledgers of items of value and control the exchange of these items in an untrusted environment.
Ethereum is the most popular blockchain implementation and supports complex software applications.
Learning about blockchain technology with Ethereum will help you understand how people and organizations will conduct business in the near future. The following list summarizes some of Ethereum’s most important features:
- A blockchain is a chain of blocks, where each block stores the Keccak-256 hash value of the previous block, connecting it to its predecessor.
- Ethereum uses the Keccak-256 hashing algorithm to calculate hash values for blocks.
- The Ethereum Virtual Machine (EVM) is the runtime environment that executes on each Ethereum node and carries out smart contract instructions.
- Any changes to a block invalidate that block and all subsequent blocks in the chain.
- At startup and then periodically, each EVM instance verifies that the local blockchain is valid.
- Smart contracts must run the same way, and produce the same results, on every EVM instance.
- Each blockchain operation in a smart contract requires an amount of gas, or cryptocurrency, to complete.
- Each transaction has a gas budget.
- If a smart contract runs out of gas for the transaction, the transaction fails to complete and is restored to the starting state.
- Ethereum uses the Proof of Work (PoW) consensus algorithm to validate new blocks for the blockchain. PoW pays a reward for a node called a miner that solves a difficult math puzzle to find a hash value for a block that meets complexity standards.
- Databases support Create, Read, Update, and Delete (CRUD) operations, whereas blockchains support only Read and Write operations.
- Crypto-asset owners are identified by an Ethereum address, which is a truncated portion of their Ethereum public encryption key.
- The most common use of a blockchain is to transfer some item of value from one account to another.
Ethereum solidity quick syntax
The real power of Ethereum is in the combination of distributed trusted data and the operations you can perform on that data. Smart contracts provide consistent and trusted methods to add new data to the Ethereum blockchain and to read existing data.
The most popular language for Ethereum smart contracts is Solidity, which looks a lot like JavaScript and is designed to be easy to understand and use. The following list summarizes some of the most common Solidity keywords you’ll use to write smart contracts for Ethereum:
pragma solidity ^0.4.25;
tells Solidity which versions of the compiler should compile this file.contract contractName {
identifies the start of a smart contract and gives it a name.import “myToken.sol”;
imports the contents of another file, in this case myToken.sol, to give the compiler access to the code in another file.function functionName {
identifies the start of a function in a smart contract and gives it a name.]
closes any block, including contracts, functions, flow control, and conditional execution blocks.uint
andint
are data types for variables that store integer data.byte
andstring
are data types for variables that store character data.bool
is a data type for variables that store logical (yes/no) data.address
is a dData type for variables that store Ethereum addresses.mapping
is a dData type for variables that store data associated with key values and support lookup by key value.public
is a visibility modifier that makes a variable or function accessible by anyone.external
is a visibility modifier that makes a variable or function only accessible to external functions.internal
is a visibility modifier that makes a variable or function accessible only to functions in the current contract or any contract derived from the current contract.private
is a visibility modifier that makes a variable or function accessible only to functions in the current contract.if else
is a conditional statement that executes code based on a condition being true.while
executes a group of statements zero or more times, based on a condition.do while
executes a group of statements one or more times, until a condition is true.for
executes a group of statements zero or more times, until a condition is true, with the condition defined in the header.revert()
,assert()
, andrequire()
are functions that undo changes in a transaction and return control to the caller.
Ethereum smart contract development process
The process of developing, testing, and deploying smart contracts in Ethereum is consistent, regardless of how the size or complexity of your project. Follow the same basic steps and plan for each phase to ensure that your smart contracts provide the most effective software solution.
The following list summarizes the steps you’ll take during the development and deployment phase for smart contracts using Geth, Truffle, and Ganache:
- Initialize a new Truffle:
mkdir projectName ; cd projectName ; truffle init
- Create test cases:
truffle create test projectName
- Write smart contact code in Solidity and test cases in JavaScript: use your favorite IDE or editor
- Launch Ganache
- Compile all your code:
truffle compile --all --network development
- Deploy your code to your development blockchain (Ganache):
truffle deploy --reset --network development
- Test your code:
truffle console
(for manual tests) ortruffle test
(for automated test cases) - Fix any errors, redeploy, and then retest
- Choose a test network (such as Ropsten, Rinkeby, or Kovan)
- Fill up with gas: Get enough gas to test your code from test network faucets.
- Compile all your code:
truffle compile --all --network test
- Deploy your code to your test blockchain:
truffle deploy --network test
- If all tests pass, deploy to the live network
- Compile all your code:
truffle compile --all --network live
- Deploy your code to the live blockchain:
truffle deploy --network live