Editors note: In the first part of our blockchain blogs Pascal wrote about dApps and the basics of blockchain technology. In the second part it gets more technical: not only will he explain what a Smart Contract is, but also show you how to set one up in a tutorial.
Smart Contracts and Ethereum
At the core of the Ethereum blockchain lies the concept of smart contracts. The original motivation for Ethereum actually stems from Bitcoin’s scripting limitations, which provide limited support for smart contracts.
Okay, understood, but what does that mean exactly?
Well, Bitcoin has limited support for smart contracts since its scripting language is non-Turing-complete, in part due to its lack of loops. This means that there are many applications that can be programmed on Ethereum by developers, but not on Bitcoin. Technically speaking, Ethereum has been designed from the beginning as “a platform for the deployment and execution of smart contracts”.
What does Turing-complete mean?
Ethereum’s Turing-complete language is originally based on Alan Turing’s concept of an Universal Turing machine (UTM). Simply put, a Turing machine is a hypothetical machine invented by the mathematician Alan Turing in 1936. Despite its simplicity, the machine can simulate any computer algorithm, no matter how complicated it is. And the UTM is nothing more than a Turing machine that simulates an arbitrary Turing machine on arbitrary input. However, since Ethereum implements the concept of a gas limit, developers cannot run programs that require more gas than the gas limit. Thus, the Ethereum yellow paper calls the Ethereum Virtual Machine, the EVM, a “quasi-Turing-complete machine”. In spite of many similarities, Ethereum is not really Turing-complete, since developers cannot program anything they want without gas.
What is a Smart Contract?
Let us therefore quickly take a look at the definition of a smart contract:
- A smart contract is a self-executing contract with the terms of the agreement between a buyer and a seller being directly written into lines of code.
- The code and the agreements contained therein exist across a distributed, decentralised blockchain network.
- The code controls the execution, and transactions are trackable and irreversible.
Everything grasped from a theoretical perspective? Great, let us get hands-on. For those who want to delve even deeper into the history of Ethereum and smart contracts I recommend this blog post.
A Smart Contract Tutorial
The purpose of this tutorial is to show you one of the easiest ways to deploy a smart contract on Ethereum. And in doing so, I will follow a certain structure that is common in the realm of smart contract development. First, we deploy the smart contract locally on a personal blockchain (I will use Ganache). Second, we deploy the smart contract on one of the designated test networks (I will use the Rinkeby test network). Third, we deploy the smart contract on the main network.
As the development framework I am using Truffle. Truffle is a development environment, testing framework, and asset pipeline for Ethereum, aiming to make life as an Ethereum developer easier.
1) Preparation: Install the Necessary Tools
We need some basic tools to get started properly. First of all we download Node.js. It must be at least version 8.9.4. I also assume that we use Windows, Linux, or Mac OS X. Once we are ready, we open our shell and run the following command: