You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/v3/guidelines/dapps/tutorials/web3-game-example.md
+57-53Lines changed: 57 additions & 53 deletions
Original file line number
Diff line number
Diff line change
@@ -1,53 +1,52 @@
1
1
# TON blockchain for games
2
2
3
3
## What’s in the tutorial
4
-
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](https://github.com/ton-community/flappy-bird).
4
+
In this tutorial, we will explore how to integrate the TON blockchain into a game. As an example, we will use a Flappy Bird clone built with Phaser and gradually add GameFi features. To improve readability, we will use short code snippets and pseudocode. Additionally, we will provide links to real code blocks for better understanding. The complete implementation can be found in the [demo repo](https://github.com/ton-community/flappy-bird).
5
5
6
6

7
7
8
-
We are going to implement the following:
9
-
- Achievements. Let’s reward our users with [SBTs](/v3/concepts/glossary#sbt). The achievement system is a great tool to increase user engagement.
10
-
- 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.
11
-
- Game shop. We will provide users with the possibility to purchase in-game items using either in-game currency or the TON coin itself.
12
-
8
+
We will implement the following:
9
+
- Achievements. Let’s reward our users with [SBTs](/v3/concepts/glossary#sbt). The achievement system is a great tool for increasing user engagement.
10
+
- Game currency. On the 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 game coins and spend them later.
11
+
- Game shop. We will allow users to purchase in-game items using either in-game currency or TON coins.
12
+
13
13
## Preparations
14
14
15
15
### Install GameFi SDK
16
-
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.
16
+
First, we need to set up the game environment by installing `assets-sdk`. This package is designed to provide developers with everything required to integrate the blockchain into games. The library can be used either from the CLI or within Node.js scripts. In this tutorial, we will use the CLI approach.
17
17
```sh
18
18
npm install -g @ton-community/assets-sdk@beta
19
19
```
20
20
21
21
### Create a master wallet
22
-
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.
22
+
Next, we need to create a master wallet. This wallet will be used to mint jettons, collections, NFTs, and SBTs, as well as to receive payments.
23
23
```sh
24
24
assets-cli setup-env
25
25
```
26
-
You will be asked a few questions:
26
+
You will be asked a few questions during the setup.
27
27
28
28
Field | Hint
29
29
:----- | :-----
30
-
Network | Select `testnet`as far it’s a test game.
31
-
Type | Select `highload-v2`type of wallet as far it’s the best, performant option to use as a master wallet.
32
-
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.
33
-
IPFS gateway | Service to load assets metadata from: `pinata`, `ipfs.io`or enter other service URL.
30
+
Network | Select `testnet`since this is a test game.
31
+
Type | Select `highload-v2`wallet type, as it offers the best performance for use as a master wallet.
32
+
Storage | Storage is used to hold`NFT`/`SB`T files. You can choose between `Amazon S3` (centralized) or `Pinata` (decentralized). For this tutorial, we'll use Pinata since decentralized storage is more illustrative for a Web3 game.
33
+
IPFS gateway | This service loads asset metadata from`pinata`, `ipfs.io, or a custom service URL.
34
34
35
-
The script outputs the link you can open to see the created wallet state.
35
+
The script will output a link where you can view the created wallet's state.
36
36
37
37

38
38
39
-
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](https://t.me/testgiver_ton_bot). Please open it to claim 5 test TON coins.
40
-
41
-
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`.
39
+
As you can see, the wallet has not actually been created yet. To finalize the creation, we need to deposit funds into it. In a real-world scenario, you can fund the wallet however you prefer using its address. In our case, we will use the [Testgiver TON Bot](https://t.me/testgiver_ton_bot). Open it to claim 5 test TON coins.
42
40
41
+
A little later, you should see 5 TON in the wallet, and its status will change to Uninit. The wallet is now ready. After the first transaction, its status will change to Active.
43
42

44
43
45
44
### Mint in-game currency
46
-
We are going to create in-game currency to reward users with it:
45
+
We are going to create an in-game currency to reward users.
47
46
```sh
48
47
assets-cli deploy-jetton
49
48
```
50
-
You will be asked a few questions:
49
+
You will be asked a few questions during the setup:
Symbol | `FLAP` or enter any abbreviation you want to use.
58
57
Decimals | How many zeros after the dot your currency will have. Let’ it be `0` in our case.
59
58
60
-
The script outputs the link you can open to see the created jetton state. It will have `Active` status. The wallet status will change the status from `Uninit` to `Active`.
59
+
The script will output a link where you can view the created jetton's state. It will have an **Active** status. The wallet’s status will change from **Uninit** to **Active**.
Just for example, in the demo game we will reward users for the first and the fifth games. So, we will mint two collections to put SBTs into them when users achieve related conditions – play first and fifth time:
64
+
For our demo game, we will reward users after their first and fifth games. To do this, we will mint two collections, where SBTs will be assigned when users meet the required conditions—playing for the first and fifth time:
66
65
```sh
67
66
assets-cli deploy-nft-collection
68
67
```
@@ -74,14 +73,17 @@ Name | Flappy First Flight | Flappy High Fiver
74
73
Description | Commemorating your inaugural journey in the Flappy Bird game! | Celebrate your persistent play with the Flappy High Fiver NFT!
75
74
Image | You can download [the image](https://raw.githubusercontent.com/ton-community/flappy-bird/article-v1/scripts/tokens/first-time/image.png) here | You can download [the image](https://raw.githubusercontent.com/ton-community/flappy-bird/article-v1/scripts/tokens/five-times/image.png) here
76
75
77
-
We are fully prepared. So, let’s go to the logic implementation.
76
+
Now that we are fully prepared, let's proceed to implementing the game logic.
78
77
79
78
## Connecting wallet
80
-
The process begins with the user connecting their wallet. So, let’s add wallet connect integration. To work with the blockchain from the client side we need to install GameFi SDK for Phaser:
79
+
The process begins with the user connecting their wallet. Let's integrate wallet connectivity.
80
+
81
+
To interact with the blockchain from the client side, we need to install the GameFi SDK for Phaser:
81
82
```sh
82
83
npm install --save @ton/phaser-sdk@beta
83
84
```
84
-
Now let’s setup GameFi SDK and create an instance of it:
85
+
Now, let's set up GameFi SDK and create an instance of it:
> To learn what `tonconnect-manifest.json` is please check ton-connect [manifest description](/v3/guidelines/ton-connect/guidelines/creating-manifest).
120
122
121
-
Now we are ready to create a wallet connect button. Let’s create a UI scene in Phaser which will contain the connect button:
123
+
Next, we are ready to create a **Wallet Connect** button. Let’s create a UI scene in Phaser that will contain the **Connect** button:
124
+
122
125
```typescript
123
126
classUiSceneextendsPhaser.Scene {
124
127
// receive gameFi instance via constructor
@@ -143,7 +146,7 @@ class UiScene extends Phaser.Scene {
143
146
144
147
> Read how to create [connect button](https://github.com/ton-community/flappy-bird/blob/article-v1/workspaces/client/src/connect-wallet-ui.ts#L82) and the [UI scene](https://github.com/ton-community/flappy-bird/blob/article-v1/workspaces/client/src/connect-wallet-ui.ts#L45).
145
148
146
-
To watch when a user connects or disconnects his wallet let’s use the following piece of code:
149
+
To monitor when a user connects or disconnects their wallet, use the following code snippet:
> To learn about more complex scenarios please check out the full implementation of [wallet connect flow](https://github.com/ton-community/flappy-bird/blob/article-v1/workspaces/client/src/index.ts#L16).
159
162
160
-
Read how [game UI managing](https://github.com/ton-community/flappy-bird/blob/article-v1/workspaces/client/src/index.ts#L50)might be implemented.
163
+
Read how [game UI managing](https://github.com/ton-community/flappy-bird/blob/article-v1/workspaces/client/src/index.ts#L50)can be implemented.
161
164
162
-
Now we have the user wallet connected and we can move forward.
165
+
Now that we have the user's wallet connected, we can move forward.

167
170
168
171
169
172
## Implementing achievements & rewards
170
-
To implement achievements and reward system we need to prepare an endpoint which will be requested per user try.
173
+
To implement the achievements and reward system, we need to set up an endpoint that will be triggered each time a user plays.
171
174
172
175
### `/played` endpoint
173
-
We need to create an endpoint `/played ` which must do the following:
174
-
-receive a body with a user wallet address and Telegram initial datapassed to Mini App during the app launch. The initial data needs to be parsed to extract authentication data and ensure a user sends the request only on its behalf.
175
-
-the endpoint must calculate and store the number of games a user played.
176
-
-the endpoint must check if it’s the first or fifth game for a user and if so, reward a user with related SBT.
177
-
- the endpoint must reward a user with 1 FLAP for each game.
176
+
We need to create an endpoint `/played ` which does the following:
177
+
-receives a request body containing the user’s wallet address and Telegram initial data, which is passed to the Mini App during launch. The initial data must be parsed to extract authentication details and verify that the user is sending the request on their own behalf.
178
+
-tracks and stores the number of games a user has played.
179
+
-checks whether this is the user’s first or fifth game. If so, it rewards the user with the corresponding SBT.
180
+
-rewards the user with 1 FLAP for each game played.
Let’s play the first time and ensure we will be rewarded with a FLAP token and SBT. Click the Play button, fly through a pipe or two and then hit into a tube. Alright, everything works!
205
+
Let’s play for the first time and ensure we receive a FLAP token and an SBT. Click the Play button, fly through a pipe or two, then crash into a pipe. Everything works!
203
206
204
207

205
208
206
-
Play 4 more times to get the second SBT, then open your Wallet, TON Space. Here your collectibles are:
207
-
209
+
Play four more times to earn the second SBT, then open your TON Space Wallet. Here are your collectibles:
208
210

209
211
210
-
## Implementing game shop
211
-
To have an in-game shop we need to have two components. The first is an endpoint that provides info about users purchases. The second is a global loop to watch user transactions and assign game properties to its owners.
212
+
## Implementing the game shop
213
+
To set up an in-game shop, we need two components. The first is an endpoint that provides information about users' purchases. The second is a global loop that monitors user transactions and assigns game properties to item owners.
212
214
213
215
### `/purchases` endpoint
214
216
The endpoint does the following:
215
-
- receive `auth`get param with Telegram Mini Apps initial data.
216
-
- the endpoint gets items a user purchased and responds with the items list.
217
+
- receive `auth`query parameter containing Telegram Mini App initial data.
218
+
-retrieves the items a user has purchased and responds with a list of those items.
To know when users make payments, we need to watch the master wallet transactions. Every transaction must contain a message `userId`:`itemId`. We will remember the last processed transaction, get only new ones, assign users properties they bought using `userId` and `itemId`, rewrite the last transaction hash. This will work in an infinite loop.
223
+
o track user payments, we need to monitor transactions in the master wallet. Each transaction must include a message in the format `userId`:`itemId`. We will store the last processed transaction, retrieve only new ones, assign purchased properties to users based on `userId` and `itemId`, and update the last transaction hash. This process will run in an infinite loop.
When a user clicks the button, the shop scene is opened. The shop scene contains the list of items a user can buy. Each item has a price and a Buy button. When a user clicks the Buy button, the purchase is made.
234
+
When a user clicks this button, the **Shop Scene** opens. The shop contains a list of items available for purchase. Each item has a price and a Buy button. When a user clicks the Buy button, the purchase is processed.
235
+
236
+
Opening the **Shop Scene** will trigger the loading of purchased items and refresh the list every 10 seconds.
232
237
233
-
Opening the Shop will trigger purchased items loading and updating it every 10 seconds:

255
259
256
-
It's also possible to pay with the TON coin:
260
+
It is also possible to pay with TON coins:
257
261
```typescript
258
262
import { toNano } from'@ton/phaser-sdk'
259
263
@@ -265,11 +269,11 @@ gameFi.buyWithTon({
265
269
266
270
## Afterword
267
271
268
-
That’s it for this tutorial! We considered the basic GameFi features, but the SDK delivers more functionality like transfers between players, utils to work NFTs and collections, etc. We will deliver even more features in the future.
272
+
TThat’s it for this tutorial! We explored the basic GameFi features, but the SDK offers additional functionality, such as player-to-player transfers and utilities for working with NFTs and collections. More features will be introduced in the future.
269
273
270
-
To learn about all the GameFi features you can use read the docs of[ton-org/game-engines-sdk](https://github.com/ton-org/game-engines-sdk) and [@ton-community/assets-sdk](https://github.com/ton-community/assets-sdk).
274
+
To learn about all available GameFi features, read the documentation for[ton-org/game-engines-sdk](https://github.com/ton-org/game-engines-sdk) and [@ton-community/assets-sdk](https://github.com/ton-community/assets-sdk).
271
275
272
-
So, let us know what you think in the[Discussions](https://github.com/ton-org/game-engines-sdk/discussions)!
276
+
et us know your thoughts in[Discussions](https://github.com/ton-org/game-engines-sdk/discussions)!
273
277
274
278
275
-
The complete implementation is available in [flappy-bird](https://github.com/ton-community/flappy-bird)repo.
279
+
The complete implementation is available in the [flappy-bird](https://github.com/ton-community/flappy-bird)repository.
0 commit comments