<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Vladimir Támara Patiño</title>
    <description>The latest articles on DEV Community by Vladimir Támara Patiño (@vtamara).</description>
    <link>https://dev.to/vtamara</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F498902%2F55e7d209-3683-4778-bc46-819cd96ebabd.png</url>
      <title>DEV Community: Vladimir Támara Patiño</title>
      <link>https://dev.to/vtamara</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vtamara"/>
    <language>en</language>
    <item>
      <title>Developing and testing a simple smart contract with toncli</title>
      <dc:creator>Vladimir Támara Patiño</dc:creator>
      <pubDate>Thu, 12 Jan 2023 11:17:56 +0000</pubDate>
      <link>https://dev.to/vtamara/developing-and-testing-a-simple-smart-contract-with-toncli-2le5</link>
      <guid>https://dev.to/vtamara/developing-and-testing-a-simple-smart-contract-with-toncli-2le5</guid>
      <description>&lt;h1&gt;
  
  
  1. Introduction
&lt;/h1&gt;

&lt;p&gt;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).&lt;/p&gt;

&lt;p&gt;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 &lt;a href="https://dorahacks.io/hackathon/hack-a-tonx/detail"&gt;https://dorahacks.io/hackathon/hack-a-tonx/detail&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since the first contest in April 2022 I have been porting the TON tools to my main operating system that is &lt;a href="https://aprendiendo.pasosdejesus.org/"&gt;adJ&lt;/a&gt; (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 &lt;a href="http://adj.pasosdejesus.org/pub/AprendiendoDeJesus/endesarrollo/"&gt;tools I used in December&lt;/a&gt; and &lt;a href="https://github.com/pasosdeJesus/adJ/tree/ADJ_7_2/arboldes/usr/ports/mystuff/net/ton"&gt;the source code&lt;/a&gt;). 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 &lt;a href="https://ton.org/docs/develop/smart-contracts/environment/installation"&gt;https://ton.org/docs/develop/smart-contracts/environment/installation&lt;/a&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  2. Fundamentals of the TON Blockchain
&lt;/h1&gt;

&lt;p&gt;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 &lt;a href="https://github.com/ton-blockchain/ton"&gt;https://github.com/ton-blockchain/ton&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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 &lt;a href="https://coinmarketcap.com/currencies/toncoin/"&gt;Coinmarketcap&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The package &lt;code&gt;ton&lt;/code&gt; for adJ/OpenBSD 7.2 includes:&lt;/p&gt;

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

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export FIFTPATH=/usr/local/lib/fift:/usr/local/share/ton/smartcont
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h1&gt;
  
  
  3. Preparing toncli 0.0.43
&lt;/h1&gt;

&lt;p&gt;&lt;code&gt;toncli&lt;/code&gt; is a python package that facilitates the development and testing of smart contracts for the TON blockchain.&lt;/p&gt;

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

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

&lt;p&gt;Once you have the &lt;code&gt;ton-toncli&lt;/code&gt; package installed, install &lt;code&gt;toncli&lt;/code&gt; like this:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;doas pkg_add py3-pipdoas pip install bitstring==3.1.9doas pip install toncli
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;After that, you should be able to run&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;toncli
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;which on first run will ask you:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Path of &lt;code&gt;func&lt;/code&gt;. Use &lt;code&gt;/usr/local/bin/ton-toncli/func&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Path of &lt;code&gt;fift&lt;/code&gt;. Use &lt;code&gt;/usr/local/bin/ton-toncli/fift&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Path of &lt;code&gt;lite-client&lt;/code&gt;. Use &lt;code&gt;/usr/local/bin/ton-toncli/lite-client&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Your responses will be in &lt;code&gt;\~/.config/toncli/config.ini&lt;/code&gt; (that you could modify later if needed).&lt;/p&gt;
&lt;h1&gt;
  
  
  4. Example of a smart contract and a test for it
&lt;/h1&gt;

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

&lt;p&gt;From the terminal you can prepare a directory structure with:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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 .
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;You will see the structure of a typical toncli project including:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.
├── build
├── fift         Source code in fift
├── func
│ └── code.fc      Code with the function to test
├── project.yaml Project structure for toncli
└── tests
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;We can rearrange this example project a bit for our case of a single function in FunC with:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rm -rf fift # we do not need the example in fift
mv func/code.func func/solution.fc # The .fc extension is widely used
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Edit &lt;code&gt;project.yaml&lt;/code&gt; so that it has the following content indicating that the func code to test is in &lt;code&gt;func/solution.fc&lt;/code&gt; and the tests are in &lt;code&gt;tests/tests.fc&lt;/code&gt;:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;contract:func:
  - func/solution.fc
tests:
  - tests/tests.fc
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;The content of &lt;code&gt;func/solution.fc&lt;/code&gt; implements a well known method to calculate the greatest common divisor between two numbers:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;And the content of &lt;code&gt;tests/tests.fc&lt;/code&gt; will simply calculate the greatest common divisor of 30 and 12 and assert that it is 6:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;After this you can run the tests with:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;toncli run_tests
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;or simpler&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;make
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;once you create a &lt;strong&gt;Makefile&lt;/strong&gt; with the following content:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;all:
        toncli run_tests
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;It should present &lt;code&gt;SUCCESS&lt;/code&gt; as result of the test, like the following screenshot:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aSDsnqcT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u4seyy1lt1ht7292w5qd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aSDsnqcT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u4seyy1lt1ht7292w5qd.png" alt="Screenshot of terminal running successful test" width="499" height="316"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  5. Conclusion
&lt;/h1&gt;

&lt;p&gt;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 &lt;a href="https://t.me/tonsociety/155"&gt;encouraged&lt;/a&gt; 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.&lt;/p&gt;

&lt;h1&gt;
  
  
  6. References
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;To learn about TON: &lt;a href="https://ton.org/docs/#/docs"&gt;https://ton.org/docs/#/docs&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;To learn FunC: &lt;a href="https://ton.org/docs/develop/func/overview"&gt;https://ton.org/docs/develop/func/overview&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;The Ton Connect standard is available att &lt;a href="https://github.com/ton-connect"&gt;https://github.com/ton-connect&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;The design documents of the blockchain and TVM are available at &lt;a href="https://ton.org/docs/ton.pdf"&gt;https://ton.org/docs/ton.pdf&lt;/a&gt; and &lt;a href="https://ton.org/docs/tvm.pdf"&gt;https://ton.org/docs/tvm.pdf&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;You can review the tasks and solutions of &lt;a href="https://github.com/ton-blockchain/func-contest1"&gt;1st&lt;/a&gt;, &lt;a href="https://github.com/ton-blockchain/func-contest2"&gt;2nd&lt;/a&gt; and &lt;a href="https://github.com/ton-blockchain/func-contest3"&gt;3rd&lt;/a&gt; FunC contests.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;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 &lt;a href="http://pasosdejesus.github.io/usuario_adJ/conf-programas.html#ton"&gt;initial spanish version&lt;/a&gt; of it.&lt;/p&gt;

</description>
      <category>ton</category>
      <category>blockchain</category>
      <category>programming</category>
      <category>adj</category>
    </item>
  </channel>
</rss>
