Building Your First Game dApp On TomoChain (Part 1)

Challenge yourself by building a simple lucky dApp game with Truffle Framework, Cocos Creator and deploy to TomoChain. 💪

TomoChain Publisher
TomoChain
7 min readJul 31, 2019

--

What are dApps?

  • Understanding Blockchain: Before we can even fathom what dApps do, we need to be familiar with its underlying technology — the blockchain. Put simply, a blockchain is a ledger of records organized in ‘blocks’ that are linked together by cryptographic validation. It is a digital storage of consensus truth. The key is to understand that this ledger is neither stored in a centralized location nor managed by any single entity, hence its distributed-ness. The block validation system results in new transactions being added irreversibly and old transactions preserved forever for all to see, hence its transparency and resilience. Open-source software that leverage blockchain technology are called dApps.
  • Understanding Smart Contract: Smart contracts help you exchange money, property, shares, or anything of value in a transparent, conflict-free way while avoiding the services of a middleman. The best way to describe smart contracts is to compare the technology to a vending machine. Ordinarily, you would go to a lawyer or a notary, pay them, and wait while you get the document. With smart contracts, you simply drop a bitcoin into the vending machine (i.e. ledger), and your escrow, driver’s license, or whatever drops into your account. More so, smart contracts not only define the rules and penalties around an agreement in the same way that a traditional contract does, but also automatically enforce those obligations. If you are looking for a more detailed walkthrough of smart contracts please check out our blockchain courses on smart contracts.

dApp is an abbreviation for Decentralized application. dApps are a new paradigm for building apps where a back end centralized server is replaced by a decentralized peer to peer network.

One of the most popular dApps, cryptokitties, is a collectibles-style game built on Ethereum. When we build a game with Ethereum each game action and transaction is stored on the Ethereum blockchain.

What is TomoChain?

TomoChain is an innovative solution to the scalability problem with the Ethereum blockchain, and other blockchain platforms. TomoChain supports all EVM-compatible smart-contracts, which basically means that every DApp run on Ethereum can be easily ported to TomoChain.

For more information about TomoChain, please refer to our website.

What is Cocos Creator?

Cocos Creator is a complete package of game development tools and workflow, including a game engine (based on Cocos2d-x), resource management, scene editing, game preview, and debug and publish from one project to multiple platforms.

For the first time we introduced entity-component structure and data-driven workflow to the Cocos2d-x family. With JavaScript, you can script your component in no time. The editor and engine extension is also made with JavaScript so you can make games and refine your tools in a single programming language.

Cocos Creator provides an innovative, and easy to use toolset such as the UI system and Animation editor. The toolset will be expanding continuously and quickly, thanks to the open editor extension system.

Homepage: https://cocos2d-x.org/creator

Docs: https://docs.cocos2d-x.org/creator/manual/en/

Overview “Tomo Lucky Box” game dApp

For this tutorial, we will make a simple game dApp called “Tomo Lucky Box” using Cocos Creator.

Gameplay: We have 3 boxes to choose. We need to pay 1 TOMO each time we open one box, if you choose the lucky box you will get a prize.

We recommend you have a basic understanding of:

  • JavaScript (Good link to read)
  • Solidity & smart contract (Good link to read)
  • Truffle framework (Good link to read)
  • Cocos Creator (Good link to read)

Create a TOMO Wallet

You will need a wallet address and some tokens. We will show you how to do it on both TomoChain Testnet and Mainnet.

Create a TOMO wallet and save your Mnemonic

You can create a new TOMO wallet using TomoWallet mobile app for Android or iOS. Under Settings go to Advanced Settings, here you can Choose network and select TomoChain TestNet or TomoChain MainNet.

Go to Settings menu, select Backup wallet and then Continue. Here you can see your wallet’s private key and the 12-word recovery phrase. Write down the 12-word recovery phrase.

You can also create a new TomoChain wallet with MetaMask, MyEtherWallet or TrustWallet. For instance, for mainnet you can go to MyEtherWallet and select TOMO (tomochain.com) on the top right corner. Enter a password and then Create a new wallet. Write down your recovery phrase.

For this tutorial, the wallet address (testnet) is: 0xc9b694877acd4e2e100e095788a591249c38b9c5

The recovery phrase (12-word mnemonic) is:

myth ahead spin horn minute tag spirit please gospel infant clog camera

Write them down. This will be needed later. Notice that your wallet address (public key) and your recovery phrase will be different than mine.

Important! Always keep your private key and recovery phrase secret!

Get some TOMO funds

Tokens are required for different matters, like smart contract deployment or to use in DApps.

Testnet: Receive 15 free testnet TOMO tokens using TomoChain’s Faucet.

Mainnet: You need real TOMO from exchanges.

Go to faucet and collect 30 TOMO. Now your wallet has enough balance to do everything in this tutorial so… let’s go ahead!

The Block Explorer

To check the balance of a wallet address, you can search the address on TomoScan.

Testnet: https://scan.testnet.tomochain.com/

Mainnet: https://scan.tomochain.com/

Write TomoLuckyBox Smart Contract

Create Truffle project

Open your Terminal. Run commands:

Config TomoChain networks for Truffle project

Open file truffle-config.js, and replace with below code to config TomoChain Mainnet and TomoChain Testnet

TomoLuckyBox.sol

Create a new file named TomoLuckyBox.sol in the contracts/ directory.

Our TomoLuckyBox.sol smart contract will control the core logic for our game.

Check full source code of smart contract on GitHub.

Let’s have a look at some snippets code:

Constant: We define some constant variables to help our smart contract code easier to read and understand. And we have a nonce, which will be used to randomize our results efficiently

Events: We have only 1 event LogRandomLuckyBoxSuccessed, this event will emit when player request to open their selected box and smart contract will random result and return it to player. Events help in conveying state change on the front-end.

Functions:

  • random: This is core function to help us random a number between 1-> 9

NOTE: Current implement of our random is not secure. For production mode, you can use Oraclize or Commit-Reveal for good secure.

  • randomLuckyBox: Receive which box of player selected, then we will perform to random the lucky box with following steps:
  • Random number between 1->9.
  • Depend on random number above we will check to get result of player is win or lose.
  • If player win, we calculate prize and send payment to player address. Then finally, LogRandomLuckyBoxSuccessed to front-end.

sendPayment: Help smart contract can send payment to player address.

Deploying our smart contract to TomoChain

Run the commands below to compile and deploy our contract to TomoChain Testnet.

The migrations will start…

Conclusion

We finish part 1 finally. In next part we will create Cocos Creator project for front-end and make it working with our smart contract through web3.js.

TomoChain contact

Follow us on TomoChain Announcement Channel | Twitter | Facebook | LinkedIn | Reddit | Github | Sign up for our newsletter via the Website

For General support: TomoChain Chat

For Technical support: Gitter

--

--