Lesson 2

# ÐApps & Smart Contracts

9

## What is a smart contract?

A smart contract is a piece of code that's been uploaded to the blockchain. The code contains some public functions, which can be called by anyone who interacts with the blockchain from their computer.

The difference between interacting with a smart contract and interacting with something like a REST API, is that there's not just one server performing the requested actions. Instead, it's a network of computers, all verifying each other, making sure that no single party is trying to cheat the system.

This decentralisation is what allows people to build so-called DApps ("Decentralised apps") that have some unique features compared to traditional web apps.

For instance, smart contracts are immutable – once deployed to an Ethereum address, they can never be changed. This enforces the trust that users have when interacting with smart contracts, they know that the function they call will always behave in the same way, regardless of what happens to the user or company that wrote it.

It also introduces some important constraints to us as developers however. Today, a web programmer is pretty much expected to ship buggy software, and tweaking it afterwards as user feedback comes in. When writing smart contracts, you have to plan much more ahead, and decide in advance what parts of the contract should be replaceable and which ones should not. You could almost say that writing a smart contract is more comparable to building a house, rather than hacking together a web app.

## The ether currency

An important thing to remember when it comes to blockchain platforms is the fact that running any smart contract function is inevitably going to use quite a lot of resources. After all, even a simple operation needs to be duplicated across thousands of computers all around the world.

In order for these computers to be incentivised to run these operations, you'll need to send them a certain amount of money. This money is denominated in Ethereum's own currency – ether.

Every ether can be divided into one quintillion (!) subunits called wei. You can think of them as cents to the dollar – they are the Ethereum network's smallest currency denomination. Below you'll find a list of all the available denominations:

UnitAmount of wei
wei$1$
KWei (kilowei)$1,000$ ( a thousand wei)
MWei (megawei)$1,000,000$ (one million wei)
GWei (gigawei)$1,000,000,000$ (one billion wei)
µEth (microether)$1,000,000,000,000$ (1 million µEth = 1 eth)
mEth (milliether)$1,000,000,000,000,000$ (1000 mEth = 1 eth)
eth (ether)$1,000,000,000,000,000,000$

Don't worry, you don't need to learn this list by heart, but it definitely helps to roughly remember how the units relate to each other. If you ever need to convert from one denomination to another, I recommend using this site.

## Gas

So how can you estimate how much ether you need to send in order to incentivise the nodes in the network to perform your operation? This is where the concept of gas comes in.

Every programming operation in a smart contract, be it an addition, a subtraction or a for-loop, uses a certain amount of gas. How much gas each operation uses can be found in tables such as this one. Here are the first three rows of that table:

OperationsGas usedDescription
stop$0$Halts execution.
AND3Addition operation.
MUL5Multiplication operation.

As an example, let's imagine that we have a function like this in our smart contract:

Granted, this function is pretty useless, but it helps us understand how to calculate gas. In this example, we perform one addition and one multiplication. Therefore, according to the table above, the amount of gas required to run the doMath function is $3+5=8$.

In addition to this, there's also a minimum amount of $21 000$ gas for any kind of Ethereum transaction, so the total is actually $21008$.

By classifying operations in this way, we will always be able to calculate how much gas an Ethereum function call uses, which is a good first step when evaluating how much ether it costs.

## How Ether relates to gas

When spending gas on smart contracts, we also set a gas price. Gas prices are usually measured in GWei (pronounced "gigawei"). Therefore, it helps to know that 1 eth is equivalent to 1 billion GWei (as seen in the table of denominations above).

The price of gas is a dynamic market which adjusts according to supply and demand. If many people are trying to use the network at once, and the number of miners is limited, the gas price will naturally increase.

We can see what the current price of gas is on websites like EthGasStation. At the time of writing, 1 gas ≈ 3 GWei. If we wanted to determine the amount of ether needed to make the most basic type of transaction (which uses 21000 gas), we can calculate it by doing:

$\text{Amount\ of\ gas\ used} * \text{Price\ per\ gas\ in\ GWei\ }/\text{\ GWei\ per\ ether}$
$3*21,000/1,000,000,000 = 0.000063$

At the time of writing, the price of ether is around $200, which means that the average cost for a transaction in US dollars is around $0.000063*200=0.0126$. So$0.01 for one transaction. Pretty cheap in other words!

Now that you understand the relationship between smart contracts, gas and ether, let's look into some code!