How-to: Getting started with smart contracts. Hello world. : TON Tips

How-to: Getting started with smart contracts. Hello world.

#guides

Posted by Team Tips on 05 Jun 2021

In the last article, we set up the environment and installed the demo project via TONDEV.

If you haven't seen the article, be sure to check it out:

We created a demo project with the command tondev js demo hello. A repository with this demo is also available on GitHub.

First, you need to install the dependencies. Run npm install command in the project directory. The project can now be launched. But let's first go through the structure of the project.

Here we see:

  • Contract Hello.sol
  • JS wrapper HelloContract.js, which stores ABI и TVC
  • Main file index.js

Contract

It is primitive contract with just 2 functions: touch() and getTimestamp(). The first one updates local variable, the second one returns its value.

JS code

The index.js file is very interesting. In fact, all interaction with the contract is automated with this file. !

TON SDK is a very powerful tool, which takes blockchain development to a new level. With it, you can both develop full-fledged Dapps, and use it as a tool for unit tests when developing smart contracts.

Let's go back to the code. The main function implements all the work with the contract.

  1. The contract code is loaded and keys are generated
  2. The future address of the contract is calculated
  3. Contract deployed using giver
  4.  touch function called
  5.  getTimestamp function called

As you can see, everything is pretty simple.

Deploy

Now let's make sure everything works as it should. Docker with TON OS SE still running? In the last article, we looked at setting it up. We need a local blockchain.

The contracts are already compiled and we don't need to change anything. Let's just run npm run start

In the console, you can verify that the contract has been loaded into the local network and executed. Awesome!

Editing contract

Well, but how to add something of your own here? Let's create a new function in the contract for example.

function HelloWorld() public pure returns (string) {
return "Hello World!";
}

Now the contract needs to be compiled and packaged.

tondev sol compile Hello.sol

tondev js wrap Hello.abi.json

Great, it remains to configure our JS code to work with the updated contract.

Let's add these lines to the end of the main function of the index.js file

response = await helloAcc.runLocal("HelloWorld", {});

console.log(`Contract hello output`,Buffer.from(response.decoded.output.value0 , 'hex').toString('utf8'));

Finally, let's run npm run start