Let’s Build A Decentralized Game Economy Using Blockchains [Part 2]

Setting Up A Local Solidity Development Environment and a Private Blockchain

In Part 1 of this series we created the foundations of a decentralized game economy on the blockchain. We discussed Decentralized Applications, created our own ERC20 token on the Ethereum blockchain, and used Metamask and Unity to transfer tokens from one account to another.

To reduce setup time and quickly get started, most of the software we used for Part 1 were online cloud-based tools: remix.ethereum.org to deploy our contract code, the Ropsten testnet as our blockchain, and ropsten.etherscan.io to verify our contract transactions.

If we’re going to start writing other contract code aside from the boilerplate ERC20 token though, it would be a good idea to set up a local development environment. Having one would allow us to easily recreate our own private blockchain with provisioned accounts, execute contracts more quickly, and even run test scripts on our code.

Let’s Set It Up!

Let’s install the tools we need one by one, starting from the top.

1. Atom + language-ethereum package

Atom is a great IDE for this — not only is it a clean and customizable code editor for any language, but it can also be used with Solidity after we install the language-ethereum package.

Install atom from their website: https://atom.io/

After installation, go to Atom → Preferences → Install and install language-ethereum to enable Solidity code highlighting.

2. Truffle Framework

Truffle is a great development framework for Ethereum from ConsenSys. They also made Ganache (previously TestRPC) which we’ll be using for our private blockchain.

Install Truffle by following instructions on http://truffleframework.com.

Truffle uses node.js and npm, so if you don’t have them installed you would also need to download the installer from https://nodejs.org.

To test if Truffle installed successfully, run ‘truffle’ from the Terminal or command line. You should see a list of commands that Truffle can execute.

3. Ganache

Installing and running Ganache is pretty straightforward. Download it from http://truffleframework.com/ganache/ and then start the application. Once the blockchain is up and running, you’ll see the following screen.

Let’s Try It Out!

1. Set Up Our Project Environment using Atom and ‘truffle init’

mkdir local-blockchain
cd local-blockchain
truffle init

If we look into our directory, we’ll see that ‘truffle init’ has created some initial files for us.

Let’s view these files in Atom. In Atom, select File → Add Project Folder, and select the project directory that we created.

Viewing the ‘truffle init’ files in Atom, with language-ethereum code highlighting

We see that Truffle has created an initial contract for us called ‘Migrations.sol’. This is a special smart contract that’s used to keep track of changes. There’s also a migrations directory where we’ll be putting javascript files to deploy our contract code.

2. Recreate our Token Contract on Atom and create Deployment Script

Create a new file called TokenERC20.sol in the contracts directory (In Atom, right-click on the contracts folder and then select ‘New File’.)

Edit that file and let’s again copy-paste the boiler plate ERC20 token code from Ethereum. (Here’s a raw link to the source: https://raw.githubusercontent.com/polats/unity3d-blockchain-wallet/master/Solidity/token.sol)

Create another file called 2_deploy_tokenerc20.js in the migrations directory. Copy-paste the code below:

If we look at the other deployment script in the folder, 1_initial_migration.js, we’ll see that 2_deploy_tokenerc20.js has mostly the same code.

The only differences are we’re deploying TokenERC20 instead of Migrations, and we’ve added additional parameters to deployer.deploy. These additional parameters are the constructors for the Smart Contract (totalSupply, tokenName, and tokenSymbol).

TokenERC20 constructor variables highlighted. The project folder also shows the 2 files that we should have already created: TokenERC20.sol and 2_deploy_tokenerc20.js

3. Compile then Deploy our Contract on the Ganache Private Blockchain using ‘truffle console’

Open truffle.js in Atom, and add the details in the file, like below.

Once we’ve defined the network settings, run the following commands in the Terminal:

truffle console

We start the Truffle console, then use it to compile and then migrate (deploy) our contract code. A successful run is shown below:

We can also verify the contract creation on Ganache by going to the Transactions tab:

The contract creation transactions as seen in Ganache. To verify a successful deploy, we can also check the Blocks tab to see the mined blocks with the transactions, or the Accounts tab by checking the first account’s ETH balance and TX count. Truffle uses the first account by default when deploying contracts.

Before going to the next step, let’s retrieve the Token contract’s address by using the Truffle console. In the console, enter the following command:


This should output the contract’s address on the Terminal. We can see that aside from deploying contracts, the console can also be a useful tool for checking blockchain variables as well.

4. Verify the Token via unity3d-blockchain-wallet

We should have the unity3d-blockchain-wallet Unity project checked out from Part 1. Let’s open up the WalletUI scene in the Unity Editor, select the [ManagerComponents] GameObject and change the following variables:

WalletManager | Network URL — replace this with the RPC network details from Ganache. By default it will be

Token Contract Service | Token Contract Address — replace this with the address that we retrieved from Truffle console.

If everything works as expected, we should be able to see the token details that we defined in our deployment script appear on the Unity game view.

Congrats! You have now created an ERC20 token on a private blockchain.

5. Run test scripts using ‘truffle test’

Create a file called ‘TestTokenERC20.sol’ in the test directory, and copy-paste the following code:

The above code runs a test which checks that the decimals variable of our contract should be 18. Running ‘test’ on the truffle console will show the result of all tests found in the project test folder.

Example output of ‘test’ (or ‘truffle test’ if run from outside the console)

More info on tests can be found on the Truffle documentation.

Congratulations and Next Steps

In the next part of the series, we’ll look at deploying other Solidity smart contracts aside from the standard ERC20 token we’ve been using, while still integrating our new contracts with Unity via unity3d-blockchain-wallet.

Thank you for reading! If you liked this post, please give it a few claps so others also get the chance to read it.

Please also leave questions and comments, as well as any suggestions on what you want to know more about on the next article.

CTO and Co-Founder at Outplay.games (Alto.io) | Into Game Design x New Technologies

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store