2

What are Smart Contracts? [The true power of blockchain] – Part 1

What are Smart Contracts? This article describes how cryptocurrencies are only the tip of the iceberg when it comes to decentralization, the many applications of the blockchain technology (besides a peer-to-peer cash system), what are smart contracts and how they relate to the Ethereum platform, the ERC20 token standard and Ether. It also shows how to start setting up a development environment for writing smart contracts under a local blockchain through the use of the Ganache framework; this is usually done before moving to the contract to the real Ethereum blockchain.


Blockchain, the technology behind cryptocurrencies like Bitcoin, Ether, XRP and many more (1500+), is here to change the digital world as we know it, surely it is still evolving and mass adoption has been a difficult barrier to cross, but, cryptocurrencies are just the tip of the iceberg of what can really be achieved with blockchain.

Bitcoin was conceived as a peer-to-peer electronic cash system, and that’s probably why so many cryptocurrencies have popped out in the last couple of years. The concepts of peer-to-peer and decentralization through blockchain have many other applications though.

Cryptocurrencies are but one use case of blockchain, many other decentralized applications are possible.

Storage, encrypted messaging, document verification, digital assets, smart contracts and voting systems are only some of the many applications of this technology, that can (and will) take advantage of the integrity and transparency that can be achieved with blockchain.

One cool application of blockchain I saw in the news a few days back, was for keeping record of the identities of a refugee camp in Siria, a facial scan of the person was taken, recorded and stored into a blockchain. Once in there, the record cannot be altered, so, only after verifying the identity of the person against the blockchain, the refugee (and only him/her) will be able to get the required help. Blockchain can also be used for authentication purposes.


Smart contracts

A smart contract is a digitally singed agreement between two peers, the agreement is written in a programming language, evaluated/executed by the computer code and immutable (once it has entered the blockchain). They are usually time-dependent and, at times, they are also known as self-executing contracts.

smart-contracts

What is Ethereum?

Ethereum “is a decentralized platform that runs smart contracts: applications that run exactly as programmed without any possibility of downtime, censorship, fraud or third-party interference”. Ethereum is a blockchain-based platform, similar to Bitcoin, with the difference that it greatly expands the ability to create and execute code on it.

The Bitcoin blockchain allows the creation and execution of smart contracts, however, it was not really envisioned for this task and the code you can write is usually limited (although there has been some improvements in the last couple of years). Ethereum, on the other side, was created specifically for the purpose of dealing with smart contracts.

No wonder why decentralized application developers these days prefer to use a more flexible platform like Ethereum. This is also, I argue, why we have seen many Ethereum-based projects arising these days.


What is ERC20?

ERC20 is an Ethereum crypto token standard that “describes the functions and events that an Ethereum token contract has to implement”. It basically defines a standard set functionality that a smart contract has to implement in order to be considered ERC20-compliant.

erc-20-ethereum-standard

These functions and events are usually specified through a programming language, Solidity been one of the most used languages for this purpose. In a future article we will see how to use Solidity to create an Ethereum-based smart contract.


What is Ether?

Ether is the underlying crypto token that fuels the Ethereum blockchain. Unlike Bitcoin (which was specifically designed as a peer-to-peer cash system), the purpose of Ether is not to be a unit of currency, although it has price and it is traded on public markets, instead, Ether is the “fuel” or “gas” the powers decentralized applications in the Ethereum network.

crypto-1oz-ethereum-obv

Starting with Smart Contracts

Below we will see how to install some of the dependencies required to start writing our own Ethereum smart contracts. We start by installing nodejs, a Javascript runtime for executing Javascript code outside of the browser. And then we download and install Ganache, a personal Ethereum blockchain.


Installing NodeJS

To install the latest version of nodejs, first you need to add the repository, you can do that by typing the next command into a console:

curl -sL https://deb.nodesource.com/setup_10.x | bash -
curl node setup

Then you just use apt-get to install nodejs:

apt-get install nodejs

Running the Ganache app

Now, we want to use part of the Truffle Framework known as Ganache, a personal Ethereum blockchain running in Javascript, that you can use “to run tests, execute commands, and inspect states while controlling how the chain operates”. It’s the perfect tool for writing your first smart contract.

If you are using a Linux-based environment, you can download the AppImage from here (an AppImage, as advertised, is a Linux app that runs in most Linux-based environment, they can be compared to an .exe file on Windows).

After downloading the AppImage, give it execution permissions:

chmod 755 ganache-1.2.1-x86_64.AppImage

And run it like you would with any other executable (a message might come up asking whether to add Ganache to your application menu, I choose No):

./ganache-1.2.1-x86_64.AppImage

You would see the Ganache GUI come up. Notice that there are 10 blockchain addresses that you can use to play and test your smart contract against (more to come on how to do this). Also, if you pay attention to you computer’s processor at this time, you might hear the fan speed up, since Ganache is set to automining and it is using you CPU’s power for that.

ganache - What are smart contracts

Each account has a balance of 100ETH, but before you think you hit the jackpot, remember that this is a simulated  blockchain, not the real thing and that is fake ether.

Writing your first Smart Contract. [The true power of blockchain] – Part 2