TLDR:
The Open Network (TON) is a decentralized and open platform comprising several components, including TON Blockchain, TON DNS, TON Storage, and TON Sites. Originally developed by the Telegram team, TON aims to provide fast, secure, and user-friendly decentralized services. Its core protocol, TON Blockchain, connects the underlying infrastructure to form the greater TON Ecosystem.
TON is focused on achieving widespread cross-chain interoperability within a highly scalable and secure framework. Designed as a distributed supercomputer or “superserver,” TON provides various products and services to contribute to developing the decentralized vision for the new internet. This makes it well-suited for a wide range of decentralized applications.
One of the unique features of TON smart contracts is that they can receive messages and do some action based on it; we’ll leverage this in the smart contract we’ll develop.
This tutorial will guide you through creating, building, testing, deploying, and interacting with a simple storage contract on the TON blockchain. We’ll use the Blueprint SDK.
The Blueprint SDK is an essential tool for developers working with the TON blockchain. It provides a comprehensive suite of tools and libraries that simplify the process of writing, testing, and deploying smart contracts.
Start for free and get your app to production levels immediately. No credit card required. You can sign up with your GitHub, X, Google, or Microsoft account.
This will be an introductory project where we use the Blueprint SDK to develop, test, and deploy a simple storage-style smart contract where we can save a number and increment a counter.
The smart contract is written in the TACT language, TON’s smart contract language, and is comparable to Solidity for EVM-based chains.
Let’s create a new TON project and run the initialization command in a new directory.
Select an empty project in TACT.
When you create a new project using the Blueprint SDK, the directory structure is organized to help you efficiently manage your smart contract development process. Here’s a brief overview of the main directories and their purposes:
build/: This directory contains the compiled artifacts of your smart contracts. After you run the build command, the output files will be stored here, including the compiled contract bytecode and other related artifacts.
contracts/: This is where you write your smart contracts. All .tact
files containing the contract code are placed in this directory. For instance, the SimpleStorage
contract will reside here.
scripts/: This directory is used for deployment and interaction scripts. These scripts facilitate deploying your contracts to the TON blockchain and interacting with them. For example, scripts to deploy the contract or fetch data from the contract will be placed here.
tests/: This directory holds the test files for your smart contracts. Here, you can write tests to ensure your contracts behave as expected. The default test script verifies contract deployment, and you can extend it to test additional functionalities.
wrappers/: This directory contains the TypeScript wrappers generated for your contracts. These wrappers provide a convenient way to interact with your contracts in a type-safe manner from your scripts and tests.
Now, we are ready to start developing the contract. You’ll find a .tact
contract in the contracts
directory, paste the following code in it.
Here is a breakdown of how the code works.
Imports and Message Declaration
import "@stdlib/deploy";
.Save
is declared, which contains an amount
field of type Int
(aliased as uint32
).Contract Declaration
SimpleContract
is declared with the Deployable
trait, allowing it to be deployed on the blockchain.id
, savedNumber
, and counter
, all of type Int
(aliased as uint32
).Initialization
init
function acts as a constructor and initializes the contract with an id
. We need to give a custom ID because the smart contract address will be determined during deployment based on the code and initial status.savedNumber
and counter
are set to zero upon deployment.Message Handlers
receive("add 1")
handler increments the counter
by 1 when it receives the message “add 1”.receive(msg: Save)
handler allows the contract to receive an object of type Save
and store the amount
in savedNumber
.Getter Functions
savedNumber
, counter
, and id
.The SimpleContract
is a straightforward example of a storage contract on the TON blockchain. It demonstrates basic functionalities such as initializing variables, handling messages to perform specific actions, and providing getter functions to retrieve stored values. In the next sections, we will build, test, deploy, and interact with this contract using the Blueprint SDK.
Once we have the contract, we can run the build command to compile it and ensure no error.
It will compile and build the contract
Once the contract compiles, we can test it; the test file is in the tests
directory. The default test script verifies that the contract deploys correctly.
Let’s edit it to also check that the counter and the save features work. Paste the following code.
Here is a quick breakdown, the test and interaction scripts are written in TypeScript. The idea is that the test file spins up a virtual chain to run the tests on with let blockchain: Blockchain; // Init a virtual chain
.
Imports
SimpleContract
wrapper.Describe Block
SimpleContract
. Inside the describe
block, we initialize variables for the blockchain, deployer, and contract instances.beforeEach Hook
Test: should deploy
beforeEach
hook.Test: should increase
add 1
message. It retrieves the counter value before and after sending the message and checks if it has increased.Test: should save the amount
Save
message and verifies if the savedNumber
variable is updated correctly.Run the test with the test command.
The Blueprint SDK allows you to deploy contracts to the mainnet or testenet and it provides endpoints out of the box, but in this case we want to use the Chainstack endpoint we deployed since performs better and it’s more reliable. We can add it in a configuration file, in the project’s root create a new file named blueprint.config.ts
and paste the code.
Now that the custom endpoint is configured, edit the deploy script inscripts
to include the contract ID; in this case, the ID is a random value, but you might change it based on the resulting address that will give you.
Blueprint allows various deployment options; in this case, we’ll use the CLI and the run command directly.
Find more methods in the Blueprint SDK docs.
First, add environment variables from the terminal if you want to use the mnemonic phrase to use your wallet; you can also use Tonkeeper and the app from your phone (more secure).
Run the run
command and follow the instructions, we used the mnemonic deployment in this case.
Example result:
Read data from the contract. Make a new file in the scripts
direcotry named getCounter.ts
:
Run it with the same run
command and follow the instructions:
Result:
Now use the wallet to send a transaction, including the message “Save” and some TON token to save the value.
Make a new script in scripts
named addValue
:
Follow the same process with the run
command, then once the transaction is validated, you can run the get script to fetch the updated value.
TLDR:
The Open Network (TON) is a decentralized and open platform comprising several components, including TON Blockchain, TON DNS, TON Storage, and TON Sites. Originally developed by the Telegram team, TON aims to provide fast, secure, and user-friendly decentralized services. Its core protocol, TON Blockchain, connects the underlying infrastructure to form the greater TON Ecosystem.
TON is focused on achieving widespread cross-chain interoperability within a highly scalable and secure framework. Designed as a distributed supercomputer or “superserver,” TON provides various products and services to contribute to developing the decentralized vision for the new internet. This makes it well-suited for a wide range of decentralized applications.
One of the unique features of TON smart contracts is that they can receive messages and do some action based on it; we’ll leverage this in the smart contract we’ll develop.
This tutorial will guide you through creating, building, testing, deploying, and interacting with a simple storage contract on the TON blockchain. We’ll use the Blueprint SDK.
The Blueprint SDK is an essential tool for developers working with the TON blockchain. It provides a comprehensive suite of tools and libraries that simplify the process of writing, testing, and deploying smart contracts.
Start for free and get your app to production levels immediately. No credit card required. You can sign up with your GitHub, X, Google, or Microsoft account.
This will be an introductory project where we use the Blueprint SDK to develop, test, and deploy a simple storage-style smart contract where we can save a number and increment a counter.
The smart contract is written in the TACT language, TON’s smart contract language, and is comparable to Solidity for EVM-based chains.
Let’s create a new TON project and run the initialization command in a new directory.
Select an empty project in TACT.
When you create a new project using the Blueprint SDK, the directory structure is organized to help you efficiently manage your smart contract development process. Here’s a brief overview of the main directories and their purposes:
build/: This directory contains the compiled artifacts of your smart contracts. After you run the build command, the output files will be stored here, including the compiled contract bytecode and other related artifacts.
contracts/: This is where you write your smart contracts. All .tact
files containing the contract code are placed in this directory. For instance, the SimpleStorage
contract will reside here.
scripts/: This directory is used for deployment and interaction scripts. These scripts facilitate deploying your contracts to the TON blockchain and interacting with them. For example, scripts to deploy the contract or fetch data from the contract will be placed here.
tests/: This directory holds the test files for your smart contracts. Here, you can write tests to ensure your contracts behave as expected. The default test script verifies contract deployment, and you can extend it to test additional functionalities.
wrappers/: This directory contains the TypeScript wrappers generated for your contracts. These wrappers provide a convenient way to interact with your contracts in a type-safe manner from your scripts and tests.
Now, we are ready to start developing the contract. You’ll find a .tact
contract in the contracts
directory, paste the following code in it.
Here is a breakdown of how the code works.
Imports and Message Declaration
import "@stdlib/deploy";
.Save
is declared, which contains an amount
field of type Int
(aliased as uint32
).Contract Declaration
SimpleContract
is declared with the Deployable
trait, allowing it to be deployed on the blockchain.id
, savedNumber
, and counter
, all of type Int
(aliased as uint32
).Initialization
init
function acts as a constructor and initializes the contract with an id
. We need to give a custom ID because the smart contract address will be determined during deployment based on the code and initial status.savedNumber
and counter
are set to zero upon deployment.Message Handlers
receive("add 1")
handler increments the counter
by 1 when it receives the message “add 1”.receive(msg: Save)
handler allows the contract to receive an object of type Save
and store the amount
in savedNumber
.Getter Functions
savedNumber
, counter
, and id
.The SimpleContract
is a straightforward example of a storage contract on the TON blockchain. It demonstrates basic functionalities such as initializing variables, handling messages to perform specific actions, and providing getter functions to retrieve stored values. In the next sections, we will build, test, deploy, and interact with this contract using the Blueprint SDK.
Once we have the contract, we can run the build command to compile it and ensure no error.
It will compile and build the contract
Once the contract compiles, we can test it; the test file is in the tests
directory. The default test script verifies that the contract deploys correctly.
Let’s edit it to also check that the counter and the save features work. Paste the following code.
Here is a quick breakdown, the test and interaction scripts are written in TypeScript. The idea is that the test file spins up a virtual chain to run the tests on with let blockchain: Blockchain; // Init a virtual chain
.
Imports
SimpleContract
wrapper.Describe Block
SimpleContract
. Inside the describe
block, we initialize variables for the blockchain, deployer, and contract instances.beforeEach Hook
Test: should deploy
beforeEach
hook.Test: should increase
add 1
message. It retrieves the counter value before and after sending the message and checks if it has increased.Test: should save the amount
Save
message and verifies if the savedNumber
variable is updated correctly.Run the test with the test command.
The Blueprint SDK allows you to deploy contracts to the mainnet or testenet and it provides endpoints out of the box, but in this case we want to use the Chainstack endpoint we deployed since performs better and it’s more reliable. We can add it in a configuration file, in the project’s root create a new file named blueprint.config.ts
and paste the code.
Now that the custom endpoint is configured, edit the deploy script inscripts
to include the contract ID; in this case, the ID is a random value, but you might change it based on the resulting address that will give you.
Blueprint allows various deployment options; in this case, we’ll use the CLI and the run command directly.
Find more methods in the Blueprint SDK docs.
First, add environment variables from the terminal if you want to use the mnemonic phrase to use your wallet; you can also use Tonkeeper and the app from your phone (more secure).
Run the run
command and follow the instructions, we used the mnemonic deployment in this case.
Example result:
Read data from the contract. Make a new file in the scripts
direcotry named getCounter.ts
:
Run it with the same run
command and follow the instructions:
Result:
Now use the wallet to send a transaction, including the message “Save” and some TON token to save the value.
Make a new script in scripts
named addValue
:
Follow the same process with the run
command, then once the transaction is validated, you can run the get script to fetch the updated value.