Build your first game with TRC21 (Part 1)

This is the first part of a 2-part series on how to develop your first game with TRC21 tokens on TomoChain.

Victor
TomoChain
5 min readAug 17, 2019

--

In our previous tutorials Part 1 and Part 2 on developing a game on TomoChain, we show how easy it is to create a simple TomoLuckyBox game using $TOMO as the in-game cryptocurrency/token. The game basically allows players to find the lucky box among three boxes to get rewards paid in $TOMO.

In this article, we further develop a variant of the game by introducing a token.

  • Simply stated, instead of using $TOMO as the cryptocurrency for paying rewards, we will issue a TRC21 token
  • Each player will bet on a box and will get a reward if the selected box is the lucky box, otherwise, the player will lose a certain amount of tokens.

This first part focuses on customizing the smart contract of the TomoLuckyBox simple game developed in the previous series. We show how the game contract issues a TRC21 token LKB and how players use the token to play games. Then we show how to deploy the contract, apply the issued TRC21 token to TomoIssuer, and transfer tokens from one to another.

The second part of the series will focus on modifying the front-end web code to interact with the game contract.

What’s TRC21 token?

TRC21 is a token standard working with TomoZ protocol allowing a DApp/game user to pay fees for transactions to token smart contracts in the token the user is holding. Simple said, if Alice has 100 LKB TRC21 tokens, Alice can make transactions to the LKB token smart contract without $TOMO in Alice’s wallet.

This allows any game/dapp builders to build their own ecosystem on TomoChain without requiring their users to know about TomoChain, which significantly improve user experience compared to most current blockchains.

Customize TomoLuckyBox to work with TRC21

Modify the TomoLuckyBox contract to be a token contract

Step1: Clone TomoLuckyBox repository https://github.com/frogdevvn/tomoluckybox-smartcontract

Step 2: Create a TRC21 token contract using TomoChain’s template for TRC21 tokens.

a. Copy TRC21.sol file into our cloned repository and put it in the same folder with TomoLuckyBox.sol file

b. The most interesting part of TRC21 contract is how the token fees are defined especially minFee, which indicates the number of tokens a token holder must pay as transaction fees.

c. In TRC21.sol, modify MyTRC21 with your own token name. I put it LKB for the case of LuckyBox game.

Here is the code for LBKTRC21 contract: Note this is the token contract that will be used for LuckyBox game.

contract LKBTRC21 is TRC21 {
string private _name = "LuckyBox Token";
string private _symbol = "LKB"; uint8 private _decimals = 18; uint256 private _cap = uint256(100000000) * uint256(10)**_decimals;uint256 private _mintFee = 1 * uint256(10)**_decimals;
constructor () public {
_mint(msg.sender, _cap); _changeIssuer(msg.sender); _changeMinFee(_minFee);}/** * @return the name of the token. */function name() public view returns (string memory) { return _name;}/** * @return the symbol of the token. */function symbol() public view returns (string memory) { return _symbol;}/** * @return the number of decimals of the token. */function decimals() public view returns (uint8) { return _decimals;}function setMinFee(uint256 value) public { require(msg.sender == issuer()); _changeMinFee(value); }}

The contract has the following important parameters:

  • Token name: LuckyBox Token
  • Token symbol: LKB
  • Token decimals: 18
  • Total supply: 1B
  • Minimum token fee: 1 => any transaction to the contract must pay at least 1 LKB as transaction fees

>> If multiple inheritances are needed in your game, as a rule in TomoZ, the game contract should put the token contract as the first in the inheritance list.

Step 3: Rename TomoLuckyBox.sol to LKBLuckyBox.sol and TomoLuckyBox contract to LKBLuckyBox.

Step 4: In TomoZ, only transactions made to contracts that inherit ITRC21 interface are allowed to pay fees in tokens.

  • In our game, we need to make the game contract LKBLuckyBox inherit the token contract LBKTRC21.
contract LKBLuckyBox is LKBTRC21 { uint constant LUCKY_BOX_1 = 1000; uint constant LUCKY_BOX_2 = 2000; uint constant LUCKY_BOX_3 = 3000;
  • Modify randomLuckyBox function to accept the token as a bet
function randomLuckyBox(uint idBoxSender) external{uint randomNumber = random();uint idLuckyBox = 0;if(randomNumber >= 1 && randomNumber <= 3)idLuckyBox = LUCKY_BOX_1;else if(randomNumber >= 4 && randomNumber <= 6)  idLuckyBox = LUCKY_BOX_2; else  idLuckyBox = LUCKY_BOX_3;if(idLuckyBox == idBoxSender) { uint amountPrize = msg.value * 3; sendPayment(msg.sender, amountPrize); emit LogRandomLuckyBoxSuccessed(msg.sender, RESULT_WIN, idLuckyBox, amountPrize);}else { emit LogRandomLuckyBoxSuccessed(msg.sender, RESULT_LOSE, idLuckyBox, 0); }}

Contract deployment on TomoChain testnet

TomoIssuer has an intuitive UI for anyone to issue a TRC21 token and deploy the token contract on the chain within a few clicks. However, for our gaming and token contract, we should either use Truffle or Remix to deploy the contract since we customize the token contract.

For this simple contract, we’ll use Remix. For those who use Truffle, you can follow this tutorial to deploy your contract on TomoChain.

Deploy the game contract on TomoChain using Remix:

  • Connect Metamask to TomoChain testnet: follow this guide
  • Go to https://remix.ethereum.org
  • Import TRC21.sol and LKBLuckyBox.sol to Remix
  • Deployed your contract once compiled with 0.4.24 solidity compiler. Note that you need to set gas price as 10000 GWei.
  • Here’s the deployed contract: 0x549aeb02233e0183dc33e5fb563bb02332251a58

Apply to TomoIssuer

Once deployed, you need to apply the deployed token contract to TomoIssuer in order for LKB tokens to follow the TomoZ protocol: pay transaction fees by tokens.

  • Go to a detailed page of LuckyBox Token and click on Apply to TomoZ Protocol. The application requires to deposit at least 10 TOMO to be used as transaction fees.
  • Once applied, Transaction Fees should be shown as the amount of token per transaction, which is 1 LBK in this case.
  • Transaction fees for transferring LKB tokens should be in token now.

Token transfer

LKB tokens now can be transferred using TomoWallet, MetaMask, or MEW. Here’s one of the transactions that are made using MetaMask
https://scan.testnet.tomochain.com/txs/0xb5febc6661e722d2a2c9d25b996bcdb88df4976d1add60967cbf52a2add58c20

TomoScan also shows that 5000 LKB is sent to 0xcc6cb82ae889d3d52a124e93cb12fbdd619da973 with 1 LKB as token fee.

In Part 2 of this series, we’ll show how to modify the front-end web code to interact and play with our LuckyBox game contract.

Reference

Link to LKBLuckyBox contract code https://github.com/phamvancam2104/tomoluckybox-smartcontract.git

--

--