Deploying our First Smart Contract on TON blockchain

Mahdi Bagheri
3 min readApr 8, 2024

--

The world of Decentralized Applications (dApps) has captivated tech enthusiasts for years. These powerful applications leverage blockchain technology to cut out the middleman, fostering secure and transparent interactions. At the heart of every dApp lies the smart contract, a self-executing program that dictates the application’s logic.

This medium story will delve into our experience with TON and provide a glimpse into the exciting world of smart contract development.

Terminology

The Open Network is a decentralized computer network consisting of a layer-1 blockchain with various components.

A Layer-1 Blockchain refers to the foundational level of blockchain architecture, operating as the primary and autonomous chain on which transactions are directly executed and confirmed, as well as providing the essential infrastructure for decentralized applications and smart contracts.

A smart contract is a computer program or a transaction protocol that is intended to automatically execute, control or document events and actions according to the terms of a contract or an agreement.

Blueprint is A development environment for TON blockchain for writing, testing, and deploying smart contracts.

Tact is a new programming language for TON blockchain that is focused on efficiency and simplicity. It is designed to be easy to learn and use, and to be a good fit for smart contracts. Tact is a statically typed language with a simple syntax and powerful type system.

We’ll be utilizing Blueprint to construct and launch our inaugural contract, leveraging its pre-configured counter template. This particular contract will maintain a numerical counter, which we can enhance by executing a specific function within the contract itself. The choice to employ the Tact programming language for this tutorial was influenced by its user-friendly syntax and its resemblance to object-oriented programming paradigms.

Let’s embark on crafting our initial project, proceeding with a meticulous, step-by-step approach. 🚀

Requirements
- Node.js with a recent version like v18, verify version with node -v
- IDE with TypeScript and Tact support like Visual Studio Code with the Tact plugin or IntelliJ Idea with the TON Development plugin.

  1. Run and follow the on-screen instructions: `npm create ton@latest` or `npx create-ton@latest`
  2. (Optional) Then from the project directory: npm install or yarn install (based on your beloved package manager)
  3. Select “counter contract template with Tact” from command line options and DONE!

Congratulations on creating a new project using the Counter template. Now, let's explore the directory structure:

  • contracts: This directory is straightforward, right? It’s where your smart contracts reside.
  • build: The build artifacts for your contracts will be automatically created here.
  • scripts: Here, you can craft brief scripts to interact with your contract externally. Whether it’s sending a transaction, retrieving data made public by the contract, or deploying your contract to the mainnet (or testnet).
  • wrappers: Contained within this folder are the wrappers for your smart contracts, serving as interfaces. This allows for effortless management of your contract’s operations and accessors.
  • tests: This folder is essential for housing your contract’s test files. It’s where you’ll write tests to verify your contract’s functionality and ensure everything is working as expected.

Let’s open our counter.tact contract:

import "@stdlib/deploy";

message Add {
queryId: Int as uint64;
amount: Int as uint32;
}

contract CounterContract with Deployable {
id: Int as uint32;
counter: Int as uint32;

init(id: Int) {
self.id = id;
self.counter = 0;
}

receive(msg: Add) {
self.counter += msg.amount;
}

get fun counter(): Int {
return self.counter;
}

get fun id(): Int {
return self.id;
}
}

Let’s build it by running:

npx blueprint build

And finally deploy it by running:

npx blueprint run

Select “deployCounterContract” option. Choose testnet and connect your TON wallet or use your seed phrase to deploy the contract using your testnet TON wallet.

Deploying contracts on mainnet and testnet both require paying fees for both storage and network.

For testing, you can get free test TONs from @testgiver_ton_bot in Telegram so you can deploy your smart contracts on testnet.

Consider testnet as a free equivalent of mainnet which has no difference.

You can see your contract’s address printed in the terminal, and you can find it on a blockchain explorer like tonscan or tonviewer.

Follow my Telegram Channel for more.

--

--

Mahdi Bagheri

Founder of #PizzaTon | Follow @TonScholar on Telegram