Building dAPPs as a Bhad Guy: Part One

0
14
Johnny Bravo (A Typical Cool Bhad Guy) a 90s Cartoon from Cartoon Network.

In this guide, we will cover how to get started with Building Decentralized Applications or dAPPSs and what are the required things you need when developing one. The Blockchain development field is constantly evolving and you must keep refreshing your knowledge from different sources.

Getting Started

Blockchain has a wide-ranging set of applications and almost any field can benefit from having such technology at its disposal. Blockchain is also a way by which trust can be ensured in the online world and lets us have greater confidence in the range of products and services that we use online.

How To Code A Decentralized Applications or Dapps

Dapps are a new way of thinking about how we can go about writing applications for the internet. Previously we usually have the backend code residing in a server or a set of servers, dAPPs let us run web-enabled applications where the back end is instead hosted on a blockchain network which executes the code that is needed for it.

What makes an app a dAPP?

Firstly, let’s take a shallow dive into the history of regular apps. An app is a short form for application, the word app became common since the innovation of smartphones like the iPhone and Android which came in some time in 2007. Developers around the world began to build software programs that run on these smartphones operating system, today we have them as web or mobile apps. Apps were part of the new wave back then. We usually have new apps released every week since the date this innovation was announced and this kept trend going on and on till it became more evolved and advanced.

dAPP is something new and it all started with the release of the Ethereum Blockchain(The second most popular Blockchain network). dAPP is a short form for decentralized application and is typically an app that runs on a blockchain such as Ethereum or EOS. These apps can be programmed as smart contracts a protocol intended to digitally facilitate, verify, or enforce the negotiation or performance of a contract, or software that is executing massive computations which are shared across hundreds or thousands of computers called Nodes.

Qualities of a dAPP

  • They run on the blockchain.
  • Their code is made open-source operates autonomously without any person or group controlling the majority of tokens.
  • They generate DAPP tokens to provide value to their contributing nodes.
  • Users are granted access to them in exchange for tokens.
  • Miners are rewarded with tokens when they successfully contribute to the ecosystem
Architecture Example of dAPP built on the Ethereum Blockchain.

Getting Started

The first thing we need to put in mind is picking what to learn, we have been able to define a dAPP, explained the qualities of a dAPP and highlighted some key examples of Blockchain Systems that support building dAPPS. Just like having a programming concept or idea, there is a wide range of platforms available for you to code in the blockchain space.

While each platform has its pros and cons, you need to see how your application is structured, a feasibility study will be advised for you to conduct before starting to build so you don’t use inappropriate services for your dAPP.

What is needed to build a dAPP

Presently, we are familiar with modern-day computing, it is important to have all the required components of a tech stack with their counterparts in the decentralized world. However, it is not trivial to find out and do your research on each of these components. Thankfully, we have an infographic that could be used to get a sense of the ecosystem:

On web 1.0 and 2.0 we have solutions for computing, e-commerce, file storage, API calls (external data), monetization and payments. But our main focus is on web 3.0 in which solutions like Ethereum is currently bringing a paradigm shift and how we can tackle the computing aspect of the internet.

Since computing is one of the most important aspects of building dAPPs, it is very important to get familiar with it, by interacting with its enabling technology such as smart contracts.

Smart Contracts

In 1994, Nick Szabo (a cryptographer), came up with the idea of being able to record contracts in the form of computer code. This contract would be activated automatically when certain conditions are met. This idea could potentially remove the need for trusted third-party companies (such as banks).

Smart contracts are the programs that we write on the blockchain and let us interact with them. It is in a smart contract that we can define business logic and let us code the rules with which we want to interact with the blockchain. The most powerful feature of a smart contract is the fact that once it is deployed on to the blockchain, it is immutable and you cannot go back and edit the programs.

This makes us embrace an innovation where we have a piece of code with the logic that we want in it, which is immutable and can execute by itself when it is called.

In other words, we could say the smart contract is the piece that truly unleashes the power of the blockchain and hence it is important to learn how to develop smart contracts on the blockchain.

A Simple Diagram illustrating how smart contracts work.

Ethereum: The Era For Decentralized Applications

Ethereum is one of the most popular blockchain platforms that exist right now. It was released to the public for more than 5years now and it has a thriving community that maintains the project.

The platform also has a developed programming language called Solidity that lets us write smart contracts on the Ethereum blockchain.

Ethereum has a public blockchain that allows people to run code on it. It is one of the simplest ways to start programming on the blockchain.

Gas Fee

The Ethereum blockchain has a concept of paying for transaction charges(Gas Fee), it is called Gas. The Gas fee is very important for you to understand before learning how to write a smart contract or develop a dAPP.

Gas is the amount of fuel that you need to pay to get your transaction executed. The transaction could either be used in deploying your contract on the blockchain or can be used for running a function that already exists. Every time a function is called, some code is executed. This code is executed on the computer of the person who is mining the transaction and requires computation power — to incentivize people to share their computational power and execute the transaction, each operation is charged some gas depending on the complexity of the transaction. The Gas in an ethereum smart contract can range to 21 million. The gas that your transaction needs is multiplied by the gas cost, which is used to get the gas price.

Building Smart Contracts with Solidity

Solidity

Go to https://remix.ethereum.org. This is an IDE to write and execute the smart contract.

Ethereum Remix Interface

Create a New File by clicking on the (+) symbol on the top left corner of the IDE. Copy and paste the following codes on the code section in the Ethereum Remix IDE.

Module One

Simple HelloWorld Program.

pragma solidity ^0.4.24;
contract HelloWorld {
string saySomething;
constructor() public  {
saySomething = "Hello World!";
}
function speak() public constant returns(string itSays) {
return saySomething;
}
function saySomethingElse(string newSaying) public  returns(bool success) {
saySomething = newSaying;
return true;
}
}

In the remix IDE, switch the execution environment to the JavaScript VM. Now, you can press the deploy button to execute the smart contract. After this, run the contract AssetTransfer with the functions available.

A Simple HelloWorld program is written in Solidity

The first line tells you that the source code is written for Solidity version 0.4.24. This is to ensure that the contract is not compilable with a new (breaking) compiler version, where it could behave differently. Pragmas are common instructions for compilers about how to treat the source code (e.g. pragma once).

You can interact with the contract by selecting any function from the list of functions available on the left-hand side panel of the Remix IDE Interface.

Module Two

Advanced Smart Contract Program — Asset Transfer Program in Solidity

pragma solidity >=0.4.25 <0.6.0;
contract AssetTransfer
{
enum StateType { Active, OfferPlaced, PendingInspection, Inspected, Appraised, NotionalAcceptance, BuyerAccepted, SellerAccepted, Accepted, Terminated }
address public InstanceOwner;
string public Description;
uint public AskingPrice;
StateType public State;
address public InstanceBuyer;
uint public OfferPrice;
address public InstanceInspector;
address public InstanceAppraiser;
constructor(string memory description, uint256 price) public
{
InstanceOwner = msg.sender;
AskingPrice = price;
Description = description;
State = StateType.Active;
}
function Terminate() public
{
if (InstanceOwner != msg.sender)
{
revert();
}
State = StateType.Terminated;
}
function Modify(string memory description, uint256 price) public
{
if (State != StateType.Active)
{
revert();
}
if (InstanceOwner != msg.sender)
{
revert();
}
Description = description;
AskingPrice = price;
}
function MakeOffer(address inspector, address appraiser, uint256 offerPrice) public
{
if (inspector == 0x0000000000000000000000000000000000000000 || appraiser == 0x0000000000000000000000000000000000000000 || offerPrice == 0)
{
revert();
}
if (State != StateType.Active)
{
revert();
}
// Cannot enforce "AllowedRoles":["Buyer"] because Role information is unavailable
if (InstanceOwner == msg.sender) // not expressible in the current specification language
{
revert();
}
// Check the following Link to see the complete code.
An Asset Transfer Smart Contract Program

let us see the various data types that are used in solidity:

  1. Uint — unsigned int that can take only positive values.
  2. Address — a type that is created in ethereum that can store wallet addresses.
  3. Mapping — mappings are very useful to keep track of key-value pairs. They are declared as mapping(_KeyType => _ValueType). If we make the KeyType as an address, we can keep track of certain information against a particular address.
  4. Bool — a boolean variable that can be either true or false.

Some Key properties of solidity

Events

Events let other programs (or internal function) listen to a particular event on the blockchain and then react after it is executed. As soon as the event is fired, all the parameters that are called with the event are passed on the code that is listening to it and this can do some additional operations on the code. This is very useful for Javascript callbacks that might be interacting with a dAPP.

Constructor

Constructors in Solidity have the same name as the contract and have to be public in scope. In this contract, we use it to initialize the price and description variables and set a state to detect if it as active.

Function

There are four types of Solidity functions: external, internal, public, and private.

  • Modifiers change the way functions work.
  • Functions can be set as view and pure to restrict reading and modifying of the state.
  • Function overloading occurs when several functions in a contract have the same name but differing arguments.

I will be updating this article soon with the link to read Part 2.


Building dAPPs as a Bhad Guy: Part One was originally published in Coinmonks on Medium, where people are continuing the conversation by highlighting and responding to this story.