Deploying Your First App to Loom PlasmaChain: Installing Loom, Setting up Your Environment, and Generating Keys
Intro to Loom PlasmaChain
If you’re a developer looking to build scalable DApps on Ethereum then you’ve probably run into quite a few problems dealing with transaction throughput and cost per transaction.
Enter Loom, a network of sidechains using Delegated Proof of Stake that increases throughput while checkpointing their security using Transfer Gateways to Ethereum mainnet. To learn more about Loom, check out this handy guide.
PlasmaChain is one of the blockchains in the network of Loom blockchains that focuses on providing a robust platform for blockchain games. These chains make use of Transfer Gateways which allow you to transfer assets from the sidechain (in this case, PlasmaChain) to Ethereum mainnet (or any other chain for that matter).
1. Install Loom and Create Your Work Environment
First step to learning something new is just starting to play with it! Let’s go ahead and install the Loom binary on our system, so we can have a local chain to test with. I’m using Linux as my platform, but the steps are basically the same across Linux and Mac. If you’re using Windows, try checking out WSL, but I’ll warn you, it’ll be buggy sometimes. Alternatively, run the chain in a virtual machine or on a remote server for testing, or just go straight to using the testnet — it’s your choice!
$ curl https://raw.githubusercontent.com/loomnetwork/loom-sdk-documentation/master/scripts/get_loom.sh | sh && chmod +x loom
If this command doesn’t work for you (for example, it doesn’t detect that I’m running Arch Linux as the officially tested versions are for debian, ubuntu, and CentOS), then you can alternatively download the latest binary directly using:
$ curl -sL -o loom https://private.delegatecall.com/loom/linux/stable/loom && chmod +x loom
Then we’re going to initialize the genesis file and chaindata directory by running:
$ ./loom init
To start our chain, we can use:
$ ./loom run
By default, the chain runs on port 46657, and we can use the provided block explorer to see the transactions in real time if we so like:
(If you are not hosting the chain locally, simply change the URL and port number at the bottom to point to the location where you are hosting the chain.) Block generation time is 1 second, so we can have near instant transactions.
One last useful command to note is:
$ ./loom reset
This command will, as it says, reset the chaindata without clearing the config files to give you a fresh chain. Simple run the ./loom run command to start the chain fresh.
2. Generate Keys
To submit transactions to our chain, we’ll also need to create some keys.
$ ./loom genkey -k priv_key -a pub_key
This will create priv_key and pub_key files in your directory containing your keys. Note that it will save your keys in encoded format, and we’ll need access to the raw public key that it outputs, so make sure to save the local address public key output somewhere so we can access it easily later.
3. Sample Code
Let’s create a very simple contract to deploy on our chain. If you don’t have truffle, now would be a good time to get it. Truffle is a framework for creating, testing, and deploying complex solidity applications.
Once you have truffle installed on your machine, let’s clone the example repo from Loom’s GitHub:
$ git clone https://github.com/loomnetwork/truffle-dappchain-example $ cd truffle-dappchain-example
# copy the private key generated earlier to the root directory of the example repo
$ cp ../priv_key extdev_private_key
You can use Yarn to download and install the dependencies.
Let’s take a look at what’s in this repository that’s interesting:
The extdev_private_key is the key we generated, and we will use to deploy our code to the testnet. The priv_key and pub_key files in the truffle-dappchain-example directory are the same keys for deploying to a local chain.
truffle_config.js is where we define the different networks we can connect to. Let’s take a look at what’s inside:
Specifically, if we look at the networks object, we’ll see that there are currently three networks defined.
loom_dapp_chainis defined as the local chain network and used for quickly testing.
extdev_plasma_us1is defined as the plasma chain Loom testnet, and to deploy to that we will need some tokens.
rinkebyis the Rinkeby Ethereum testnet, which we will use later in our Transfer Gateway article.
This is a great template for learning how to add your own networks (for example, a test network on AWS, or maybe add Ropsten as an alternative Ethereum network). If you don’t have an Infura API key for connecting to Ethereum nodes, you can get one here or simply run your own node connecting to whichever node you like.
Finally, in the Contracts folder, you’ll find a series of sample contracts going over how to create ERC20 and ERC721 tokens for PlasmaChain.
This tutorial will only deal with SimpleStore.sol (the others will be covered in our upcoming article). SimpleStore is, as it’s moniker suggests, a simple contract to store and retrieve a value on the blockchain. Nothing fancy, no tokenization, just a barebones contract. This should give you a good hackable contract to supplement with features as you learn.
Just like Ethereum charges gas for deploying smart contracts, deploying smart contracts on Loom blockchains also requires some funding. Paying for deployment of your contracts costs Karma. Unlike Ethereum, however, you don’t have to pay for transactions to and from your contracts. The contract owner would normally pay a monthly fee and all transactions to their contract would be free.
The PlasmaChain testnet is free because it’s intended to be used as a learning and prototyping environment. You only need to provide Karma tokens (which you can get for free from the faucet) and do not have to worry about this monthly fee.
To deploy to your local chain, first make sure your local chain is running by doing:
$ ../loom run
Then in a different terminal let’s first install the dependencies (only need to do this once).
$ npm install
Then we can deploy using Yarn.
$ yarn deploy
By default, yarn deploy will deploy to the loom_dapp_chain network. This network is your local chain and won’t cost any tokens for deployment. You can specify a different network using ‘:’ and a full list of available deploy options are available in the package.json file.
If you try to run the following deployment, you’ll get an error telling you that you don’t have enough Karma tokens. This is because extdev deploys to the PlasmaChain testnet, which, as we mentioned before, requires funding to successfully deploy your code.
$ yarn deploy:extdev
Navigate over to:
https://faucet.dappchains.com/ and use that public key we saved from the genkey command. Note that the faucet is looking for an Ethereum-style hex address and not the encoded style that’s in the pub_key file. Make sure to use that hex address from step #3 that you saved somewhere.
Give it a couple seconds and ‘faucet-karma: 100’ should show up on the right side telling you that you’ve received 100 karma tokens to use.
Now if you do:
$ yarn deploy:extdev
Truffle will run through and deploy SimpleStore (along with all other contracts as defined in the migrations) and return to you the addresses they all deployed on. You can interact with these contracts using the
extdev-plasma-us1.dappchains.com endpoint in web3 and treating it like a regular chain.
In the webapp portion of your DApp, simply add:
var extdev = new Web3('
In our next article, we will cover how to create ERC721 tokens on PlasmaChain and transfer them to Rinkeby Ethereum testnet.
Loom Network is a platform for building highly scalable DPoS sidechains to Ethereum, with a focus on large-scale games and social apps.
Want more info? Start here.
Fan of blockchain gaming? Check out Zombie Battleground, the world’s first desktop and mobile card game that runs fully on its own blockchain.
And if you enjoyed this article and want to stay in the loop, go ahead and sign up for our private mailing list.