Learning crypto?
Our new handy guide simplifies it all. ‘Crypto Categories Unveiled’
Download here

Smart contracts are pieces of code stored within a blockchain that specify a number of conditions and can only be executed once these conditions are met. They come in many different shapes and sizes, from those that move around coins or tokens within the blockchain environment, to those that rely on external data to satisfy their conditions.

How do smart contracts work?

What sets smart contracts apart from regular programs is that they are run on a decentralized network. This gives them a huge advantage, since they do not rely on a single central system to execute them. Blockchains platforms, such as Ethereum, enable smart contracts to be stored and run on a network of nodes spread around the world.

In this article, we take a look at how the Ethereum blockchain allows smart contracts to be nested in it, how these articles are compiled and how they are deployed, as well as pointing out a couple of case studies.

If this is the first time you are hearing about smart contracts, then you might consider first reading our article on smart contract basics.

SMART CONTRACT OPERATION ESSENTIALS

  • Smart contracts are software code that can be executed once their conditions have been met.
  • Smart contracts are trustless software that cut the need for a middleman.
  • Smart contracts are run by the Ethereum Virtual Machine runtime environment.
  • Most of them are written in the Solidity programming language.
  • Fees for the execution of smart contracts are calculated in gas and paid in ETH.
  • Real-world data required for their execution is provided by so-called oracles.

Underlying mechanism

Ethereum’s capacity for facilitating smart contracts is thanks to the protocol’s native runtime environment called the Ethereum Virtual Machine or EVM, which is run by all the nodes in the Ethereum network. It is a protected sandbox for the execution of smart contracts, which is completely isolated. The code within the EVM has no access to the network, file system, or other processes.

EVM is defined as a quasi-Turing complete machine. This means that it can perform any calculation or run any computer program out there, as long as the gas required for running the program does not exceed the set gas limit. In a way, EVM acts like the judicial system, making sure that smart contracts are enforced as they should be.

Contract compilation and oracles

Unlike regular contracts, smart contracts are not sheets of paper printed with complex legal formulations. Instead, they are programs mostly written in Solidity, which is a high-level programming language developed specifically for the implementation of smart contracts.

Once a developer writes a smart contract in Solidity, it must be compiled in EVM-compatible bytecode before its is deployed. The compilers used for translating Solidity into bytecode will notify the developer of many potentially exploitable problems in the code. This gives the developer a good chance to fix the code before the contract’s deployment.

Since smart contracts are public, they can be accessed by everyone in the blockchain. Unless a developer exposes the code via etherscan or some other platform, what is visible to the blockchain community is the bytecode, not the Solidity source code.

The inherent problem with smart contracts is that they cannot access the real-world information that is often essential for their proper execution. To solve this problem, smart contracts rely on oracles, which are third-party data feeds for smart contracts. They are responsible for finding and verifying real-world information, which is then communicated to the blockchain.

Since oracles are third parties and not part of the blockchain mechanism, they are not entirely trustless. This is a problem because there are no rollbacks in smart contracts. When you “sign” a smart contract, you agree to all of its “terms,” and there is no way of reversing the process. This means that the information provided by oracles – be it information on flights, accommodation, or anything else – must be accurate.

Smart contract deployment and use cases

So, how exactly does a transactor go about entering a smart contract? The basic principle is as follows.

Each command, or so-called OP code, has a more-or-less set amount of gas required for being issued. This means that an estimation of the gas required to execute a given command can roughly be calculated in advance – however, the actual amount of gas can only be calculated at the time of execution. If you don’t provide enough gas, the smart contract’s function will not be executed. If, by contrast, you have dedicated sufficient gas (the upper cap is unlimited), the smart contract will be able to work as designed (it will be “Turing complete”).

Among others, smart contracts have the following use cases:

  1. They can function as multi-signature accounts. This means that an amount of crypto locked in a smart contract will only be spent once a set percentage of people signing the contract have reached an agreement.
  2. They can manage agreements between users. For instance, one party might buy insurance from the other party by means of a smart contract. The amount to be paid by the insured will be deducted automatically and forwarded to the insurer.
  3. They provide utility to other contracts, kind of how software libraries function.
  4. They store application information, for instance domain registration data or membership records.

It is worth mentioning that – contrary to popular belief – smart contracts do not execute automatically when predetermined conditions are met. They are only executed once somebody or something triggers the smart contract function. This usually requires a certain amount of gas, since a new state of the contract has to be written into the blockchain. So, if you want your smart contract to be executed, you will have to pay a slight fee in ETH.

Smart contracts remain one of the most intriguing aspects of blockchain technology, since they extend blockchain functionality far beyond a ledger for monetary transactions. New and exciting smart contracts and DApps are created all the time, with many companies in the financial industry and beyond choosing to give blockchain a chance primarily because of the potential of smart contracts.

Ready to start your crypto journey?