SimplyVital presents Nexus: Hello World – SimplyVital Health

0 3


The HelloWorld folder is where our project files will go

I started with a Projects directory, and created a HelloWorld folder inside it. The HelloWorld folder will hold all the files we need for this project so that when I’m done I can just remove this folder to clean up the code, blockchain, app, and tests. Incidentally, I’m running Ubuntu/Linux, however you should be able to do the same on Mac or Windows.

git clone into your HelloWorld folder

After I created the directory I opened a terminal (on your system you may refer to this as a command prompt or console), and inside the HelloWorld directory use Git to clone the source code for the Nexus development blockchain. You’ll need to source code to compile the blockchain client for your system.

/Projects/HelloWorld$ git clone https://github.com/Health-Nexus/blockchain.git

You will need to install Git and have an account on github.com as well request access to our private development blockchain repository. To get access to Nexus while our blockchain is development contact us at david[at]simplyvitalhealth[dot]com.

Cloning the repository will require you provide your Github credentials.

From your terminal go into your newly cloned blockchain directory and compile the source code.

/Projects/HelloWorld/blockchain$ make geth

Once complete, you will see the “Done building.” followed by the directions needed to start the blockchain client. The go-ethereum project, on which Nexus is based, is written in Go. Depending on your system you may need to install the needed libraries to compile Go code. For more info on dependencies for your system please checkout the, Installing Geth Guide from the go-ethereum project.

Create a folder for the blockchain data

Now that I’ve compiled the blockchain client, I’m going to create a folder to hold all the data the blockchain will begin to sync once I start the blockchain. To do this I move back into my HelloWorld folder and create new folder called, rather uncreatively, data.

Start the blockchain for the fist time

I opened a terminal window from inside the data directory and started Nexus for the first time to verify the blockchain client was working correctly.

/Projects/HelloWorld/data$ "/Projects/HelloWorld/blockchain/build/bin/geth" --healthnexus --datadir ./ --port 7330

Please look at the screen above to see that the flags need to be denoted with two subtraction characters (which Medium sometimes auto-replaces with a single longer character).

Starting the blockchain will automatically start syncing blocks. Once I verified it was working I stopped it, using Ctrl C. Now that I have a working blockchain client I want to create an account I can use to send transactions to the blockchain.

create a new account

In my terminal, still in the data folder, I use a modified version of the first command (adding “account new”) to the end to create a new account. This process will ask for a password, twice. It is very important to remember this password as it it irrecoverable should you lose it.

Once you complete the process the public address is displayed. In this case my public address is 0xd79B9CCd05B505B69cE0237ddD6a5612D790106C.

Also note that what happens behind the scenes here is that a secret key file is created in the data/keystore directory. This will enable you to move your account to another machine (or wallet) if later you want to relocate your blockchain client.

Now that you have your public address you can follow the instructions you received when you emailed for access to the code repository, to get some development crypto-currency. You will to fund your account before you will be able to deploy transactions.

create a password.txt file

At this point I want to make it clear that I am setting up a development blockchain, that uses free development crypto-currency. Because this is strictly a development blockchain we will not overly concern ourselves with securing our account password, network, or implementing practices that improve security at the expense of introducing unnecessary complexity.

In this I go into the HelloWorld/data directory and create a new file. You can call it whatever you like, I chose password.txt because that’s all this file will contain. Inside the file I place the password I used when I created my new account — just the password, no spaces or newline characters.

start Nexus again, this time with your unlocked account

Now that my password file is created I can start the blockchain again, this time with my account unlocked. In this step not only will I be unlocking my account, but we will be enabling RPC, which will allow us to send transactions from other applications running locally.

/Projects/HelloWorld/data$ "/Projects/HelloWorld/blockchain/build/bin/geth" --healthnexus --datadir ./ --port 7330 --unlock "0xd79B9CCd05B505B69cE0237ddD6a5612D790106C" --password ./password.txt --rpc --rpcapi "eth,web3,personal,net" --allow-insecure-unlock

Once I started my blockchain again, I was to see that it correctly “Unlocked account” and it created the ‘HTTP’ endpoint at 127.00.0.1 (localhost default).

Again, I want to stress, this is generally an insecure way of running a blockchain and us recommend only for this limited developmental mode.

Truffle is a very useful framework for developing decentralized applications in the Ethereum ecosystem. Because Nexus is based on Ethereum, we are able to leverage the Truffle open source framework to develop Nexus applications.

Opening a new terminal (leaving the existing running Nexus in the background), and changing to the HelloWorld folder I installed truffle using:

/Projects/HelloWorld$ npm install -g truffle

You can visit the Truffle website to find out more about the entire suite of open source tools available to help you develop on Nexus. Truffle does require Node.js, so please install that first if you don’t already have it.

inside HelloWorld create an app folder

Inside the HelloWorld folder I created another folder name “app”. This is where we will download an Hello World project from Truffle to deploy on Nexus.

Let’s use a truffle box to get started

Back in my terminal window, inside the new app directory, I use the truffle unbox command to download and install a pre-configured project.

/Projects/HelloWorld/app$ truffle unbox azure-hello-blockchain

Truffle Boxes are the easiest way to get started. They are example applications that range from the simple smart contracts with integrated testing, to full-featured dapps (decentralized applications) implemented in the most popular frameworks.

Browsing the Truffle box page I was able to discover three “Hello World” boxes that implemented a simple sender/response dapp using a smart contract. I picked this one because it provided everything we need to compile, migrate, and test our dapp without any unwanted boilerplate.

The truffle unboxing completes with a new truffle.js file in the app folder you created. Open the truffle.js and modify that file as follows:

module.exports = {
// See <http://truffleframework.com/docs/advanced/configuration>
// to customize your Truffle configuration!
networks: {
nexus: {
host: "127.0.0.1",
port: 8545,
network_id: "*"
}
},
compilers: {
solc: {
version: "0.4.23",
},
}
};

Here we add a new network, nexus, and specify the solc version we need for this box. That saved, now we can move on to compile the smart contracts.

The two smart contracts included with the truffle box are HelloBlockchain.sol and Migrations.sol. Migrations is part of the truffle’s life cycle management where it will keep track of smart contract revisions and selective enable migrating changed smart contracts as needed. The HelloBlockchain smart contract is a fairly simple contract that stores some strings and allows you to set a request message (as the contract owner — Requestor in this case), and sent a response message.

Overly complicated for Hello World

This let’s us put our “Hello, World!” into the blockchain and read it back and allows someone to respond in kind.

For what we want it to do it’s overly complicated, but that also gives us some space to explore more advanced concepts in Solidity, such as inheritance and the design choices behind using (or not) modifiers.

truffle compile

Truffle has some useful tools that makes developing smart contracts easier. Smart contracts in truffle are written in Solidity, the most popular smart-contract language for Ethereum.

Inside my terminal window, while in the app folder, I use the truffle compile command to compile the HelloBlockchain and Migrations smart contract in preparation for deploying them.

/Projects/HelloWorld/app? truffle compile

While compiling you may see some compilation warnings. Solidity has changed since these smart contracts were written, making this syntax obsolete. However, it should still compile if you specified the solc version in your truffle.js as indicated above.

Now compiled, I could use another of truffle’s commands (migrate) to deploy them to Nexus. However, I want to deploy them and run the test all at once.

Again, from the same terminal window we compiled our smart contracts run the tests using truffle test.

/Projects/HelloWorld/app$ truffle test --network nexus

Using the network flag in our truffle command told Truffle to use the network we configured when we modified our truffle.js earlier. Truffle will first start by looking to see if the contract needed to be compiled again (or first if you never compiled then), then quickly move onto running the test in the app/test folder.

A quick look shows that it’s deploying the HelloBlockchain to the blockchain and checking for the initial state to be as expected before setting our request message to Hello World!

And that’s how you deploy a simple smart contract to Nexus using popular open source tools. Note: If you try this before receiving the development crypto currency on your account, then this will fail to work and you may receive unhelpful warnings about “exeeding gas limit”.

truffle migrate

If you’ve gotten this far you can now use truffle migrate –network nexus to deploy the contracts and get back the contract address so you can interact with it independently or using other tools.

truffle migrate --network nexus

So I deployed the smart contract to Nexus and used it to update the Response Text stored on the blockchain to “Hello World”, which is enough to get you started as you explore the tools for Dapp development.

Here’s what I was able to do in the process:

  • Build the Nexus blockchain client
  • Create a new account on the Nexus development blockchain
  • Start the Nexus blockchain for development
  • Use Truffle to deploy a smart contract to Nexus

What I wasn’t able to fully explore, because of length constraints, was the built-in testing for Truffle, how to extend testing using more familiar Javascript testing frameworks, or how using a pure development blockchain like Ganache can greatly increase your efficiency in dapp development.

I used Truffle Boxes partially with the hope that you would poke around there a bit and see what other starter projects could provide inspiration for your own dapps.

If you’re interested in developing healthcare related applications that leverage decentralized technology, contact us at SimplyVital Health or at our following sites. We are also hiring!

https://angel.co/company/simplyvital-health

https://www.simplyvitalhealth.com/

https://www.facebook.com/SimplyVitalHealth/

Please contribute to the discussion in the comments below. Thanks for reading!

You might also like

Pin It on Pinterest

Share This

Share this post with your friends!

WhatsApp chat