Ethereum Smart Contracts

Redouane OTMANI
Coinmonks
10 min readJul 15, 2022

--

Ethereum is a project created by Vitalik Buterin in 2013. Basically, he wanted a scripting language, a way to create programs for Bitcoin, however, he didn’t have that approval from Bitcoin community to do so. So he went ahead and created his own project which later became a giant in its own.

The purpose of Bitcoin is simply to create a cryptocurrency. As for Ethereum, it is a platform for building programs, so a blockchain that allow not only to store data and transactions but also programs.

In this article, I will present the smart contracts, as well as basics of Ethereum.

Outline of the article

I. What is a smart contract ?

II. The basics of Ethereum

  1. Token
  2. Initial coin offering
  3. Ether
  4. Wallet
  5. Gas
  6. Ethereum Virtual Machine
  7. Oracles
  8. Decentralized Autonomous Organization
  9. The fork
  10. The attack against The DAO.

I. What is a smart contract ?

A smart contract is a computer program that runs automatically and autonomously on each node of the blockchain. It is constituted and governed by a set of rules and clauses concluded between the two parties involved in the transaction.

Based on the logic “If this, then that” allowing to exchange money, goods, shares or any other object of value in a transparent way and without trust intermediary.

It is a concept that was developed by cryptographer and computer scientist Nick Szabo in the mid- 1990s, long before the advent of Blockchain technology. The word “smart contract” defines a set of rules that automatically execute if conditions are met without the involvement of a trusted third party. But this idea could not be realized at the time because no technology supported the implementation of smart contracts… Until 2009, the application of blockchain technology emerged with Bitcoin, allowing the implementation of a draft of smart contracts, however, it was not until 2013, with the appearance of Ethereum, that the smart contracts were truly implemented and introduced.

Indeed, Bitcoin was not able to deploy smart contracts because the programming language associated with it, the “Bitcoin script” which, unlike Ethereum’s “Solidity” programming language, was not Turing complete. In other words, it was not possible to code any logic with the bitcoin script, moreover, the important element missing from Bitcoin script are loops. Besides, this is not accidental, they have it intentionally, because in general, programs run on each node of the Blockchain, so they can really slow down if someone intentionally (or not) creates a program that contains an infinite loop. Loops are indeed a dangerous tool if they are used in an incorrect way.

II. The basics of Ethereum

Ethereum is considered a decentralized network of computers that anyone can program and use. In particular, it allows transactions to be carried out and any type of data to be stored on its blockchain. It is characterized by a multitude of basic concepts : Token, ICO, Ether, Wallet, Gas, EVM, Oracles, Fork, DAO, etc.

1. Token

A token is a digital asset issued and exchangeable on a Blockchain, and having the characteristics of a crypto currency. It is transferable between two parties without a trusted third party.

Tokens have several functions and their possibilities of use are very numerous. Thus a token can represent a right, a means of payment or a unit of exchange value within a given application or ecosystem.

2. Initial coin offering

An ICO (Initial Coin Offering) is a fundraising mechanism that works by issuing tokens that can be exchanged for crypto currencies during the start-up phase of a project. The tokens exchanged do not represent shares in the company, they allow to prepay the product or service to be developed.

3. Ether

Ether is a decentralized, tradable digital cryptocurrency, which additionally powers the Ethereum network by paying for transaction fees and computing services.

4. Wallet

Wallets are applications that allow you to store funds (Ether), easily perform transactions and check the balance whenever the need arises. In addition, wallets have public addresses that allow them to uniquely identify themselves in the network. Therefore, if a person wants to send ether, they will send it to this public address. It should be noted that in addition to ether, wallets also store user’s private keys.

5. Gas

We talked about loops above and why Bitcoin script never included them because someone can maliciously or accidentally make an infinite loop or a very heavy load that will slow down the calculations on all nodes and therefore slow down the network. Ethereum on the other hand has solved this problem with the concept of gas, now loops are no longer a threat. To put it simply, we have a quantity of gas that we will use to pay for all the operations we need to perform. As the Excel file shows how many units of gas it takes to perform an operation, each operation has its own cost (even for low-level operations). At this point, we can clearly see how this solves the problem, even if our program is computationally too heavy or if it contains an infinite loop, in which cas it will quickly run out of gas and stop working. This concept really encourages people to write good code, otherwise you don’t want your code to be inefficient even a little bit, because it will cost you.

So gas is the amount of computer time needed to perform an operation. Thus, an the complexity of an operation increases, the cost of the gas unit increases. Each unit of gas has a price denominated in Gwei (1 Gwei = 0.000000001 ETH), defined by the demand on the market.

Why don’t we use Ether to pay instead of gas ?

Well, the price of ether changes overnight, it changes all the time, every day, so it would be very unpredictable to try to figure out what it is going to cost you to do what you are doing.

The connection between gas and ether, helps us to see the difference between Bitcoin and Ethereum.

The predominant idea behind Bitcoin is to create a cryptocurrency for people to transact, exchange and pay for services, something that is transparent and borderless, with no restrictions and no permissions (You don’t have to ask permission to send money anywhere you want). This is the principle behind Bitcoin.

Whereas with Ethereum, and ether, even if people don’t use it to make transactions, they pay to run their programs.

6. Ethereum Virtual Machine

One of the security aspects dealt with in Ethereum answers the problem : If someone writes a virus like a smart contract and the deploys it on the blockchain, everyone will have a copy, and suddenly, that’s the end, all the nodes will be infected. This is where the EVM (Ethereum Virtual Machine) comes in, it runs on your computer, and therefore it completely encapsulates everything that passed through it. So, these programs run on the EVM and nothing can get out, since it is not in contact with the host machine, so it is isolated, so if something were to go wrong, this program will never have access to your files, it will never be able to see them.

7. Oracles

The Ethereum blockchain and blockchains in general cannot call on an external service. In other words, it is blind to the outside world, hence the need to use an oracle.

The oracle is a service in charge of manually entering an external data into the blockchain. Indeed, at the moment T, which will have been defined in advance, the oracle will retrieve the information that has been requested and will insert it into the Blockchain at the place that has been designated. When the smart contract that needs this data is executed, it will search for it in the Blockchain at the designated address and then execute according to this data.

8. Decentralized Autonomous Organization

What are organizations nowadays ? An organization is composed of a director, managers and employees, etc.

Well, these people follow certain procedures in order to make their company work. For the checks and everything, within that company, everything is done by humans most of the time, it’s just procedures that have to be followed by people, to make business decisions, every organization has work that has to be done.

Organization

So the idea is to assume that if we have an organization, where we don’t have to do it the traditional way, that is when it can be done automatically, that will bring the notion of smart contracts. If we code all this into smart contracts, all of a sudden you have an organization that works by itself, without human intervention, and that’s what DAO is all about. So a DAO (Decentralized Autonomous Organizations) is a fully decentralized and automated organization. It exists only on the Ethereum Blockchain and its participants interact with each other only on the Blockchain. As a result, the DAO is characterized by its transparency and continuous operation.

DAO

9. The fork

A fork is a division into several branches. At the blockchain level, it can be a division of the blockchain caused by a conflict between blocks or a division caused by a change in the protocol governing the blockchain. There are two types of fork : The soft fort and the hard fork.

The soft fork. Is a change in the rules known as backwards compatible, that is to say, it’s unnecessary to update all the nodes and miners for the blockchain to continue to function.

The hard fork. Is a fork that modifies more deeply the rules governing the Blockchain. As a result, the blockchain becomes unusable for miners who do not update their system. At the end of this process, two cases are possible : Either all the nodes and miners accept the consensus and update their system, or some of the nodes and miners refuse to update their systems. In this case, two blockchains will be built in parallel.

Hard Fork

10. The attack against The DAO

DAO was one of the first decentralized autonomous organizations created by Vitalik Buterin in 2016 on Ethereum. The concept was to create an investor-led venture capital fund, where it helps develop decentralized applications to run on Ethereum, it’s like a noble cause to help others start their own decentralized projects on the blockchain. Note that the DAO organization does not belong to any country.

In May 2016, when it finally appeared, they raised one hundred and fifty million dollars $150,000,000 through Crowdfunding and it was the highest amount in the history of crowdfunding. People believed in this idea of decentralization.

However, unfortunately, there was a mistake in the DAO code, a vulnerability, I’d rather say.. In the way the DAO smart contracts were programmed, because as we said, these DAOs are entirely governed by smart contracts rather than people.

As a result, the DAO was attacked and hacked, in June 2016 for $50 million (at the time), and what’s interesting here is that the attacker didn’t do anything “illegal”, he just found this loophole in the code, and he used it to transfer money from the DAO’s account to his own account…

Everyone saw what was going on, but they couldn’t do anything about it, because the DAO is autonomous, and you can’t just tell it what to do, it does things on its own, it’s governed by its own algorithms, and at the same time, there was this loophole in the contract that allows the attacker to steal ether. Not to mention the fact that the contract is immutable in nature, you can’t change it once it’s in the blockchain, you can’t just go and change things because that’s against the purpose of the blockchain, so that was a major problem.

However, the good news was and according to the design of the DAO, there was a fail-safe mechanism that meant that the funds could not be withdrawn in full, until about 30 days later.

At this point, the whole community had the opportunity and time to think and decide what to do, so there was a dilemma, which was “Code is Law ?“. One part of the community said we’re not going to do anything, because the code is law, and we can’t just go and change thing that are supposed to be unchangeable and so we have to use it as it is.

The other part of the community said this is way too much money, fifty million dollars, and it belongs to people, and we’re going to do hard fork by changing the rules, and that way, we’ll be able to change the contract and get money back then giving it to its owners.

Eventually, they ended up doing a hard fork and it was Vitalik’s initiative to do the fork, but because some people didn’t like the idea, not because they wanted to protect the attacker, but to preserve the integrity of this concept of blockchain, that is there is a smart contract, it is a contract for a reason.

As a result, Ethereum was divided into ETH (Ethereum) and ETC (Ethereum Classic). In the ETH version, the pirated money as recovered and distributed to people, but in the ETC version, the money remained in the possession of the attacker.

It was, so to say, two parallel worlds, in one the money was recovered and in the other the money remained with the attacker, because the stolen money was doubled in both versions of Ethereum.

At the end the hacker, had left with 67 million dollars in the ETC version (67 because it was calculated later, exchange and all).

Note that the problem was in the DAO code and not in Ethereum, it was a problem in the way the smart contract was coded not the platform.

Hard fork

[Credit to Mélissa]

If you enjoy my stories, you can follow me on Twitter

--

--