A brief guide to how smart contracts work on Everscale

A brief guide to how smart contracts work on Everscale

Everscale is a blockchain that brings the idea of a smart contract to its absolute value. Everything in the blockchain is a smart contract. You can send EVER to any blockchain address, but it is only possible to receive it back if there is no corresponding smart contract at that address, since smart contracts are the only tools that Everscale has for changing the state of cryptocurrency. In this article, we will try to understand the principles of Everscale blockchain smart contracts and how they work.

First, let’s define the term smart contract. A smart contract is a code that is stored on a blockchain and executed by a part of the software on a node called a virtual machine. The Everscale virtual machine is called ESVM.

The Everscale blockchain includes a masterchain and a workchain. Structurally, they are identical. The masterchain contains the main network configuration parameters, information about blockchain validation, and a set of hashes of the latest blocks of workchains.

Workchains, or shards, are blockchains that contain most user transactions and data. Each workchain can have its own configuration parameters, virtual machines, and native currency, while still retaining the basic principles of data transfer.

The current Everscale mainnet configuration has a masterchain and one workchain.

A unique feature of Everscale is the dynamic splitting of the blockchain (masterchain and workchain) into threads that run in parallel, each being run by its own group of validators. This enables the blockchain to demonstrate outstanding performance of over 100,000 TPS.

To execute a smart contract, you need to pay a certain amount of EVER — this is called a “gas” fee. Gas fees are primarily intended to protect the blockchain from overloading the computing power of nodes. The gas limit for executing one smart contract in Everscale is 1 EVER.

Since the blockchain is able to handle huge workloads, the gas fees are low. In the near future, Everscale has plans to significantly improve the node software and enhance the SMFT consensus, which will further increase their performance and cut gas costs in half. This will make Everscale one of the cheapest blockchains in terms of the cost of executing smart contracts.

Everscale smart contracts interact with each other using messages. Messages are divided into internal and external ones. Internal messages are data that is transferred inside the blockchain from one account to another. These messages can transfer information about cryptocurrency.

A transaction in Everscale means a change in the state of the blockchain. The only way to change the state of an account is to send an appropriate corresponding internal message. When a smart contract receives an incoming message, it can initiate the process of sending multiple outgoing messages. The transaction refers to the incoming message that initiated it.

There is no limit to the size of internal blockchain messages — it is only limited by the size of the block or the amount of gas used, which is based on the space used. For example, if a processed transaction message is the size of a block, the block will consist only of this message.

All internal messages in Everscale are numbered sequentially and executed strictly in order. The “front running” attacks that Ethereum is vulnerable to are not possible in Everscale due to its asynchronous execution model and all sequencing agreements, which are determined and strictly executed on the blockchain.

External messages are messages sent either to the blockchain from outside or from the blockchain outward. They cannot transmit cryptocurrency and their size is limited to 16KB to protect against DDoS attacks. Validators have a gas reserve equal to 10,000 to execute them.

This is what makes Everscale so fundamentally different, and it affects the entire operation of smart contracts.

An Everscale smart contract is deployed by an external message. That’s why the size of the smart contract code is limited to 16KB, and this limitation is important to consider at the initial stage of smart contract development. If you face a 16KB code limit during development, it’s better to split the smart contract into functional components.

There is a rule in Everscale: a smart contract can only be deployed at an address that is calculated based on the smart contract code and user data. To update the smart contract code, for example, when new functions are added to the wallet smart contract, there is the SetCode function. With the required permissions and by sending a message from the cell with the new code using this function, the target smart contract code will be updated.

You can write Everscale smart contracts in two languages: C++ and Solidity.

The development tools available are a compiler, TVM Linker, Tonos Cli utilities to work with the blockchain, numerous diagnostic tools, an assembler, and a disassembler. Syntax highlighting adapted for Everscale is also available.

Everscale’s technical design is for smart contract systems that will operate in parallel. For this reason, a good development style on Everscale is to describe the rules according to which your smart contracts are built, maintained, interacted with, updated, and queried, and only then develop the required functionality. At the same time, there is an inverse relationship: the more isolated a smart contract is, the less efficient it will be in Everscale.

Read More