How an Ethereum smart contract works

Ethereum is rapidly catching up to Bitcoin, and its relative growth is often attributed to its greater potential applications.

Ether, the default value token or cryptocurrency of the Ethereum blockchain, and Bitcoin are built on blockchains which function as distributed ledgers secured by public consensus.

However, the Ethereum blockchain expands the concept of a distributed ledger to allow for more advanced commands.

A basic explanation of the differences between Ethereum and Bitcoin can be found in this article.

Decentralised Contracts

Users can build “smart contracts” on the Ethereum blockchain by creating a transaction without a destination address and containing a section of code to determine what it does.

These smart contracts are then processed by Ethereum miners as regular blocks and added to the blockchain, permanently storing them in the public distributed ledger.

If someone interacts with the contract in the future (for example, by transferring some currency to its public address), the contract will execute its code and create other blocks to be added to the blockchain.

This allows the Ethereum blockchain to function as a decentralised virtual computer, on which developers can build applications consisting of multiple smart contracts.

A simple example of a smart contract is a “greeter bot” which replies to all users who send it ether. If the original creator sends a transaction to the bot from its address, the bot will transfer all collected funds to the creator.

Below is a very basic summary of how a simple smart contract like this would work:

  1. Creator submits a transaction combining the “greeter bot” code and a minimum amount of ether to form a smart contract.
  2. Another user sends an amount of 1 ether to the bot’s public address.
  3. The bot is triggered and sends an empty transaction with a short text message back (“Hello!”) in response to the sender.
  4. The creator sends an empty transaction to the bot.
  5. The bot is triggered and sends all collected ether to the creator’s Ethereum wallet.

Ethereum’s smart contract system can be very powerful and is enabled by two important factors: A turing-complete programming language called Solidity, and a minimum Ether price for executing each line of code.

Neural network

Gas

Every line of code executed in a smart contract costs ether depending on how process-intensive it is.

This encourages developers to create smart contracts which are as simple as possible while also preventing abuse and making intensive. repetitive attacks financially prohibitive.

The ether price required to execute a smart contract or send a transaction is reffered to as the “gas” price to differentiate the currency that powers blockchain transactions from the cryptocurrency used for payments.

All transactions have a minimum (usually negligible) gas price which is earned by the miners who process that block.

Gas is often reffered to as the fuel required to interact with the Ethereum blockchain and confers a tangible value on the ether currency, as consumers who want to use an app on the blockchain will have to purchase or mine ether to do so.

Applications built on the Ethereum blockchain are often referred to as “dapps” (decentralised apps).

Examples of dapps currently functioning on the Ethereum blockchain are trustless gambling apps, escrow or lending services, and the Raiden Network.

Solidity

Solidity is a turing-complete programming language created specifically for use in Ethereum smart contracts.

Turing-complete means the language can potentially be used to solve any computational problem and includes support for functions such as infinite loops.

Solidity’s syntax is simlar to that of JavaScript, and the Ethereum Solidity tutorials provide a collection of resources to help developers learn to use the language.

When developing smart contracts for implementation on the Ethereum blockchain, it is important for developers to make the code as simple as possible to avoid unnecessarily high gas costs which can deter users from interacting with their application.

While some smart contracts cannot be reasonably implemented due to gas cost constraints, Ethereum’s developers are constantly improving Solidity and refining the way it interacts with the blockchain.

Smart contracts are what differentiate Ethereum from the rest of the major blockchains, and its creators envision the platform as the next big networking breakthrough, or “Web 3.0”.

Now read: We bought an Ethereum mining rig with the hope of retiring early

Latest news

Partner Content

Show comments

Recommended

Share this article
How an Ethereum smart contract works