4 Things I Learned Coding My First Blockchain Application

As a software developer, I thought it would be interesting to spend some time getting my hands dirty with creating blockchain applications in order to better understand the implications of this new technology. Here are 4 things I learned during my first experience, 2 tools to get started and 2 learning resources to help developers and non-techies alike learn more about Decentralized Applications (DApps). 

Lessons Learned

1. When deploying an application that is on the blockchain, every device that is running the application must have an entire version of that blockchain downloaded onto their device. This is completely different from the cloud-based architecture that I was used to, where users can simply access only the data they need. 

Thankfully, there are solutions that work around this problem like hosted blockchain servers so that users don't have to spend a ton of RAM and memory just to use decentralized applications, but workarounds brings trade-offs with regards to security.  

2. Coding within the Ethereum architecture is not that different from modern standards of web development. Using Web3js, we can hook directly into instances of an Ethereum Virtual Machine directly from the browser. However...

Just like transactions, code can sit within the blockchain, but code deployed to the blockchain is immutable. That means that in order to make changes to an application that has already been compiled and deployed, we must deploy an entirely new version of the project - no patches, only upgrades. 

3. To get started programming decentralized applications I used Solidity, a language that intereacts with the Ethereum blockchain using contracts. From a developer's perspective, contracts are kind of like classes: they get initialized with constructors and can execute functionality using both public and private functions. 

To provide context, here's the code of an example Contract:

pragma solidity ^0.4.18;

contract Voting {

  mapping (bytes32 => uint8) public votesReceived;
  bytes32[] public candidateList;

  function Voting(bytes32[] candidateNames) public {
    candidateList = candidateNames;
  }
  // ... more functions here ...
}

4. From an external perspective, contracts are more like APIs. Their public functions can be called from external services based on certain protocols. For example, a function may require an integer user_id, and that user_id can be cross-referenced against the list of users in the application before returning information about it. 

Tools

1. In order to communicate with an instance of the blockchain, it's vital to use a Remote Procedure Call (RPC). Ganache is an amazing tool that easily lets you emulate a personal blockchain instance and interact with it via RPC for development and testing purposes. Upon install, it seeds 10 fake users with Ethereum balances for easy testing of application code without deployment. 

Screenshot 2018-01-20 19.05.13.png

2. I was also amazed by the ease of use getting started with using Javascript within Ethereum application thanks to Web3JS. Almost instantly upon starting up, I was able to write readable javascript code that interacted with my Solidity contract directly through a simple javascript file.

function voteForCandidate(candidate) {
 candidateName = $("#candidate").val();
 try {
  contractInstance.voteForCandidate(candidateName, {from: web3.eth.accounts[0]}, function() {
   let div_id = candidates[candidateName];
   $("#"+div_id).html(contractInstance.totalVotesFor.call(candidateName).toString());
  });
 } catch (err) {
 }
}

Props to the Web3JS for the groundwork they have laid to make blockchain development more accessible through their API. 

Resources 

1. If you're interested in learning more about the core principles of writing Solidity code and building expressive smart contracts, then you should check out CryptoZombies, an awesome beginner-friendly game to learn DApps and build a zombie army. 

2. If you're more leaning towards understanding the core principles of the blockchain and quickly deploying a sample app that you can play around with, then definitely do this course from Zastrin. I found the technical explanations of difficult concepts to be engaging and understandable, and the end result of deploying my first app was very empowering! I have added my source code for the application on Github. 

I'm already looking forward to taking this a step further and building more complex DApps. Any questions about this post? Let me know in the comments below.