Solidity, “hello world” with the Blockchain language

Codemotion
5 min readDec 19, 2019

--

In a previous article, we introduced the basic concepts of the Ethereum Blockchain, including a quick overview of the ERC-20 tokens.

In this article, we will dive into Solidity, the programming language used to interact with the Ethereum Blockchain. We will learn how it works, how it is translated into the Ethereum Virtual Machine and then executed, and which IDE to use to begin to write some code.

Solidity

The main and most commonly known language to write smart contract in Ethereum is Solidity. It was designed in 2014, a few months before the official launch of the Ethereum Blockchain, and is now used for the Ethereum public Blockchain, as well as for private Blockchain such as Hyperledger. As we said in the first article of this series, it’s a statistically-typed programming language that runs smart contract on the EVM, Ethereum Virtual Machine.

Solidity is compiled to bytecode, that it’s then executed on the EVM. To date, the are a total of 142 valid opcodes that compose the bytecode necessary to compile any Ethereum smart contract.

Another commonly used language is Vyper, which is based on Python, but this series will not cover the development of smart contracts with Vyper.

The Ethereum Virtual Machine

On the Ethereum Blockchain, a smart contract is a simple Ethereum address with the special capability of running EVM bytecode when receiving a transaction, allowing them to perform calculations and further transactions.

Transactions can carry a payload of 0 or more bytes of data, that is used to specify the type of interaction with a contract and any additional information.

The smart contract begins execution on the EVM at the beginning of the bytecode: each opcode is encoded as one byte, except for the PUSH opcodes that have an immediate value. Every opcode is also associated with a specific gas cost. The sum of all the gas costs of a smart contract is the cost of one execution of the smart contract. Since gas cost is critical in ensuring the viability of a smart contract (an expensive-to-run smart contract is often infeasible for users to actually use), it’s useful to bear in mind that a smart contract needs to run with the least possible amount of instructions. A full list of gas costs per opcodes is available here.

Interaction with a smart contract is possible with a public ABI, which is a list of supported ways a user can interact with a contract.

To interact with it, a user will submit a transaction carrying any amount of gas (even 0 is acceptable) and a data payload formatted according to the ABI, specifying the type of interaction and any additional parameters.

The logical structure of a smart contract can be divided as described below:

  • Call Data: the data associated with a transaction. It usually contains a 4-byte method identifier followed by serialized arguments;
  • Stack: the EVM maintains a stack of uint256 used to hold local variables, function call arguments and return addresses;
  • Memory: an array of uint8s used to hold processing data while the contract is being executed — this data does not persist across consequent transactions
  • Storage: a persistevi associative map, with uint256s as keys and values, the contains all contract fields and mappings.

Choosing the IDE

To write code and test smart contracts, a plethora of IDEs is available, both as online and offline applications. We will provide a complete list of all known IDEs to develop Solidity as of today, and then we will dive into the most simple to use.

Online

  • Remix: Built-in static analysis and test Blockchain VM;
  • Ethereum Studio: Built-in browser Blockchain VM, Metamask integration (one-click deployments to Testnet/Mainnet), transaction logger and live coding;

Offline

  • Atom: editor with Atom Solidity Linter, Etheratom, autocomplete-solidity, and language-solidity packages;
  • VIM Solidity: VIM syntax file for Solidity
  • Visual Studio Code: Visual Studio Code extension that adds support for Solidity
  • IntelliJ Solidity Plugin: open-source plug-in for JetBrains IntelliJ Idea IDE (free/commercial) with syntax highlighting, formatting, code completion;
  • YAKINDU Solidity Tools: Eclipse-based IDE, featuring context-sensitive code completion and help, code navigation, syntax coloring, build in compiler, quick fixes and templates;
  • Eth Fiddle: allows you to write, compile and debug the smart contract. Features sharing and search of code snippets.

For this series, we will choose Ethereum Studio, because it is the most complete out-of-the-box solution for developing and deploying smart contract on the network.

First steps with Ethereum Studio

Ethereum Studio is available on studio.ethereum.org. Visiting the website will pop-up a banner that instructs the user to choose from a set of smart contract templates: for now, we will choose the ‘Hello World’ smart contract template, that allows us to understand the basics of development without writing specialized code.

An ‘Hello World’ template on Ethereum Studio — studio.ethereum.org,

Before writing some actual code, it’s important to understand all the basic functions of Ethereum Studio.

The window is divided into three panes (from left to right):

  • Explorer: in the first pane, one can view the usual file hierarchy. At the top of the explorer, one can view the blockchain network on which the contract will run (the default is the Browser blockchain, that is a fake blockchain to run tests on) and the contract address that’s associated with it. Notice that it’s only possible to deploy one smart contract per address.
  • Text: where it’s possible to write the actual code
  • Simulator: a visual simulation of the functions of the smart contract or, in the case of smart contracts running at the core of an app, the rendering of the app (much like Xcode).

In the preferences (top-right corner of the windows), it’s possible to adjust the gas requirements of the contract, setting personalized amount for gas limit and gas price (in wei).

At the bottom of the window, instead, there’s basic information about the account address and the network: the address balance (in ETH), the gas limit, the gas price and the address of the network.

Now, we’re finally ready to write some code. In the next articles, we will write basic functions and understand how to create smart contracts for different purposes.

--

--

Codemotion
Codemotion

Written by Codemotion

We help tech communities to grow worldwide, providing top-notch tools and unparalleled networking opportunities.

No responses yet