# Ethereum Smart Contracts

October 18, 2017

In the last section we learned that the software and apps executed by Ethereum Virtual Machine are called smart contracts. A human being and a smart contract are both seen as users on Ethereum. Whatever a human user can do, a smart contract can do too, and then some.

Smart contracts act exactly like any other human user in the network. Both of them can send and receive ether just like any other currency.

But unlike human users, smart contracts can also execute a predefined computer program to perform various actions when triggered. To understand the power of a smart contract, let’s consider an example.

# Power of smart contracts

Imagine you and I place a bet about tomorrow’s weather. I bet that it’ll be sunny tomorrow while you bet it’ll be rainy. We agree that the loser has to give the winner $100. How can we do this and ensure that the loser will keep his promise? I can think of three distinct ways: 1) Trust each other The simplest way to do this would be to trust each other. If we’ve been friends for a long time now, trusting each other is pretty easy. I know where you live and you know all kinds of embarrassing stuff about me. But things get more difficult if we’re complete strangers. You have no reason to trust me and I have no reason to trust you. 2) Sign a legal agreement Another plausible way is to formulate our bet as a legal agreement. We’ll both sign an agreement that defines all the terms of our bet in detail — including what happens if the loser violates the agreement. The agreement would makes us legally obligated to pay the winner, but it wouldn’t serve any practical purpose because forcing the agreement through the legal route would be more expensive than what the bet was worth. 3) Get help from a mutual friend We could find a mutual friend who we both trust and we’d both give him/her$100 each for safekeeping. The nextday, he/she would check the weather and give the total$200 to whoever won the bet. Simple and easy, except that it isn’t: What if the trusted friend runs away with$200?

Now we have three different ways of doing the bet but each option has its shortcomings. Because we are strangers, we cannot trust each other. Forcing a legal agreement will be so expensive that it wouldn’t be practically feasible. Getting help from a mutual friend brings up the question of trust again.

Ethereum’s smart contract can save the day in such a situation. A smart contract is like the trusted mutual friend but written in code. Ethereum allows us to write a software that accepts ether worth $100 from both of us, and then the next day, uses the open weather API to check the weather and transfer the total ether worth of$200 to the winner.

Once a smart contract has been written it cannot be edited or altered in any way. Therefore, you can be sure that whatever the contract dictates, it will be executed no matter what.

But how is a smart contract executed? And how does it relate to blockchain?

# How does smart contract relate to blockchain?

Whenever a smart contract is executed, it records the information about the execution on a block as a transaction. On a very high level, a transaction on Ethereum blockchain looks like this:

The fields are self explanatory except the last one. The ‘data’ field is what gives Ethereum its unique power. The ‘data’ field is used to record creation and execution of smart contracts as a transaction. Any given block on the Ethereum blockchain can contain three kind of transactions:

1) Regular transfers of ether from one user to a human user

These are the regular bitcoin-like transactions in the network. If you send ether directly to your friend the data field will be left empty.

2) Transfers of ether from one user to no one

When a transaction without a recipient is made, it means that the purpose of the transaction is to create a smart contract in the network using the contents present in the ‘data’ field. The ‘data’ field contains the software code that will be made to act like any other user in the network.

3) Transfers of ether from a user to a smart contract

Whenever a user (or a smart contract) wants to execute a smart contract, he/she/it is required to make a transaction to the smart contract and put the instructions for the execution in the ‘data’ field.

Just like in any other blockchain, whenever any of the three events mentioned above happen, it’s announced to the whole network and everyone makes a note of it. In addition to making note of it, each node also executes the instructed smart contract to bring the state of their EVM in sync with the rest of the network.

Each node executes a piece of software, thus, making the whole network act as a giant (but slow) distributed computer. Every tiny execution is then stored on the blockchain to make it permanent.

# Conclusion

Ethereum isn’t just a cryptocurrency to be traded; its real value lies in its purpose. Ethereum’s purpose is to allow the owner to use the distributed world computer that several thousand nodes are powering.

Of course, because every tiny statement has to be executed by every node in the network, the decentralized, distributed computer becomes slow and expensive. But slow compared to what? The faster but centrally controlled servers.

To enjoy the lower costs of using a centralized computer, we give them the power to control us. If the central computer (server) goes down or gets hacked, it takes down with it all the clients attached to it. A decentralized computer will only go down if every single node goes down, making it a computer that will always be available.