Creating a DeBot in Free TON

Creating a DeBot in Free TON

In the latest article we have discussed how to create smart contracts and implement them in the blockchain. It’s time to go a little further and figure out what is a DeBot in Free TON and how to deploy your first simple DeBot. 

In this article we’ll take the example of the simplest DeBot, which can receive different data types from a user and react to keypresses in the Ton Surf interface. 

Basic concepts

DeBot is a dedicated smart contract, which allows interactions with another target smart contract.

Target smart contract is a smart contract, which is operated through the DeBot interface.

DeBot protocol is a set of rules which describe interaction between a browser and the DeBot.

DeBot engine or DEngine is a software component, which executes the DeBot code and processes the outcome according to a DeBot protocol. 

DeBot browser is a software interface for DeBot to interact with end users. 

By the time this article is written, there are two known DeBot browsers: TON Surf, graphical user interface and tonos-cli - console interface. In this article we will use tonos-cli for interacting with our DeBot and practice with TON Surf will be your homework ?

Last time we’ve already described how to set up a development environment and compile a contract, and in the second part we’ve managed to deploy the contract on the net. If you haven’t read it yet, we strongly recommend doing it because we will now need knowledge and skills gained from those articles. 

Hello World

As an example, we will go through an easy example for DeBot “Hello World”, which can be found in this github repository

The source code looks like this:

Source code of DeBot’s “Hello World

Source code of DeBot’s “Hello World”

Let’s have a look at the functions in this contract.

  • Start - a base method, located in Debot.sol file. It’s a DeBots entry point, i.e. a point in a program where the execution of a program begins.
  • getDebotInfo - a base method, located in Debot.sol file. It returns DeBot’s metadata such as Name, Author, Current version, Language etc. 
  • getRequiredInterfaces - a base method, located in Debot.sol. It returns a list of interfaces, used by a DeBot (DInterface), i.e. ways of user interaction, which must be in a DeBot browser. For instance, a DeBot can:
    • Receive user entry information;
    • Request data about other smart contracts;
    • Request transactions and messages; 
    • Get information from external subsystems (like file systems for example) and external devices (like NFC, cameras etc.)
    • Invoke libraries’ external functions, which help execute processes unsupported by a virtual machine. It can work with json, convert numbers to strings and vice-versa encrypt/decrypt/sign data. 

A list of available interfaces is regulated by DeBot Interface Specifications (IS) Consortium. DeBot browser in its turn can determine if it can correctly execute DeBot’s code.using this method.

  • setUserInput - is a user input handler and the method will be executed locally in a DeBot browser as a reaction to user input. In our case it will be input text, which will be requested by the input method in Terminal
  • setIcon - sets a DeBot icon in m_icon field, which can be used for displaying in DeBot browsers further. 
  • setABI is a base method, located in Debot.sol file, which sets ABI for DeBot. A Browser can get it by getDebotInfo method. 

Now let’s modify DeBot and expand it’s functionality by adding another interface, approved by the consortium, - Menu. Because it’s already in the source repository, we don’t need to download it separately. Please note that every DInterface must be followed by examples of the use, before it can be approved by the consortium and put into the repository. So let’s try the Menu and add it to our code, it will look like this: 

Modified “Hello World” DeBot code

Modified “Hello World” DeBot code 

Now, when we execute an entry point of the modified DeBot, the user will see a greeting and two options to choose. By choosing the first option the bot will terminate the session and by choosing the second option it will return with the same output until we choose the first option. Let’s compile and deploy our contract. We’ve already installed tondev utility from the last guide and we will use it now:

To compile a contract: 

tondev sol compile helloBot.sol

In the output we get updated helloBot.tvc and helloBot.abi.json files. For deployment we will use generated Test keys from the last article. 

To get the deployment address we can use the following command: 

tondev contract info helloDebot.abi.json -s Test

Let’s add a couple of tokens to the address and deploy DeBot’s smart contract. 

tondev contract deploy helloDebot.abi.json -s Test

On the next step we need to go to a contract and invoke setABI method:

tondev contract run -s Test -i "dabi:$(xxd -ps -c 20000 <"helloDebot.abi.json")" "helloDebot.abi.json" setABI

Now our DeBot is ready and for its execution let’s use tonos-cli utility. You can download it with tondev: 

tondev tonos-cli install

Let’s execute the DeBot’s code: 

tonos-cli --url debot fetch DEBOT_ADDRESS

Where DEBOT_ADDRESS is the address of the just deployed smart contract. 

If everything is done right, then the DeBot’s code will execute: 

An example of DeBot info output in tonos-cli

An example of DeBot info output in tonos-cli

Here you can see the DeBot information and run the script: 

Example of interaction with DeBot (Menu interface) in tonos-cli

Example of interaction with DeBot (Menu interface) in tonos-cli

You can find the final version of the DeBot code on GitHub

In the future articles we will focus in details on more complex DeBots, which request to target smart contracts. You can also familiarize yourself with already existing DeBots and available interfaces, approved by the consortium. 

Subscribe to our Telegram channel and get the latest news from Free TON world! 

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