Creating a smart contract in Free TON (Part 2)

Creating a smart contract in Free TON (Part 2)

In the first part, we considered how to set up an environment for developing contracts, sorted out the simplest example of a wallet with freezing funds and successfully compiled it. If you haven't read the first part yet, don't miss the chance to do it.

In the second part of the article, we will deploy (send to blockchain) our smart contract to the test network (net.ton.dev), and test its functionality using the available tools.

Preparation of the necessary utilities

At the moment of publishing this article, the TONDev extension for Visual Studio Code does not support signing contracts, so we will use the tondev console utility. To install and use it, we will need a NodeJS (at least the 10th version), which is available for installation on the official website

The installation process goes beyond the frameworks of this article, so we continue further, believing that this step has already been completed.

We proceed to the installation of the TONDev utility, you can do this using the command line:

npm install -g tondev

Now all the resources required to deploy the contract are set and we go to the next step.

Deployment of the contract

Since we just want to test our contract, we will deploy it into the test network (net.ton.dev). The currency of this network is rubies (unlike crystals in the main network), and they can be obtained simply by requesting from the relevant contract, the most convenient thing to do is to use TON Surf. You can find out more about how to enter developer mode and install TON Surf in our article.

By default, tondev uses the network for developers, but just in case we will make sure of this before the deployment of our contract. Use the following command

tondev network list
Result of tondev network list command

If you have another default network installed, you can change it:

tondev network default dev

Next, we generate a key pair to sign the contract:

tondev signer generate Test

where Test is any name under which the key pair will be stored for further use

You can view a list of all generated keys using the command:

tondev signer list
Result of tondev signer list command

Now we need to send tokens to the future address of the contract, since when the contract will be deployed, some amount of funds will be charged from this address. To begin with, we find out the address of the contract, it is calculated locally from the compiled .tvc file and the newly generated public key. You can do this using the command:

tondev contract info Contract.abi.json -s Test
Result of tondev contract info Contract.abi.json -s Test command

Note that the -s parameter, which indicates which generated key pair, should be used for signing when further interacting with the contract. In our case, it is Test. Each following command will include this option.

We send several tokens (for example, 10) to the address received in the previous step. Be careful when sending money, make sure the address is written correctly, and make sure you send money in the test network. The sending process is irreversible!

After we sent the tokens to the address, and the transaction reached the addressee (almost instantly, thanks Free TON 😏), it remains only to deploy our contract:

tondev contract deploy Contract.abi.json -i date:1621620000 -s Test

Note that the -i parameter is responsible for the data that will be passed to the constructor (the method that will be called when the contract is deployed into the network). In our case, it accepts a single parameter - the date of unfreezing of funds in the format UNIX-timestamp

Result of tondev contract deploy Contract.abi.json -i date:1621620000 -s Test command

We have successfully deployed our contract and will now re-request information on it: 

tondev contract info Contract.abi.json -s Test
Result of tondev contract info Contract.abi.json -s Test command

Now we see that the contract account is active and ready for use. Then, we will try to call the contract function to transfer funds to an address. The utility tondev provides a convenient interactive interface, which we will use:

tondev contract run Contract.abi.json -s Test

The first call to the contract function is expected to fail, since the unfreezing time has not come yet (note that the amount to be translated is indicated in nanotokens, that is, the number multiplied by 10 to 9 degrees):

Result of the first tondev contract run Contract.abi.json -s Test command

Expect unfreezing time and try again:

tondev contract run Contract.abi.json -s Test
Result of repeated execution of tondev contract run Contract.abi.json -s Test command

Now, as we see, the transaction was successfully sent, which means that our contract did an excellent job.

Conclusion

Writing smart contracts is no longer as difficult as it seems at first glance. Try, learn from mistakes, and let luck always be on your side. Soon we will continue the series of articles about smart contracts, subscribe to our channel and follow the updates!

Thank you for your attention.

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