Creating a smart contract in Free TON (Part 1)

Creating a smart contract in Free TON (Part 1)

We have already told you about smart contracts in Free TON on our channel, now it is time to dive deeper into this topic and, perhaps, dispel the existing myth that writing contracts is very difficult.

In this article, we will set up a development environment for smart contracts, create and compile our first smart contract, lock it into a blockchain and check its functionality for operability, etc.

Set up a development environment

To write our smart contract, we will use Visual Studio Code. This is an easy and fast open source IDE from Microsoft, which supports many extensions developed by the community. You can download it by clicking on the link code.visualstudio.com.

Next, we need to install the TONDev extension to simplify further work on writing and compiling the smart contract. You can do this directly from VS Code. Go to the Extensions tab on the left side of the main window, enter TONDev in the search.

Search for extensions in VS Code

Write a contract

Now we are ready to start creating our first contract. To do this, we need to go to the Explorer tab and open (create) the directory in which our code will be located. You can also clone a project from the git repository in this window.

A link to the resulting contract source code can be found at the end of the article.

VS Code File Explorer

Then, we generate a template for our contract using the TONDev extension that we installed in the previous step. To do this, in the browser of the selected directory that appears, right-click the context menu and choose Create Solidarity Contract.

Now we have a generated contract file with the .sol extension.

But, as you can see, VS Code does not support the Solidity language "from the box" and displays the contract code as a text.

Generated contract code without syntax highlighting

Therefore, we will also need an appropriate extension. Go to the Extensions tab and look for "Solidity." Since this language has been used for many years to write contracts for the Ethereum network, the search gives a huge number of solutions created by the development community around the world, so choose any one. As part of this article, an extension will be taken with the most downloads from developer Juan Blanco.

Search for extensions in VS Code

Now the code has become much more pleasant to read and easier to perceive.

Generated Solidity Syntax-Highlighted contract code

Within the framework of this article, we will not delve into the syntax of the Solidity language and other subtleties, since the article aims to introduce users to the general contract development infrastructure in Free TON. However, we will discuss some details.

Performing the smart contract function consumes a certain number of tokens (the so-called "gas"). This feature is necessary to prevent mass calls to smart contract functions from other accounts aimed specifically at overloading the system. Thus, the functions of smart contracts are performed at the expense of the calling account, for this a certain number of tokens will be removed from it to pay for the function call, and the amount depends on the body of the called function. If the funds are insufficient, then instead of performing the function, the calling account will receive an error report.

We would like to draw your attention to several instructions that will be used in the contract:

  • require - allows to specify a requirement that must be met before further code is executed. If the requirement is not met, the calling account will receive an error.
  • tvm.accept - allows the contract to continue processing the incoming message. At the same time, gas is paid by the contract itself.
  • tvm.pubkey - allows to get information about a public key of the contract.
  • msg.pubkey - allows to get information about the public key of the account that calls the contract.

All necessary instructions are available in the solidity compiler documentation for FreeTON.

Within the framework of this article, we will create a contract that will be the implementation of the simplest wallet for storing tokens, but at the same time will not allow to withdraw funds until a certain point in time in the future. To do this, let's modify our contract:

Modified contract code

As we see from the code, now when deploying the contract will require you to specify the date until which all funds received on the account balance will be frozen, and the function used to send funds will be supplemented by a new condition, thanks to which our funds cannot be sent until the specified moment in the future.

Compile the contract

We have the code, now we need to compile our contract. To do this, we find it in the file browser and select Compile Solidity Contract from the context menu.

If everything is done correctly, then next to the contract code you will see 2 more files: Contract.abi.json and Contract.tvc (the name of the files depends on the name of the source file with the contract code, in our case it is Contract.sol), and in the output console we see the following:

Example of console output when smart contract compilation is successful

All in all, we created and compiled our first smart contract for the Free TON network, in the next part we will learn how to deploy our contract into the network and use it.

You can find the smart contract code from this article on GitHub.

Thank you for your attention! Subscribe to our channel and get more useful information.

Find out more about Everscale
Subscribe to our social networks and stay up to date with the latest news
SUBSCRIBE ON SOCIAL