Ethereum Truffle and Testrpc with Cloud9

Here are the steps I’ve performed to get ConsenSys’s Ethereum Truffle working on Cloud9.  Note that I am new to a lot of the things being used here, so if you see something that I’m doing wrong or “the hard way” then let me know!  I hope this post helps someone else, but I also hope that I get some good feedback.

Installation Script

After creating your Cloud9 workspace run this script (we’ll break it down afterward):

#!/bin/bash

# pyethereum
cd ~
git clone https://github.com/ethereum/pyethereum/
cd pyethereum
sudo python setup.py install

# eth-testrpc
sudo pip install eth-testrpc

# nodejs v5.0.0
. ~/.nvm/nvm.sh
nvm use v5.0.0
nvm alias default 5.0.0

# truffle
sudo npm install -g truffle

You can find the latest version of this script in my Futarchy project at https://github.com/josiahwood/futarchy/blob/master/cloud9setup.sh.

Ethereum

# pyethereum
cd ~
git clone https://github.com/ethereum/pyethereum/
cd pyethereum
sudo python setup.py install

Lines 5-7 are just copy/pasted from the installation section of https://github.com/ethereum/pyethereum/. It works perfectly as advertised. This is required for testrpc to work in the next section.

Testrpc

# eth-testrpc
sudo pip install eth-testrpc

Line 10 is all you need to install testrpc according to https://github.com/ConsenSys/eth-testrpc. I’ve found Testrpc to be the quickest way to get up and going with writing contracts.

Update Nodejs

# nodejs v5.0.0
. ~/.nvm/nvm.sh
nvm use v5.0.0
nvm alias default 5.0.0

Since Truffle uses npm you’ll want to put Nodejs at your desired version first. I found version 5.0.0 worked very well with truffle. Lines 13-15 were the most consistent way I found to update Nodejs and make the new version the default. Line 15 is important to make sure all new Cloud9 terminals are using the correct version of Nodejs by default. This also affects what happens to Cloud9 terminals if you’ve been logged off for a while. Cloud9 advertises that your environment will be exactly the way you left it, but the terminals do reset after some timeout.

Truffle

# truffle
sudo npm install -g truffle

At this point line 18 works exactly as documented in the installation section of Truffle at https://github.com/ConsenSys/truffle.

Cloud9 Environment

Ok, this part took me a while to get working on Cloud9. I had no problems running on a local Ubuntu virtual machine, but I wasn’t so lucky on Cloud9. You can see how I worked out the problem on StackOverflow at Ethereum Test RPC working in Cloud9 with Truffle.  Credit for the final answer goes to XMLHttpRequest cannot load cloud 9 io.

In order to make it possible for testrpc to bind to the public IP address of your Cloud9 environment you’ll need to click on the “Share” button in the upper right-hand corner of the Cloud9 IDE and then make the Application link public. The StackOverflow answer indicated that this was due to a recent bug in Cloud9, so this may not be necessary in the future.

Truffle Project Environment/Configuration

Once you have some code/contracts you want to try out you’ll need to start testrpc first. Do this by executing testrpc -p 8081 -d 0.0.0.0 in a new Cloud9 terminal. The -p 8081 argument tells testrpc to run on port 8081. Cloud9 only opens a few ports for public access according to https://docs.c9.io/docs/multiple-ports so we can’t use the testrpc default of 8545. Port 8080 will be used later by the truffle serve command, so 8081 is the next open port. The -d 0.0.0.0 argument tells testrpc to bind to all available IP addresses, including the public one.

Now that you have testrpc up and running, you’ll need to configure your Truffle project properly. You can do this a few ways but the key is to get this part in your config file:

"rpc": {
    "host": "project-user.c9users.io",
    "port": 8081
}

Replace project-user.c9users.io with the Application URL you made public in the “Cloud9 Environment” section above.

You can either update this in your config/app.json file (to make it the default for all environments), one of the existing config/[environment]/config.json files, or make a new environment just for Cloud9.

After deploying your contracts with truffle deploy you can now run truffle serve and your HTML/JavaScript will be visible on port 8080 of your Application URL. Since this is conveniently the default port for Cloud9 it automatically gets redirected from port 80. This allows you to see your project directly at your Application URL like “http://project-user.c9users.io”.

Conclusion

Thanks for reading! Get coding!

Leave a Reply

Your email address will not be published.