Setup Nebulas Development Environment

A walkthrough to your first deployed dApp

I recently discoverd Nebulas, a third generation Blockchain that already as a funcional mainnet and an awesome amount of Code/Apps/dApps and developers all around the world.

As of writing this text the Nebulas Incentive Program is still ongoing. Although some time has passed since my last development of code, I thought I’d give it a shot. Successfully submitting a dApp to the programm will be rewarded with at least 100 NAS (about 550$ right now). So, worth a shot.

Nebulas is located in in China, so a lot of the documentation is not translated yet. It took me quite a while to get to the point where I could actually start working on the code itself. For that reason I will provide a detailed guide to your first deployed dApp on Nebulas.

I am located on a Windows 10 PC but the Windows Code for the Nebulas-Node is not released yet, so we need to cover that. And I want to do quick developent/testing, so working on local files would be great.

This guide will cover the following points:

  1. Point out important ressources of information/code
  2. Setup virtual mashine, running a “Privatenet”
  3. Setup Adresses for “Privatenet”
  4. Setup your PC for development
  5. Deploy a simple dApp to “Privatenet”
  6. Edit Frontend of the deployed dApp

1. Important ressources of information/code

At the moment there are plenty of ressources to learn about Nebulas. But they are distributed all over the Net.

Here are the most important ones I discovered:

2. Setup virtual mashine, running a “Privatenet”

Initially I wanted to test on Testnet but its then I thought it would be nice to start testing on a pricatechain. That way I can have a fast and private network and be ablte to see the Console outputs on the blockchain itself and delopy to testnet later. So I setup a virtual mashine to run a priave and a testnet on it.

The easiest way to to this, is to install a VM running Ubuntu 16.04 either local or on a VPS. I choose Vultr.com for testing. A 10$/month plan will do it for the Node, but the blockexplorer is in java and it needs a 20$+ plan.

I setup two installation scripts for that.

  • Install Nebulas Node only: You can use this if you already have an Ubuntu system. It will install all packages required for a fully functional Nebulas Node and — if started manually— run two instances (required for private chain) of Nebulas as a “Privatenet” or one instance of Testnet/Mainnet.
  • Install a “full” DevelopNode: This is an attempt to give a more complete environment for Nebulas development. It will also start a “Privatenet” but also installs the Nebulas wallet, a Webfronend and later the Blockexplorer for your Privatenet.
    To get this running, connet into your VPS via SSH and just let the script to the work:
curl -sSL https://raw.githubusercontent.com/mirei83/NebuEnv/master/SetupEnvironment.sh | bash

This will basically do alle the work. It takes about 15 Minutes on a dualcore mashine. You can simply check the Script. At the moment the following tasks are covered:

  • Install all packages needed
  • Install GO
  • Install RocksDB
  • Install dep
  • Install Nebulas
  • Modify Nebulas to be able to run a Privatenet on a single VPS
  • Install Nebulas WebWallet
  • Prepares WebWallet for Privatenet
  • Installs Blockexplorer
  • Prepares Blockexplorer for Privatenet
  • Puts Nebulas + Blockexplorer in autostart

This is a first release. It will be bigger some time.
After the Script it finished, you can either reboot the VPS or type

./start-nebulas-privatenet.sh
./explorer-privatenet.sh

You should now see some “neb” processes via “ps aux | grep neb” and entries in log/neb.log

After the setup you can access the Dashboard on http://YOUR.NODE.IP.ADD

3. Setup Adresses for “Privatenet”

Now that we have our Privatenet, we need wallets to send some Privatenet-NAS. The Privatenet is configured to initially give 5.000.000 Coins to the Address n1FF1nz6tarkDVwWQkMnnwFPuPKUaQTdptE. Also all blockrewards will go to this address. The privatekey of that address is alredy on the node. We can distribute the coins from here. But to do that, we need Testaddresses.

So lets interact with our new Blockchain!

Go to the Nebulas directory. This minimizes unresolved variable-issues.

cd $GOPATH/src/github.com/nebulasio/go-nebulas

From here, we can create some Testnet adresses:

./neb account new

Enter a Password and note the new address. The output should look like this:

root@Nebulas_Autoinstall:~/go/src/github.com/nebulasio/go-nebulas# ./neb account new
Your new account is locked with a passphrase. Please give a passphrase. Do not forget this passphrase.
Passphrase:
Repeat passphrase:
Address: n1LEHMKzsDHgyi7wsXB7QSTgtHv712Dj2da

Repeate this as often as needed.

The Keystore files (and some preset ones) are stored in the folder keydir. Copy the new created wallets locally (with WinSCP).

$GOPATH/src/github.com/nebulasio/go-nebulas/keydir

Lets check the balance of the new wallets with this command:

curl -i -H Accept:application/json -X POST http://localhost:8685/v1/user/accountstate -d '{"address":"n1LEHMKzsDHgyi7wsXB7QSTgtHv712Dj2da"}'

And the answer:

{
"result": {
"balance": "0
",
"nonce": "0",
"type": 87
}
}

Now lets do our first transaction! We will send 1NAS from our master wallet to n1LEHMKzsDHgyi7wsXB7QSTgtHv712Dj2da, the newly created wallet.

curl -i -H 'Content-Type: application/json' -X POST http://localhost:8685/v1/admin/transactionWithPassphrase -d '{"transaction":{"from":"n1FF1nz6tarkDVwWQkMnnwFPuPKUaQTdptE","to":"n1LEHMKzsDHgyi7wsXB7QSTgtHv712Dj2da", "value":"1000000000000000000","nonce":1,"gasPrice":"1000000","gasLimit":"2000000"},"passphrase":"passphrase"}'

Now check the balance again.

curl -i -H Accept:application/json -X POST http://localhost:8685/v1/user/accountstate -d '{"address":"n1LEHMKzsDHgyi7wsXB7QSTgtHv712Dj2da"}'

And the answer:

{
"result": {
"balance":
"1000000000000000000",
"nonce": "0",
"type": 87
}
}

Yeeha! Blockchain is working!

4. Setup your PC for development

Now we need some color. As I said, I am on Windows. There are some Tricks to be done to get started. So I will safe you the pain of finding out yourself.

  1. Install Chrome Browser
  2. Install NebulasExtensionWallet in Chrome (it’s not the Web-Wallet)
  3. Allow access to local files for NebulasExtensionWallet as shown here. Otherwise the NebExtWallet cant access the local frontendfiles corretly.
  4. Add “ — allow-file-access-from-files” to Chrome startup as shown here. You need to add this attribute to open local Javascipt Files.
  5. Install a Code Editor (Visual Code Studio is nice)
  6. Connect to our Node:

In Order to be able to interact with the WebExtensionWallet and NebPay in our Privatenet, we need a litte trick. 
The WebExtensionWallet can only connect to Mainnet, Testnet, and localhost. But we do not have a node running on localhost — but we do have a remote Node. So we need to set up a portforwarding of Port 8685 to our node via the SSH Tunnel. You can do this easy with Putty, just keep the connection open:

Putty Portforwarding of port 8685

With this activated, the WebExtensionWallet will connect to localhost, wich connects to our Privatenet. Cool!

Now you should be able to select “localhost:8685” in the WebExtensionWallet and open a Keystorefile of the privatenet, copied earlier.

WebExtensionWallet connects to Privatenet

5. Deploy a simple dApp to “Privatenet”

First of all: Watch the Youtube Video of HardlyDifficult! Its a good Video to follow. We are going to deploy the “DecentralizedRNG” dApp (wich is also already on mainnet) to your privatenet.

  1. Download Sourcecode from here and extract is somehwere.
  2. There is a File Called “DecentralizedRNGContract.js”. That file cointains the smart contract for the Blockchain. Open that file in an editor and copy its content.
  3. The easiest way to deploy a contract is through the Web-Wallet, wich is availibe at our VPS at http://IP.TO.YOUR.VPS/web-wallet. The Web-Wallet on the node will only connect to the privatenet on the node.
  4. Go to Contract -> Deploy and select a wallet with some NAS on it. Now paste the smart contract into “code”.
  5. Click Test and the result should look like this:
{"result":"\"\"","execute_err":"","estimate_gas":"21566"}

6. Now press Submit and the contract is already deployed!

7. Write down the “contract_address”, we need this in out local files.

Submitted SmartContract

6. Edit Frontend of the deployed dApp

Now we need to edit the Frontend to interact with our smart contract.

  1. Open the file “DecentralizedRNG.js” and change the contract address to the new address we just created.
  2. Open results.html and change
var is_mainnet = true;
var nebulas_chain_id, nebulas_domain;
var gas_price = 1000000;
var gas_limit = 200000;
if(is_mainnet) {
nebulas_chain_id = 1;
nebulas_domain = "https://mainnet.nebulas.io";
} else {
nebulas_chain_id = 1001;
nebulas_domain = "https://testnet.nebulas.io";
}

to

var is_mainnet = false;
var nebulas_chain_id, nebulas_domain;
var gas_price = 1000000;
var gas_limit = 200000;
if(is_mainnet) {
nebulas_chain_id = 1;
nebulas_domain = "https://mainnet.nebulas.io";
} else {
nebulas_chain_id = 100;
nebulas_domain = "http://localhost:8685";
}

Thats it! We now have local html/JavaScript file interaction with NebPay and NebPay interacting with our own privatenet.

7. Test the dApp

Now open the index.html in chrome and interact with the NebExtentsionWallet.

Make sure, that Chrome is started with the attributes mentioned earlies!

If this article helped you, consider a small donation: n1HLSuo2h689WShKnwUnEaMHQiXQ7eEqbEk