Wed. Apr 14th, 2021
Hello World, smart contract, Free TON

The author of the article is Ruslan Dorofeev. Source — Habr.

One of the topics that I would like to talk about today is the programming of smart contracts. For the language in which we will program our smart contract we will take Solidity, and we will use Free TON as the platform.

We will not dive into blockchain technology, because there are already many articles about it. We will look at a simple smart contract in the following order:

  1. Where to begin
  2. Hello World
  3. Features of the TON smart contract Hello World

Quick Start

To work, we need VSCode and the TONDev plugin, after installing it, we will perform the following:In the VSCode explorer area, right-click, and in the context menu at the bottom, select Create Solidity Contract:

Visual Studio Code, Free TON
Selecting Create Solidity Contract in VSCode

The Contract.sol file generated by the plugin will appear:

Free TON, Smart contract programming
Created default smart contract in VSCode

Now we can compile it by clicking on it and selecting Compile Solidity Contract in the context menu:

Compile Solidity Contract, Free TON, smart contract
Compiling a smart contract in VSCode

This way we can immediately get a ready-made, but not very useful smart contract. The compiled .tvc and .abi.json will be added to the working directory of the project.

Error highlighting is related to the features that we will talk about later, but for now, let’s write our own HelloWorld.sol.

Hello World!

In its simplest form, our Hello World will look like this:

Hello World, Free TON, smart contract

Or we can write inside our function like this tvm.log(“Hello World!”); this is an instruction for the TON virtual machine, so let’s talk about the TON Solidity Compiler API.

Features Of The TON Smart Contract Hello World

The smart contract function is performed in such a way that the execution of each bytecode command consumes a certain amount of gas: such a system is needed to prevent mass calls of smart contract functions from other accounts, aimed specifically at overloading and denial of service of the system (in common DDoS). Thus, the functions of smart contracts are performed at the expense of the calling account. For this it needs to transfer a certain amount of funds to pay for the function call (depends on the function body), otherwise (when there are not enough funds), instead of executing the function, the calling account will receive an error report.

As you can see from our Hello World example, the first instruction we set is tvm.accept(); this is the call to the TON virtual machine API. Thus, we inform the smart contract that it is necessary to perform this function even if the account did not finance the required function call, and the function will be called at the expense of the funds on the balance of the smart contract account. Like user accounts, smart contracts have a similar account.

Since the gas for smart contracts serves as a means of protection against spam attacks, it requires financial costs, expressed in the cryptocurrency of the blockchain network. Therefore, calling tvm.accept(); spending funds from the smart contract account is not very profitable from the point of view of providing services . To balance the costs and determine whether there is a need to execute the smart contract at the expense of the account balance of the smart contract itself, or to impose the costs on the calling account, you can use the require() statement.

The require() statement allows you to specify the conditions that will trigger the execution of the smart contract function.

For example, we can add a require (msg.pubkey() = = tvm.pubkey()) requirement before calling tvm.accept(); which will prevent the smart contract function from being called if the public key of the smart contract sender does not match the account key of the smart contract itself.


Ruslan’s next publication will discuss the basics of Free TON smart contract programming.