<?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: Dan Volkov</title>
    <description>The latest articles on DEV Community by Dan Volkov (@dvlkv).</description>
    <link>https://dev.to/dvlkv</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.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F922011%2Fd4dba65a-5131-4b31-b49e-e6dee1464c8c.png</url>
      <title>DEV Community: Dan Volkov</title>
      <link>https://dev.to/dvlkv</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dvlkv"/>
    <language>en</language>
    <item>
      <title>Drawing conclusions from TON Hack Challenge</title>
      <dc:creator>Dan Volkov</dc:creator>
      <pubDate>Tue, 01 Nov 2022 12:15:31 +0000</pubDate>
      <link>https://dev.to/dvlkv/drawing-conclusions-from-ton-hack-challenge-1aep</link>
      <guid>https://dev.to/dvlkv/drawing-conclusions-from-ton-hack-challenge-1aep</guid>
      <description>&lt;p&gt;The TON Hack Challenge was held on October 23. &lt;br&gt;
There were several smartcontracts deployed in TON mainnet with synthetic security breaches. Every contract had 3000 or 5000 TON on balance, so the participant can hack it and get rewards immediately. &lt;/p&gt;

&lt;p&gt;As for me we hacked 6th task of this contest, but in this article I don't want to share my story, I want to tell you some thoughts about breaches in tasks.&lt;/p&gt;

&lt;p&gt;Source code and contest rules were hosted on Github &lt;a href="https://github.com/ton-blockchain/hack-challenge-1"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  1. Mutual fund
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Always check functions for &lt;code&gt;impure&lt;/code&gt; modifier.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The first task was very simple. The attacker can find that &lt;code&gt;authorize&lt;/code&gt; function was not &lt;code&gt;impure&lt;/code&gt;. Absence of this modifier allows compiler to skip calls to that function if it returns nothing or return value is unused.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;() authorize (sender) inline {
  throw_unless(187, equal_slice_bits(sender, addr1) | equal_slice_bits(sender, addr2));
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. Bank
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Always check for modifying/non-modifying methods.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;udict_delete_get?&lt;/code&gt; was called with &lt;code&gt;.&lt;/code&gt; instead &lt;code&gt;~&lt;/code&gt;, so the real dict was untouched.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(_, slice old_balance_slice, int found?) = accounts.udict_delete_get?(256, sender);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3. DAO
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Use signed integers if you really need it.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Voting power was stored in message as an integer. So attacker can send negative value during power transfer and get infinite voting power.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(cell,()) transfer_voting_power (cell votes, slice from, slice to, int amount) impure {
  int from_votes = get_voting_power(votes, from);
  int to_votes = get_voting_power(votes, to);

  from_votes -= amount;
  to_votes += amount;

  ;; No need to check that result from_votes is positive: set_voting_power will throw for negative votes
  ;; throw_unless(998, from_votes &amp;gt; 0);

  votes~set_voting_power(from, from_votes);
  votes~set_voting_power(to, to_votes);
  return (votes,());
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  4. Lottery
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Always randomize seed before doing &lt;code&gt;rand()&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Seed was brought from &lt;em&gt;logical time&lt;/em&gt; of the transaction and hacker can bruteforce logical time in the current block to win (cause LT is sequential in the borders of one block).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int seed = cur_lt();
int seed_size = min(in_msg_body.slice_bits(), 128);

if(in_msg_body.slice_bits() &amp;gt; 0) {
    seed += in_msg_body~load_uint(seed_size);
}
set_seed(seed);
var balance = get_balance().pair_first();
if(balance &amp;gt; 5000 * 1000000000) {
    ;; forbid too large jackpot
    raw_reserve( balance - 5000 * 1000000000, 0);
}
if(rand(10000) == 7777) { ...send reward... }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  5. Wallet
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Remember that everything is stored in blockchain.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The wallet was protected with password, it's hash was stored in contract data. But blockchain remembers everything - the password was in the transaction history.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Vault
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Always check for bounced messages.&lt;/p&gt;

&lt;p&gt;Don't forget about errors caused by standard functions.&lt;/p&gt;

&lt;p&gt;Make your conditions as strict as possible.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The vault has following code in the database message handler:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int mode = null(); 
if (op == op_not_winner) {
    mode = 64; ;; Refund remaining check-TONs
               ;; addr_hash corresponds to check requester
} else {
     mode = 128; ;; Award the prize
                 ;; addr_hash corresponds to the withdrawal address from the winning entry
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Vault does not have bounce handler and proxy message to database if user sends “check”. In database we can set &lt;code&gt;msg_addr_none&lt;/code&gt; as an award address cause &lt;code&gt;load_msg_address&lt;/code&gt; allows it. We are requesting check from vault, database tries to parse &lt;code&gt;msg_addr_none&lt;/code&gt; using &lt;code&gt;parse_std_addr&lt;/code&gt; and fails. Message bounces to the vault from database and the op is not &lt;code&gt;op_not_winner&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Better bank
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Never destroy account for fun.&lt;/p&gt;

&lt;p&gt;Make &lt;code&gt;raw_reserve&lt;/code&gt; instead of sending money to yourself.&lt;/p&gt;

&lt;p&gt;Think about possible race conditions.&lt;/p&gt;

&lt;p&gt;Be careful with hashmap gas consumption.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There were race condition in the contract: you can deposit money, then try to withdraw two times in concurrent messages. There is no guarantee that message with reserved money will be processed, so bank can shutdown after second withdrawal. After that the contract could be redeployed and then anybody can withdraw unowned money.&lt;/p&gt;

&lt;h2&gt;
  
  
  8. Dehasher
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Avoid executing third-party code in your contract.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;slice try_execute(int image, (int -&amp;gt; slice) dehasher) asm "&amp;lt;{ TRY:&amp;lt;{ EXECUTE DEPTH 2 THROWIFNOT }&amp;gt;CATCH&amp;lt;{ 2DROP NULL }&amp;gt; }&amp;gt;CONT"   "2 1 CALLXARGS";

slice safe_execute(int image, (int -&amp;gt; slice) dehasher) inline {
  cell c4 = get_data();

  slice preimage = try_execute(image, dehasher);

  ;; restore c4 if dehasher spoiled it
  set_data(c4); 
  ;; clean actions if dehasher spoiled them
  set_c5(begin_cell().end_cell());

  return preimage;
} 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is no way to safe execute third-party code in the contract, cause &lt;code&gt;out of gas&lt;/code&gt; exception cannot be handled by &lt;code&gt;CATCH&lt;/code&gt;. Attacker simply can &lt;code&gt;COMMIT&lt;/code&gt; any state of contract and raise &lt;code&gt;out of gas&lt;/code&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;I hope this article would shed light on the non-obvious rules for FunC developers. &lt;/p&gt;

</description>
      <category>blockchain</category>
      <category>security</category>
      <category>ton</category>
      <category>func</category>
    </item>
    <item>
      <title>Introduction to FunC: how to start developing on TON?</title>
      <dc:creator>Dan Volkov</dc:creator>
      <pubDate>Thu, 22 Sep 2022 13:47:47 +0000</pubDate>
      <link>https://dev.to/dvlkv/introduction-in-func-how-to-start-developing-in-ton-50hp</link>
      <guid>https://dev.to/dvlkv/introduction-in-func-how-to-start-developing-in-ton-50hp</guid>
      <description>&lt;p&gt;I've started developing in TON last year. At the beginning there were just a few articles and a bit of docs at &lt;a href="https://ton.org"&gt;ton.org&lt;/a&gt;. Now its ecosystem is growing, so many can find this blockchain interesting. In this article I want to share my experience with beginners through a guide with some useful examples. &lt;/p&gt;

&lt;p&gt;We will build &amp;amp; deploy a simple "Hello world" contract.&lt;/p&gt;

&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;p&gt;TON is PoS blockchain based on the actor model, interaction between contracts can be done only by messages, unlike EVM-powered chains, where you can call other contracts during computations. &lt;/p&gt;

&lt;p&gt;Any data stored in TON blockchain is stored in &lt;strong&gt;cells&lt;/strong&gt;. Cells can contain up to 1023 bits and 4 references to other cells. Cell itself is immutable, to read from it you need to create a &lt;strong&gt;slice&lt;/strong&gt;. Generally speaking, slices are read-only cell representation. To create a new cell here comes another type - &lt;strong&gt;builder&lt;/strong&gt;. Builder is the opposite of slice: write-only cell representation, which can be converted to cell. Cells can be serialised to &lt;strong&gt;Bag Of Cells (BoC)&lt;/strong&gt;. BoC will contain the root cell and the whole referenced cells' tree.&lt;/p&gt;

&lt;p&gt;Let's speak about tools. Now there are two languages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;fift - low-level language, has interpreter, so can be used for scripting&lt;/li&gt;
&lt;li&gt;func - high-level imperative language, compiled to fift&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;...and one more thing: &lt;a href="https://tact-lang.org/"&gt;Tact&lt;/a&gt; language, designed specifically for the beginners, is under development. &lt;br&gt;
In this article we'll focus only on FunC, but if you want, you may read about Fift yourself.&lt;/p&gt;
&lt;h2&gt;
  
  
  Installing build tools
&lt;/h2&gt;

&lt;p&gt;The best way (at the time of writing) to install actual versions of tools is to compile them yourself.&lt;/p&gt;

&lt;p&gt;UPDATE: now you can download precompiled binaries from official &lt;a href="https://github.com/ton-blockchain/ton/releases"&gt;ton-blockchain/ton&lt;/a&gt; releases.&lt;/p&gt;
&lt;h3&gt;
  
  
  Building func &amp;amp; fift from source
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Requirements&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;cmake ([For MacOS] XCode build Tools will be ok)&lt;/li&gt;
&lt;li&gt;make ([For MacOS] XCode build Tools will be ok)&lt;/li&gt;
&lt;li&gt;openssl ([For MacOS] can be installed via

&lt;code&gt;brew install openssl&lt;/code&gt;

)&lt;/li&gt;
&lt;li&gt;Clone TON &lt;a href="https://github.com/ton-blockchain/ton"&gt;monorepo&lt;/a&gt; and fetch submodules
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone https://github.com/ton-blockchain/ton
&lt;span class="nb"&gt;cd &lt;/span&gt;ton
git submodule init
git submodule update
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Create build folder&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;mkdir &lt;/span&gt;build
&lt;span class="nb"&gt;cd &lt;/span&gt;build
&lt;/code&gt;&lt;/pre&gt;




&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;Run cmake to create build environment&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# make sure that OPENSSL_ROOT_DIR is set&lt;/span&gt;
&lt;span class="c"&gt;# For MacOS users: export OPENSSL_ROOT_DIR=/usr/local/opt/openssl/&lt;/span&gt;
cmake ..
&lt;/code&gt;&lt;/pre&gt;




&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;Build func &amp;amp; fift&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight shell"&gt;&lt;code&gt;make func fift
&lt;span class="nb"&gt;cd &lt;/span&gt;crypto
&lt;/code&gt;&lt;/pre&gt;




&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;Install binaries to library directories or add them to &lt;code&gt;PATH&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cp &lt;/span&gt;fift /usr/local/bin/fift
&lt;span class="nb"&gt;cp &lt;/span&gt;func /usr/local/bin/func
&lt;/code&gt;&lt;/pre&gt;




&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now we are ready to start!&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a project
&lt;/h2&gt;

&lt;p&gt;I suggest using the Node.js environment for FunC projects, but feel free to use any other environment you want. Also, I suppose  My common project structure is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;├── build
│   └── ...build results
├── manage
│   └── deploy.ts
├── contracts
│   └── hello-world.fc
├── test
│   └── ...tests
├── build.sh
├── jest.config.js
├── package.json
└── tsconfig.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;build&lt;/em&gt; folder contains built result.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;manage&lt;/em&gt; folder contains scripts for deploy, dump smart contract data from blockchain, etc.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;test&lt;/em&gt; folder contains tests.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To init Node.js project with Typescript:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;yarn init
yarn add &lt;span class="nt"&gt;-D&lt;/span&gt; typescript ts-node @types/node
yarn run tsc &lt;span class="nt"&gt;--init&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Creating smart-contract
&lt;/h2&gt;

&lt;p&gt;A bit of theory: smart-contract in TON can handle two types of messages: &lt;em&gt;internal&lt;/em&gt; &amp;amp; &lt;em&gt;external&lt;/em&gt;. Internal messages are sent from one contract to another, externals - from the internet to contract. Another way to interact with contract is to call &lt;em&gt;get methods&lt;/em&gt;. Get methods are used for computing some  helpful information from the current contract's state and executed offchain. They are marked with the &lt;code&gt;method_id&lt;/code&gt; keyword.&lt;/p&gt;

&lt;p&gt;So, the contract consists of two entrypoints, &lt;code&gt;recv_internal&lt;/code&gt; and &lt;code&gt;recv_external&lt;/code&gt;. Also, contracts can contain as many get methods as you want.&lt;/p&gt;

&lt;p&gt;Our contract will respond to any internal message with &lt;em&gt;Hello, world!&lt;/em&gt; in comment and count how many messages were handled. Counter will be accessible through the get method.&lt;/p&gt;

&lt;p&gt;This is &lt;code&gt;hello-world.fc&lt;/code&gt; structure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;() recv_internal(int my_balance, int msg_value, cell in_msg_full, slice in_msg_body) impure {
    return ();
}

() recv_external(slice in_msg) impure {
    ;; Do not accept external messages
    throw(0xffff);
}

int counter() method_id {
    return 1;
}

slice owner() method_id {
    return null();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have three methods: &lt;code&gt;recv_internal&lt;/code&gt; for handling internal messages, &lt;code&gt;recv_external&lt;/code&gt; for handling external messages (rejects any external message) and &lt;code&gt;counter&lt;/code&gt; get method. &lt;/p&gt;

&lt;h3&gt;
  
  
  Installing &lt;code&gt;stdlib.fc&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;There are two ways to include &lt;code&gt;stdlib.fc&lt;/code&gt;: copy to project sources or install via yarn/npm.&lt;br&gt;
&lt;strong&gt;From sources&lt;/strong&gt;&lt;br&gt;
Download &lt;a href="https://github.com/ton-blockchain/ton/blob/master/crypto/smartcont/stdlib.fc"&gt;stdlib.fc&lt;/a&gt; and copy to the &lt;code&gt;src/contracts&lt;/code&gt;. Then, include it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include "stdlib.fc";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;From npm&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;yarn add ton-stdlib
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Include from node-modules:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include "../../node_modules/ton-stdlib/func/stdlib.fc";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Sending message
&lt;/h3&gt;

&lt;p&gt;Let's modify our &lt;code&gt;recv_internal&lt;/code&gt; to send a message back to the user.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;() recv_internal(int my_balance, int msg_value, cell in_msg_full, slice in_msg_body) impure {
    slice cs = in_msg_full.begin_parse();
    cs~skip_bits(4); ;; skip flags

    slice sender_address = cs~load_msg_addr();

    ;; Send message
    var message = begin_cell()
        .store_uint(0x10, 6)
        .store_slice(sender_address)
        .store_coins(0)
        .store_uint(0, 1 + 4 + 4 + 64 + 32 + 1 + 1)
        .store_uint(0, 32)
        .store_slice("Hello, world!")
        .end_cell();

    send_raw_message(message, 64);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At first, we should get the sender address. According to the &lt;a href="https://github.com/ton-blockchain/ton/blob/35d17249e6b54d67a5781ebf26e4ee98e56c1e50/crypto/block/block.tlb#L123"&gt;TL-b scheme&lt;/a&gt;, we should skip 4 bits (scheme tag &amp;amp; flags), after goes sender address.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;slice cs = in_msg_full.begin_parse();
cs~skip_bits(4);

slice sender_address = cs~load_msg_addr();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, we know the sender address and can send a response.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;;; Send message
var message = begin_cell()
    .store_uint(0x10, 6)         ;; flags
    .store_slice(sender_address) ;; destination
    .store_coins(0)              ;; amount 
    .store_uint(0, 1 + 4 + 4 + 64 + 32 + 1 + 1) ;; metadata
    .store_uint(0, 32)
    .store_slice("Hello, world!") ;; payload
    .end_cell();

send_raw_message(message, 64);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Message is structured that way: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;4 bits of flags

&lt;ul&gt;
&lt;li&gt;internal message tag (1 zero bit)&lt;/li&gt;
&lt;li&gt;ihr_disabled (&lt;em&gt;Instant Hypercube Routing&lt;/em&gt;, learn more at &lt;a href="https://ton-blockchain.github.io/docs/tblkch.pdf"&gt;tblkch.pdf&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;bounce (can the receiving part bounce a message?)&lt;/li&gt;
&lt;li&gt;bounced (means that receiving part failed, so the message returned to contract)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Source address (2 zero bits, address is none)&lt;/li&gt;
&lt;li&gt;Destination address&lt;/li&gt;
&lt;li&gt;Message metadata&lt;/li&gt;
&lt;li&gt;Content&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;The message cell layout better described &lt;a href="https://ton.org/docs/#/smart-contracts/messages?id=sending-messages"&gt;here&lt;/a&gt;. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We are sending message with flag &lt;em&gt;64&lt;/em&gt;, it means that the amount increased by the remaining value of the inbound message.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You can find more about &lt;code&gt;send_raw_message&lt;/code&gt; flags &lt;a href="https://ton.org/docs/#/func/stdlib?id=send_raw_message"&gt;here&lt;/a&gt;. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Message will be sent after computations, because transactions are split into different phases: storage, credit, compute, action and bounce. Phases described at the &lt;a href="https://ton.org/docs/#/smart-contracts/tvm_overview"&gt;brief TVM overview&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Updating counter
&lt;/h3&gt;

&lt;p&gt;After sending a message we need to update the counter.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var cs = get_data().begin_parse();
var counter = cs~load_uint(32);

set_data(
    begin_cell()
        .store_uint(counter + 1, 32)
        .store_slice(cs)
    .end_cell()
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At first, we are getting the old counter from the contract's data. Then, we store the new data with the updated counter.&lt;/p&gt;

&lt;h3&gt;
  
  
  Adding get-methods
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;counter&lt;/code&gt; should return just single integer with the current state of counter, &lt;code&gt;owner&lt;/code&gt; should return owner.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int counter() method_id {
    var data = get_data().begin_parse();
    return data~load_uint(32);
}

slice owner() method_id {
    var data = get_data().begin_parse();
    data~skip_bits(32);
    return data~load_msg_addr();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Result
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include "../../node_modules/ton-stdlib/func/stdlib.fc";

() recv_internal(int my_balance, int msg_value, cell in_msg_full, slice in_msg_body) impure {
    slice cs = in_msg_full.begin_parse();
    cs~skip_bits(4);

    slice sender_address = cs~load_msg_addr();

    ;; Send message
    var message = begin_cell()
        .store_uint(0x10, 6) ;; 010000
        .store_slice(sender_address)
        .store_coins(0)
        .store_uint(0, 1 + 4 + 4 + 64 + 32 + 1 + 1)
        .store_uint(0, 32)
        .store_slice("Hello, world!")
        .end_cell();

    send_raw_message(message, 64);

    ;; Update counter
    var cs = get_data().begin_parse();
    var counter = cs~load_uint(32);

    set_data(
        begin_cell()
            .store_uint(counter + 1, 32)
            .store_slice(cs)
        .end_cell()
    );
}

() recv_external(slice in_msg) impure {
    throw(0xffff);
}

int counter() method_id {
    var data = get_data().begin_parse();
    return data~load_uint(32);
}

slice owner() method_id {
    var data = get_data().begin_parse();
    data~skip_bits(32);
    return data~load_msg_addr();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Writing &lt;em&gt;build.sh&lt;/em&gt;
&lt;/h2&gt;

&lt;p&gt;To deploy &amp;amp; test contract we need to compile &amp;amp; serialise it to BoC. It can be done by single command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;func &lt;span class="nt"&gt;-Wbuild&lt;/span&gt;/hello-world.boc src/contracts/hello-world.fc | fift &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; /dev/null
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At first, we build hello-world.fc using &lt;code&gt;func&lt;/code&gt;, with the flag &lt;code&gt;-W&lt;/code&gt;. Flag -W appends Fift code for serialisation of the resulting contract to BoC. &lt;code&gt;func&lt;/code&gt; emits fift code that is further passed to &lt;code&gt;fift&lt;/code&gt; interpreter. Then, we forward interpreter's &lt;code&gt;stdout&lt;/code&gt; to &lt;em&gt;/dev/null&lt;/em&gt; to avoid messy outputs in the console.&lt;/p&gt;

&lt;h2&gt;
  
  
  Deploying
&lt;/h2&gt;

&lt;p&gt;There are several ways to deploy contract: via &lt;a href="https://github.com/disintar/toncli"&gt;&lt;code&gt;toncli&lt;/code&gt;&lt;/a&gt;, via fift &amp;amp; lite-client, via &lt;a href="https://github.com/toncenter/tonweb"&gt;&lt;code&gt;tonweb&lt;/code&gt;&lt;/a&gt;, via &lt;a href="https://github.com/tonwhales/ton"&gt;&lt;code&gt;ton&lt;/code&gt;&lt;/a&gt;, etc.&lt;/p&gt;

&lt;p&gt;In this guide I we will deploy contract to the sandbox network using &lt;a href="https://apps.apple.com/app/ton-development-wallet/id1607857373"&gt;Tonhub Sandbox&lt;/a&gt; and &lt;a href="https://github.com/tonwhales/ton"&gt;&lt;code&gt;ton&lt;/code&gt;&lt;/a&gt; library.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://developers.tonhub.com/docs/sandbox"&gt;Get some Sandbox coins&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Install dependencies
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;yarn add ton-core ton qrcode-terminal qs
yarn add &lt;span class="nt"&gt;-D&lt;/span&gt; @types/qs @types/qrcode-terminal
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Add &lt;code&gt;manage/deploy-sandbox.ts&lt;/code&gt; script
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Cell&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Builder&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;storeStateInit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;contractAddress&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;StateInit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;toNano&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;beginCell&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Address&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ton-core&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;readFileSync&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;fs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;qs&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;qs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;qrcode&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;qrcode-terminal&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Create a data cell similar to the initial contract state&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dataCell&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;beginCell&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;storeUint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="c1"&gt;// counter&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;storeAddress&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Address&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;INSERT_ADDRESS&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;endCell&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// Load code from build&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;codeCell&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Cell&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fromBoc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;readFileSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./build/hello-world.boc&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="c1"&gt;// Calculate address from code &amp;amp; data&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;address&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;contractAddress&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="na"&gt;code&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;codeCell&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;dataCell&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;// Prepare init message&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;initCell&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Builder&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nx"&gt;storeStateInit&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="na"&gt;code&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;codeCell&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;dataCell&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;})(&lt;/span&gt;&lt;span class="nx"&gt;initCell&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Encode link to deploy contract&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;link&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://test.tonhub.com/transfer/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;address&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;testOnly&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;?&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;qs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="na"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Deploy contract&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="na"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;toNano&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="na"&gt;init&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;initCell&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;asCell&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;toBoc&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;idx&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nx"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;base64&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Address: &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;address&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;testOnly&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;}));&lt;/span&gt;

&lt;span class="nx"&gt;qrcode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;generate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;link&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;small&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;code&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="err"&gt;  &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;code&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Run script &amp;amp; scan qr code in the Sandbox wallet
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;yarn ts-node manage/deploy-sandbox.ts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Testing contract
&lt;/h2&gt;

&lt;p&gt;Send any amount to the address you've got from the deployment step and you will get "Hello, world!" back. Check the transactions in the &lt;a href="https://tonsandbox.com/explorer"&gt;sandbox explorer&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting counter &amp;amp; owner
&lt;/h2&gt;

&lt;p&gt;Create a &lt;code&gt;.ts&lt;/code&gt; file in the &lt;code&gt;manage&lt;/code&gt; folder and name it like &lt;code&gt;dump-sandbox.ts&lt;/code&gt;. Replace address to yours. Create &lt;code&gt;TonClient&lt;/code&gt; with a sandbox endpoint and call get methods.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;TonClient&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ton&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Address&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ton-core&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;address&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Address&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;YOUR CONTRACT ADDRESS&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Create sandbox API client&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;TonClient&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
        &lt;span class="na"&gt;endpoint&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://sandbox.tonhubapi.com/jsonRPC&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;

    &lt;span class="c1"&gt;// Call get method and log the result&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;runMethod&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;address&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;counter&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Counter&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;parseInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;owner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;runMethod&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;address&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;owner&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;ownerCell&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Cell&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fromBoc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;owner&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;base64&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;ownerAddress&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ownerCell&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;beginParse&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;loadAddress&lt;/span&gt;&lt;span class="p"&gt;()?.&lt;/span&gt;&lt;span class="nx"&gt;toFriendly&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;testOnly&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Address: &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ownerAddress&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;})()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Congratulations
&lt;/h2&gt;

&lt;p&gt;You've set up environment, built your first contract on TON and tried it in the sandbox network 🚀&lt;/p&gt;

&lt;h3&gt;
  
  
  Further reading
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://learnxinyminutes.com/docs/func/"&gt;Learn FunC in Y minutes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://ton.org/docs/#/"&gt;ton.org docs&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://ton.org/docs/#/howto/smart-contract-guidelines"&gt;Smart-contract guidelines&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Examples: &lt;a href="https://github.com/ton-blockchain/token-contract"&gt;nft &amp;amp; ft contracts&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Examples: &lt;a href="https://github.com/ton-blockchain/ton/blob/master/crypto/smartcont/pow-testgiver-code.fc"&gt;PoW giver contract&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Examples: &lt;a href="https://github.com/ton-blockchain/wallet-contract"&gt;wallet v4 contract&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="https://ton-blockchain.github.io/docs/tblkch.pdf"&gt;tblkch.pdf&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>web3</category>
      <category>ton</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
