Developing and testing a simple smart contract with toncli
Tags: ton, blockchain, programming, adj
Published: 2023-01-12 11:17:56

# 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](https://dorahacks.io/hackathon/hack-a-tonx/detail "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](https://aprendiendo.pasosdejesus.org/) (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](http://adj.pasosdejesus.org/pub/AprendiendoDeJesus/endesarrollo/) and [the source code](https://github.com/pasosdeJesus/adJ/tree/ADJ_7_2/arboldes/usr/ports/mystuff/net/ton)). 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](https://ton.org/docs/develop/smart-contracts/environment/installation "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](https://github.com/ton-blockchain/ton "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](https://coinmarketcap.com/currencies/toncoin/).

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`
* `fift`to 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

# 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](https://github.com/SpyCheese/ton/tree/toncli-local "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

After that, you should be able to run

    toncli

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 .

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

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

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

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

{% gist https://gist.github.com/vtamara/82d8336880b42b0a553a10d9ce791add file=solution.fc %}

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

{% gist https://gist.github.com/vtamara/c6b3a522de6759ca9a87359257e8fbcd file=test.fc %}

After this you can run the tests with:

    toncli run_tests

or simpler

    make

once you create a **Makefile** with the following content:

    all:
            toncli run_tests

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


![Screenshot of terminal running successful test](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u4seyy1lt1ht7292w5qd.png)


# 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](https://t.me/tonsociety/155) 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

* To learn about TON: [https://ton.org/docs/#/docs](https://ton.org/docs/#/docs "https://ton.org/docs/#/docs")
* To learn FunC: [https://ton.org/docs/develop/func/overview](https://ton.org/docs/develop/func/overview "https://ton.org/docs/develop/func/overview")
* The Ton Connect standard is available att [https://github.com/ton-connect](https://github.com/ton-connect "https://github.com/ton-connect")
* The design documents of the blockchain and TVM are available at [https://ton.org/docs/ton.pdf](https://ton.org/docs/ton.pdf "https://ton.org/docs/ton.pdf") and [https://ton.org/docs/tvm.pdf](https://ton.org/docs/tvm.pdf "https://ton.org/docs/tvm.pdf")
* You can review the tasks and solutions of [1st](https://github.com/ton-blockchain/func-contest1), [2nd](https://github.com/ton-blockchain/func-contest2) and [3rd](https://github.com/ton-blockchain/func-contest3) FunC contests.

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](http://pasosdejesus.github.io/usuario_adJ/conf-programas.html#ton) of it.