What are smart contracts and how do they work?

Over the rise of new applications that use blockchain technologies through Web 3, the term “Smart contract” is being heard more and more. But, What Exactly is a smart contract? How does it function? And what advantages do they bring?

Published on

What are Smart Contracts?

From a non-technical perspective:

Smart contracts are what their names suggest, contracts. Like any contract, they establish the terms of an agreement. The difference is that smart contracts’ terms are executed as code, usually running in a blockchain.

Perhaps one of the best analogies is the one by Nick Szabo, where he describes a smart contract as a vending machine. By giving money to the vending machine and with the correct product selection, the product is guaranteed. Smart contracts work equally. If we want to request something that the smart contract offers, by making a transaction we are guaranteed to get the contract’s output.

From a computer science perspective:

Smart contracts are nothing more than pieces of code and data stored in a particular address inside a blockchain. It is important to note that not all blockchains support smart contracts, the Bitcoin blockchain, for example, doesn’t support them.

Inside a smart contract, you can find a series of attributes and methods, in a similar way they would be defined inside a class, using an OOP analogy. These methods (or functions) can be used to retrieve information from the smart contract or to change the state of the blockchain, this is why every smart contract has its own address and balance.

How do Smart Contracts work?

A smart contract can be created and deployed by anyone. All the code and logic of a contract is public, so any interested part can see how it was implemented. Smart contracts can be written in a different variety of languages but need to be compiled into machine code that can be run by a blockchain node. The most common one is EVN (Ethereum Virtual Machine). Then, once it is deployed, every node in the network has a copy of the contract and its state. When a function that makes a change in the smart contract state is called, all nodes in the network execute the code and reach a consensus about the changes and the resulting value. 

One can make use of the smart contract via transactions, this means that calling functions that alter the state will have a small cost or fee, called a “gas fee”. It is called “gas” because it is what keeps the blockchain running. This means that using a smart contract costs money, which is a security feature because it prevents the possibility of saturating the blockchain with smart contract calls.

Tags: , , , , ,

What to read next