TON: Deploy a smart contract
TLDR
- You’ll configure a Hardhat project to interact with Aave V3 flash loans on Avalanche’s Fuji testnet.
- You’ll use Chainstack for your Avalanche node endpoint and deploy a custom FlashLoan contract.
- You’ll borrow USDC, then repay it plus fees in a single transaction, demonstrating a flash loan’s instant, collateral-free mechanics.
- By the end, you’ll have a working Aave flash loan flow on a testnet environment ready for deeper custom logic.
Main article
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.
Prerequisites
- Chainstack account to deploy a TON testnet node
- Node.js
- A TON wallet, we used Tonekeeper. You can choose one on ton.org.
- Some testnet tokens; get some tokens from the TON faucet.
Run nodes on Chainstack
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.
Project overview
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.
Getting started
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, theSimpleStorage
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.
Contract development
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.
Breakdown of the Contract
-
Imports and Message Declaration
- The contract imports necessary modules using
import "@stdlib/deploy";
. - A custom message type
Save
is declared, which contains anamount
field of typeInt
(aliased asuint32
).
- The contract imports necessary modules using
-
Contract Declaration
- The
SimpleContract
is declared with theDeployable
trait, allowing it to be deployed on the blockchain. - The contract contains three variables:
id
,savedNumber
, andcounter
, all of typeInt
(aliased asuint32
).
- The
-
Initialization
- The
init
function acts as a constructor and initializes the contract with anid
. We need to give a custom ID because the smart contract address will be determined during deployment based on the code and initial status. - The variables
savedNumber
andcounter
are set to zero upon deployment.
- The
-
Message Handlers
- The contract can receive messages to perform specific actions.
- The
receive("add 1")
handler increments thecounter
by 1 when it receives the message “add 1”.
- The
receive(msg: Save)
handler allows the contract to receive an object of typeSave
and store theamount
insavedNumber
.
-
Getter Functions
- The contract includes getter functions to retrieve the values of
savedNumber
,counter
, andid
.
- The contract includes getter functions to retrieve the values of
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.
Build the contract
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
Test 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.
Breakdown of the Test File
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
- Import necessary modules and utilities from the TON Sandbox, core libraries, and the
SimpleContract
wrapper.
- Import necessary modules and utilities from the TON Sandbox, core libraries, and the
-
Describe Block
- Define the test suite for the
SimpleContract
. Inside thedescribe
block, we initialize variables for the blockchain, deployer, and contract instances.
- Define the test suite for the
-
beforeEach Hook
- This hook runs before each test. It sets up the blockchain environment, initializes the contract, and deploys it using a deployer with 1M TON tokens available. The deployment is then verified to ensure the contract is deployed successfully.
-
Test: should deploy
- This test checks if the contract is deployed correctly by fetching the contract ID. The actual deployment check is handled in the
beforeEach
hook.
- This test checks if the contract is deployed correctly by fetching the contract ID. The actual deployment check is handled in the
-
Test: should increase
- This test verifies the functionality of the
add 1
message. It retrieves the counter value before and after sending the message and checks if it has increased.
- This test verifies the functionality of the
-
Test: should save the amount
- This test checks the functionality of saving a specified amount in the contract. It sends a
Save
message and verifies if thesavedNumber
variable is updated correctly.
- This test checks the functionality of saving a specified amount in the contract. It sends a
Run the test with the test command.
Deploy to the TON chain
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:
Interact with the contract
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.