Trusted by millions

Blockchain Development Part 1

We are now ready to build a blockchain system. In this system we are now building a digital currency at a very small unit. In Blockchain Development Part 1 we are now naming our currency HowCoinOur Howcoin can do following three basic steps in this tutorial
  1. getBalance : To get balance or recharge account
  2. Users: create or add new users to our network
  3. Transfer: transfer amount among users.
For this purpose we have now created a file named howcoin.rb and create properties for it as shown below.Here we are taking a global variable to save the state in the same file named BALANCES. And defines our first user in it with his balance amount. This one is considered as our dummy userHere we are creating Johns as our user with an amount of 1 million Howcoins.In case of “get balance” users just need to know how much balance he has. For this purpose we need to get the requestee user in “get balance” and show him his balance as shown below. After get “/balance” do add the following code.Next step is now to create a user. In this case we are now creating a user but before creating the user we will also check that there is no existing user with the same name otherwise properties will override. After post “/users” do code add the following statement In case of transfer it is a little bit tricky as compared to previous one. Here we need some checkpoints to make the system secure and safe. First we need to get users and amount param from the request after that we will perform validation checks and if it passes all the check system will make a transaction successfully. After  post “/transfer” do line add this code This is all we need to do with our Howcoin.rb file for now. Next we are going to open our client.rb file and call these requests we had written in howcoin.rb file. In the client.rb file we will need to set our host and port first before proceeding further. Once it will set we now include our files and with this file we will call out its methods as shown below.That’s all for what we have to code in this phase. Let’s run this code and see what happens with the following command.Into terminal or command window goto the specific project folder and with in this folder run the following commandruby howcoin.rbYou may need to install “sinatra” and “colorized” for ruby before running the above command. It can be installed by following commands  1- sudo gem install sinatra-contrib  2- sudo gem install colorizeNow after running ruby howcoin.rb you will get the following output.It shows our server is now in the running stage and we can now use our methods. Open a new command line window for calling our methods and type “irb” to interact it with server Here we load our client script to proceed further by following commandAfter loading this file we can now call our methods like getbalance, createUsers and transactions

1- GetBalance

For getting balance of specific person we need to call method likeget_balance(‘johns’)

2- CreateUser

Now we are creating user with name “smith” as shown below. For this purpose we need to call method likecreate_user(‘smith’)3- Perform TransactionFinally we are transferring money from  Mr. Jonhs to Mr. Smith amount of 100 “HowCoins”. we need to execute our transfer method astransfer(‘johns’, ‘Smith’, 100)Super we can see that Mr. Jonhs has 99900 remaining and Mr Smith got 100 HowCoin.This is not the end. We have to face many problems in this program. See next post to proceed with Blockchain development.

Conclusion

Here you can download complete source code of each step