Ethereum For Dummies
Book image
Explore Book Buy On Amazon
Ethereum uses smart contracts to constrict data to particular rules when you make exchanges. In order to write smart contracts, you need to be proficient in a programming language. One of the most popular languages for smart contracts and Ethereum, in particular, is Solidity. Take a look for a brief intro to these topics.

Basics of Ethereum smart contracts

When you exchange items of value, generally rules govern how the transaction takes place. In many cases, the rules are simple. For example, you give Joe $1.89, and Joe gives you a soft drink. Each party can see and validate the other party’s contribution to the transaction. If you try to give Joe Monopoly money, you won’t get your soft drink.

Even though this transaction seems simple, there's more to it than meets the eye. In most cases, if a soft drink costs $1.89, you’ll have to tender more than that for it. You’ll have to pay taxes as well. So there's another participant in the transaction: the government. Instead of keeping all the money, Joe has to send some of it to the government for taxes.

Moving even simple transactions like the soft drink example into the digital world takes some careful thought. You can’t just send money to people and trust that they’ll do their part. You need some way to enforce rules and compliance to make sure that all parties are treated fairly.

Smart contracts help you enforce rules when you exchange anything of value in Ethereum. The simplest way to describe smart contracts is that they are programs that execute when certain transactions occur.

For example, if you create a soft-drink-purchase smart contract, that software code will run every time someone buys a soft drink. The smart contract code is stored in the blockchain, so all nodes have a copy of it. Also, it doesn’t matter where the software runs: All nodes are guaranteed to run it the same and get the same results as every other node.

Ethereum smart contracts are Turing complete, which means they can compute anything that is computable with enough resources. Turing completeness is important because Ethereum smart contracts aren’t limited in the types of actions they can carry out. They can carry out any complex algorithms you can design.

The soft-drink smart contract starts with the buyer. Here’s how the exchange might happen:
  • The buyer creates a transaction that sends money to the seller in exchange for the soft drink.
  • The buyer sends the seller’s address as input to the smart contract’s address.
  • The smart contract runs to carry out the transaction. It verifies that you have enough money in your account to pay for the soft drink.
  • The smart contract verifies that the seller has the soft drink you want in stock.
  • The smart contract deducts funds from the buyer, sends the funds to the seller, and tells the seller to send the soft drink to the buyer. In the same step, the smart contract sends the required tax to the tax authority account and sends the remaining amount to the seller’s account.
The process may seem tedious, but it is straightforward and makes sure each transaction occurs in the same way. This example is too simple for real-life exchanges, and some important details have been left out. For starters, you assume that the seller will send the soft drink to the buyer. Real-life exchanges require an extra layer of protection for both sides. Smart contracts use escrow accounts all the time to hold a buyer’s money until the seller delivers the goods or services.

Smart contracts provide the governance and predictability of Ethereum. Without them, Ethereum would just be a cool distributed storage technique. But with them, Ethereum is a stable decentralized platform that supports interactions and exchanges between untrusting users, including extremely complex transactions. It is easy to see the steps necessary to buy a soft drink.

Other transactions, such as real estate transactions, are far more complex, have many dependencies and requirements, and generally involve several people and organizations. Ethereum smart contracts can help developers create software that eliminates middlemen, streamlines complex processes, and reduces the overall cost and time required to complete even the most complex exchanges.

Introducing Solidity, the language of smart contracts

Smart contracts are software programs. With enough resources, smart contracts can do anything any other software can do. You can write Ethereum smart contracts in several languages:
  • Mutan: An older smart contract language that was deprecated in 2015.
  • LLL: A Lisp-like language, obviously developed to look like the language Lisp. Although LLL is still supported, it's not used for many current smart contract projects.
  • Serpent: A language that looks like the Python language. As of September 2017, Serpent is not recommended for current development.
  • Bamboo: A relatively new language that focuses on making blockchain state transitions explicit and avoiding code reentrant issues.
  • Viper: Another relatively new language that focuses on security and simplicity.
  • Solidity: Currently the most popular smart contract development language. Solidity looks like the JavaScript language and provides a full-featured language for developing general-purpose smart contracts.

Solidity is the most popular language for smart contracts, and the one you're most likely to encounter.

If you’re comfortable with JavaScript, picking up Solidity will be a little easier. If you don’t know much JavaScript, that’s okay. You’re going to learn the basics of Solidity from the ground floor. In fact, let’s start with a program that may look familiar: the ubiquitous “Hello world” program.

Take a look at this very simple smart contract code:

pragma solidity ^0.4.25;
contract helloWorld {
function printHelloWorld () public constant returns (string) {
return 'Hello world!';
}
}
That’s what a Solidity smart contract looks like! After the heading, you define your contract, and then any functions that make up the inner workings of the program. After you write and test a smart contract, you can deploy it to a blockchain, and then execute it. When you get everything right, your smart contract will show you the iconic “Hello world!” message.

As you learn more about Solidity, you’ll see that it does look a lot like JavaScript but also feels a bit like C++ and Python. The developers of Solidity based the language on all three languages. It supports inheritance, libraries, and user-defined types that can be quite complex. It is also a statically typed language, which means you have to provide explicit datatypes for the variables you create and use.

Above all, Solidity is a smart contract development language. Even though it looks like other languages, it includes primitives and an orientation designed to interact with the Ethereum blockchain.

Having a firm understanding of smart contracts and Solidity will make your interactions with Ethereum much easier.

About This Article

This article is from the book:

About the book author:

Michael G. Solomon, PhD, is a full-time security, privacy, blockchain, and data science expert. An active speaker, consultant, and author, Michael is a professor of cybersecurity and global business with blockchain technology at the University of the Cumberlands. He has written more than 20 books on IT, security, and the PMP exam.

This article can be found in the category: