TON blockchain for games
What’s in the tutorial
In this tutorial we will consider how to add the TON blockchain to a game. For our example, we will use a Flappy Bird clone written in Phaser and will add GameFi features step by step. In the tutorial we will use short code pieces and pseudocode to make it more readable. Also, we will provide links to real code blocks to help you understand better. The whole implementation can be found in the demo repo.
We are going to implement the following:
- Achievements. Let’s reward our users with SBTs. The achievement system is a great tool to increase user engagement.
- Game currency. In TON blockchain it’s easy to launch your own token (jetton). The token can be used to create an in-game economy. Our users will be able to earn the game coins to spend them later.
- Game shop. We will provide users with the possibility to purchase in-game items using either in-game currency or the TON coin itself.
Preparations
Install GameFi SDK
First, we will set up the game environment. To do that we need to install assets-sdk
. The package is designed to prepare everything developers need to integrate the blockchain into games. The lib can be used either from CLI or from Node.js scripts. In this tutorial we stick with the CLI approach.
npm install -g @ton-community/assets-sdk@beta
Create a master wallet
Next, we need to create a master wallet. The master wallet is a wallet we will use to mint the jetton, collections, NFTs, SBTs and receive payments.
assets-cli setup-env
You will be asked a few questions:
Field | Hint |
---|---|
Network | Select testnet as far it’s a test game. |
Type | Select highload-v2 type of wallet as far it’s the best, performant option to use as a master wallet. |
Storage | The storage will be used to store NFT /SB T files. Amazon S3 (centralized) or Pinata (decentralized). For the tutorial let’s use Pinata as far as decentralized storage will be more illustrative for the Web3 game. |
IPFS gateway | Service to load assets metadata from: pinata , ipfs.io or enter other service URL. |
The script outputs the link you can open to see the created wallet state.
As you can see the wallet is not actually created yet. To the wallet be really created we need to deposit some funds into it. In the real-world scenario, you can deposit the wallet any way you prefer using the wallet address. In our case we will use Testgiver TON Bot. Please open it to claim 5 test TON coins.
A bit later you could see 5 TON on the wallet and its status became Uninit
. The wallet is ready. After the first usage it changes status to Active
.