How to program a smart contract

Once we have realized the potential of applications based on Smart Contracts, we can ask ourselves: where do I start? What type of network do I choose? Depending on the needs of our business, and especially the use case that we are going to implement, we have several options. Today we are going to delve into one of the most well-known networks, Ethereum.

Ethereum

Ethereum is surely the first option that comes to mind when we think of Smart Contracts. Created by Vitalik Buterin, based on the Bitcoin protocol, Ethereum offers its Ethereum Virtual Machine (EVM) for the execution of intelligent contracts.

This virtual machine of Ethereum, that would not stop being similar to the virtual machine of Java, interprets and executes the instructions that form the Smart Contracts, with the exception that this machine is distributed in the network.

This means that all the nodes execute the instructions, which brings us two immediate consequences:

Ethereum is not very efficient, so it is not designed to perform complicated computational calculations, both for the necessary cost and for the time necessary to confirm the executions.

A decentralized consensus is achieved, which gives us a very high level of tolerance to errors, ensures that the network never falls and benefits from the characteristics of the blockchain, making stored information impossible to modify and resistant to censorship.

The contracts that can be executed are complete turing, which can be summarized in that we can use a set of operators and instructions that allow us to execute programs similar to any modern programming language.

The programming language used for these programs in Ethereum is Solidity, a language similar to Javascript in the syntax, which facilitates its adoption, but enriched with Object Oriented Programming concepts such as C ++.

Based on the smart contracts deployed on the network we can create applications known as Distributed Application (DApp). The DApps are composed by smart contract that run on the network of Ethereum nodes (the backend).

A decentralized application (Dapp, dApp or DApp) is an application that is run by many users on adecentralized network with trustless protocols. They are designed to avoid any single point of failure. They typically have tokens to reward users for providing computing power.

We have spoken that efficiency is not one of the advantages of the Ethereum network. The reason for this is that the network is based on a huge set of nodes, which execute the contracts and validate the blocks with new information.

In order to maintain these nodes, it is necessary to offer some kind of reward, and that is why the Gas concept arises.

Gas and Ether

The Gas in Ethereum is the cost associated with carrying out each of the operations in the Ethereum network. Depending on the complexity of our program or smart contract, we will have a higher or lower Gas expense.

When we talk about complexity, we must think of terms of computational instructions: to make a comparison, a sum, they are simple instructions but they are associated with a cost.

The easiest would be to think of instructions at the “assembly language” level, so that each of them has an associated Gas cost, and by analyzing our program, we can obtain an approximation of the complexity.

But as happens with the gasoline of a car, the expense not only depends on the distance (complexity), but also on the speed of the Gas that we buy.

If we buy gas “very fast” we will make sure that our program runs in a short time (usually in the next block), but if our case does not require so quickly, we can buy a gas with a lower price so that our program it will have less priority.

Gas is like gasoline to run the trip. Depending on the distance and speed, the expense will be higher or lower. It is important to check the level before leaving, or we can stay halfway through the course.

Although we can only think of Gas as an expense, Gas brings a series of advantages to the Ethereum network:

Optimizes contracts: by assuming an expense, it prevents the network from being filled with unnecessary calculations that would make it less efficient. It also helps the chain to be lighter, since it is not filled with non-relevant information.

Prevents misuse of the network: the use of Gas makes it unfeasible to use the network to perform Spam.

Ensures the network against malicious programs: the use of finite Gas protects the network against the use of malicious programs that could execute attacks on the network through the use of infinite loops and excessive computing.

LEAVE A REPLY

Please enter your comment!
Please enter your name here