Logo

dev-resources.site

for different kinds of informations.

Developing and testing a simple smart contract with toncli

Published at
1/12/2023
Categories
ton
blockchain
programming
adj
Author
vtamara
Categories
4 categories in total
ton
open
blockchain
open
programming
open
adj
open
Author
7 person written this
vtamara
open
Developing and testing a simple smart contract with toncli

1. Introduction

Thanks to God and the fine people of TON Foundation in 2022 I had the opportunity to participate in 3 contests to learn the FunC language in April, August and December. Although I was in places 32nd, 47th and 31st, I earned 74, 117 and 280 TONS in each opportunity (at today’s rate that would total more than US$1030).

Soon there will be a new contest open to everybody that will require FunC, WEB technologies, and knowledge of the TON blockchain infrastructure. The contest will start on 30th January and it will end in March. The challenge will be to develop something cool and the awards for the first places are really high (I wish to advance on something about education and evangelization). See the full announcement and how to participate at https://dorahacks.io/hackathon/hack-a-tonx/detail

Since the first contest in April 2022 I have been porting the TON tools to my main operating system that is adJ (a distribution of OpenBSD), where I feel more secure my crypto-wallet and on it I could participate in the second and third contests (see the tools I used in December and the source code). For that reason in this article I will describe how to use the TON tools on adJ/OpenBSD, but the principles explained here can be applied to the tools for Windows, Mac and Ubuntu available at https://ton.org/docs/develop/smart-contracts/environment/installation.

2. Fundamentals of the TON Blockchain

TON is an open source blockchain designed to process many transactions with low fees. The initial design is due to Nikolai Durov –who also designed and operates Telegram– but since 2020 TON is operated and implemented by other people from the community. Its sources are in https://github.com/ton-blockchain/ton

In this blockchain the transactions are specified in smart contracts that are programs for the TON Virtual Machine (TVM), which runs in rounds on various servers called validators. The validators execute contracts charging fees to each contract proportional to the computational effort of the execution, and sending messages that contracts produce to other contracts.

Then each smart contract is like a unit of work for a validator in a round, which must be executed in a limited time and pay fees for its execution, for the storage space it uses and for sending messages to other contracts β€” messages that will be processed asynchronously in a different round of the validators.

The main currency in this blockchain is TON (or TONcoin) used by the validators. You can see its current exchange rate and history for example at Coinmarketcap.

The package ton for adJ/OpenBSD 7.2 includes:

  • Assembly language library located in /usr/local/lib/fift
  • Multiple contracts in /usr/local/share/ton/smartcont
  • fiftto execute instructions on the TON virtual machine and debug and to create binaries for that virtual machine from sources in fift assembly language.
  • func to compile to fift assembly language from sources in higher level language FunC.
  • lite-client to connect to a TON network and interact with validators.
  • tonlib-cli Use he tonlib library from the terminal, for example to operate with wallet(s).
  • validator-engine and validator-engine-console to operate a validator node

It is important that in your \~/.profile file (or equivalent as \~/.zshrc.local if you are using zsh) you add:

export FIFTPATH=/usr/local/lib/fift:/usr/local/share/ton/smartcont
Enter fullscreen mode Exit fullscreen mode

3. Preparing toncli 0.0.43

toncli is a python package that facilitates the development and testing of smart contracts for the TON blockchain.

Since the initial design of the TVM did not include many development or debugging aids, to use recent versions of toncli, you will have to use a fork of the original TVM with more instructions for debugging: https://github.com/SpyCheese/ton/tree/toncli-local

adJ/OpenBSD also includes a package with that fork called ton-toncli whose files are like those in the ton package but in different paths (i.e /usr/local/bin/ton-toncli, /usr/local/lib/ton-toncli and /usr /local/share/ton-toncli/).

Once you have the ton-toncli package installed, install toncli like this:

doas pkg_add py3-pipdoas pip install bitstring==3.1.9doas pip install toncli
Enter fullscreen mode Exit fullscreen mode

After that, you should be able to run

toncli
Enter fullscreen mode Exit fullscreen mode

which on first run will ask you:

  1. Path of func. Use /usr/local/bin/ton-toncli/func
  2. Path of fift. Use /usr/local/bin/ton-toncli/fift
  3. Path of lite-client. Use /usr/local/bin/ton-toncli/lite-client

Your responses will be in \~/.config/toncli/config.ini (that you could modify later if needed).

4. Example of a smart contract and a test for it

We will implement a smart contract that calculates the greatest common divisor between two numbers (problem proposed in the second programming contest of FunC)

From the terminal you can prepare a directory structure with:

toncli start wallet # Start project with example of a wallet
toncli start wallet # Start project with example of a wallet
mv wallet tests_mdc_func # Renamecd tests_mdc_funcfind .
Enter fullscreen mode Exit fullscreen mode

You will see the structure of a typical toncli project including:

.
β”œβ”€β”€ build
β”œβ”€β”€ fift         Source code in fift
β”œβ”€β”€ func
β”‚ └── code.fc      Code with the function to test
β”œβ”€β”€ project.yaml Project structure for toncli
└── tests
Enter fullscreen mode Exit fullscreen mode

We can rearrange this example project a bit for our case of a single function in FunC with:

rm -rf fift # we do not need the example in fift
mv func/code.func func/solution.fc # The .fc extension is widely used
Enter fullscreen mode Exit fullscreen mode

Edit project.yaml so that it has the following content indicating that the func code to test is in func/solution.fc and the tests are in tests/tests.fc:

contract:func:
  - func/solution.fc
tests:
  - tests/tests.fc
Enter fullscreen mode Exit fullscreen mode

The content of func/solution.fc implements a well known method to calculate the greatest common divisor between two numbers:

And the content of tests/tests.fc will simply calculate the greatest common divisor of 30 and 12 and assert that it is 6:

After this you can run the tests with:

toncli run_tests
Enter fullscreen mode Exit fullscreen mode

or simpler

make
Enter fullscreen mode Exit fullscreen mode

once you create a Makefile with the following content:

all:
        toncli run_tests
Enter fullscreen mode Exit fullscreen mode

It should present SUCCESS as result of the test, like the following screenshot:

Screenshot of terminal running successful test

5. Conclusion

Learning FunC and about TON infrastructure has been fun and economically rewarding even with the relatively short time I have invested into it (well thanks to God I have family, church, jobs and talent in programming). In the process I have met generous people from the TON Foundation (that even encouraged me to write this article) as well as participants of the contests who have helped me in the contest channel even during the contests. I’m very thankful and I pray God will keep their generous heart and help them to know Jesus as personal Saviour and King.

6. References

I want to thank for the feedback for this article to Angel Rivera and in general the welcoming people of the Telegram channel @openbsd_mx where I presented some time ago the initial spanish version of it.

Featured ones: