In a previous article, we introduced the basic concepts of the Ethereum Blockchain, including a quick overview of ERC-20 tokens.
This article dives into Solidity, the programming language used to interact with the Ethereum Blockchain. We will learn how it works, how it is translated in the Ethereum Virtual Machine and then executed, and which IDE should be used to start writing code.
Don’t forget to check also part three, part four and part five.
Solidity
The principal and most widely known language used to write smart contracts in Ethereum is Solidity. Designed in 2014, a few months before the official launch of the Ethereum Blockchain, Solidity is now used for the Ethereum public Blockchain, as well as for private Blockchains such as Hyperledger. As discussed in the first article of this series, Solidity is a statically-typed programming language that runs smart contracts on the EVM, Ethereum Virtual Machine.
Solidity is compiled of bytecode that is subsequently executed on the EVM. To date, there are a total of 142 valid opcodes that make up the bytecode necessary to compile any Ethereum smart contract.
Another commonly used language is Vyper, based on Python, but the focus of this series does not include the development of smart contracts with Vyper.
The Ethereum Virtual Machine
On the Ethereum Blockchain, a smart contract is a simple Ethereum address with a special capability – running EVM bytecode when receiving a transaction, which allows the performance of calculations and further transactions.
Transactions can carry a payload of 0 or more bytes of data, 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 PUSH opcodes, which 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 not feasible for actual use), it’s important to bear in mind that a smart contract needs to run with the fewest possible instructions. A full list of gas costs per opcode is available here.
Interaction with a smart contract is possible with a public ABI – a list of supported ways a user can interact with a contract.
To interact with the contract, users 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. This usually contains a 4-byte method identifier followed by serialized arguments;
- Stack: the EVM maintains a stack of uint256 that is used to hold local variables, function call arguments and return addresses;
- Memory: an array of uint8 is used to hold processing data while the contract is being executed – this data does not persist across consequent transactions
- Storage: a persistent associative map, with uint256s as keys and values, that 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. A complete list (as of today) of all the known IDEs used to develop Solidity is provided below, then we dive into the simplest 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, built-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 use Ethereum Studio. This 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 activate a pop-up 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 – this will allow us to understand the basics of development without writing specialized code.
Before writing any 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 – a fake blockchaindesigned for running tests on) and the associated contract address. Note that only one smart contract can be deployed per address.
- Text: where the code should be written
- 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, a rendering of the app (much like Xcode).
In preferences (top-right corner of the window), it’s possible to adjust the gas requirements of the contract, setting personalized amounts for gas limit and gas price (in Gwei).
At the bottom of the window, you’ll find 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 articles that follow, we will write basic functions and discover how to create smart contracts for different purposes.