Demystifying Decentralized Applications

Demystifying Decentralized Applications

Decentralized applications (DApps) operate without a central authority or central point; they are built using Smart contracts and run on decentralized systems. They are used for various purposes, like identity verification, financial transactions, etc. They provide several benefits over traditional applications, like increased security, trust, and transparency.

In this article, we will learn how DApps work, their data storage, and how to build and deploy DApps using Remix IDE.

Outline

  • How do Decentralized Applications work?

    • DApp Architecture

    • What are Smart Contracts

  • Decentralized Storage

  • Build and Deploy an Ethereum Wallet using Remix IDE

    • Interact with the Deployed Wallet
  • Conclusion.

How do Decentralized Applications work?

Decentralized Applications are built using decentralized technology. Decentralized technology allows the creation of a distributed ledger, which records all the transactions within the application. This ledger cannot be altered or tampered with; this makes it impossible for data to be manipulated. Smart Contracts power DApps.

DApp Architecture

DApp architecture is different from Web 2 (traditional) applications. For Web 2 applications, its architecture is designed as follows;

A central place for data storage that is constantly updated, the backend code, usually written in Python, Node.js, or Java, defines the business logic of the application. Finally, the front-end is written in JavaScript, HTML, and CSS. The front end defines how the site or application looks.

For DApps architecture they are built on a decentralized state machine maintained by nodes on the internet. Therefore, the state of the DApp is maintained across the nodes; the backend end, which defines the application's business logic, is called Smart Contracts, which can be written in Solidity, Go, Rust, Python, etc., and are deployed into the decentralized state machines. Therefore, if you want to build a DApp on Ethereum, you deploy your Smart Contract into the Ethereum Virtual Machine (EVM). Lastly, the front-end is built using the same tools as Web 2 but has its own library Web.js.

What are Smart Contracts?

Smart Contracts are self-executing contracts that contain the terms of an agreement between two or more parties. These contracts are automatically executed when certain conditions are met, meaning that users trust that the contract will be enforced without any third-party intervention. They allow for the automation of contract executions, reducing the need for intermediaries and increasing efficiency.

An example of a simple, smart contract written in Solidity, a programming language used for writing smart contracts on the Ethereum blockchain:

    pragma solidity ^0.8.17;

contract SimpleContract {
        uint public value;

        constructor() public {
                value = 0;
        }

        function setValue(uint _value) public {
                value = _value;
        }

        function getValue() public view returns (uint) {
              return value;
        }
}

From the above example, the smart contract defines a public variable called value And two functions: setValue and getValue. The setValue function allows a user to set the value of the value variable, while the getValue function allows a user to retrieve the current value of the value variable.

Smart contracts can do more complex things and encode many agreements and business logic. For example, it could be used to automate the process of buying and selling a house, with the terms of the sale being encoded in the contract and the execution of the contract being triggered by specific events, such as the transfer of funds.

How do we store all the data being collected in our application?

Decentralized Storage

One of the main characteristics of dApps is that they store data in a decentralized manner, meaning the data is not stored in a central location but distributed across the network. This provides the ability for data to be tamper-proof.

They are various ways that dApps can store data; this depends on the blockchain or decentralized network they are running on. Some popular choices include the following:

  1. Storing data on the blockchain itself: Data is stored directly on the blockchain as part of a transaction or block. This is distributed across the network and can be verified by all participants.

  2. Storing data Off-chain: DApps can store data off the blockchain using a decentralized storage solution such as InterPlanetary File System (IPFS). This is a more efficient way of storing large amounts of data, as it reduces the burden on the blockchain itself.

  3. Storing data on a decentralized database: Decentralized databases such as BigchainDB or Ocean Protocol can be used to store data. These databases are decentralized and are not controlled by a single entity.

Let's build a simple Wallet dApp on the Ethereum blockchain.

Build and Deploy an Ethereum Wallet using Remix IDE

To build a decentralized application, one must first choose the blockchain network to build on. Various blockchain networks are available, each with its features and capabilities. For our wallet, we would build on the Ethereum network using Remix IDE to develop and deploy the wallet without installing the necessary libraries on our local computers.

Remix IDE is a web-based platform that allows users to write, deploy and debug smart contracts on the Ethereum blockchain. The IDE is user-friendly and straightforward, adequate for those new to blockchain. You can find it here.

remix ide- Remix IDE Documentation

We can create a new file by clicking New File, and we will name it EtherWallet. The EtherWallet file is where we would be writing our Solidity code.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;

contract EtherWallet {
    address payable public owner;

    constructor() {
        owner = payable(msg.sender);
    }

    receive() external payable {}

    function withdraw(uint _amount) external {
        require(msg.sender == owner, "caller is not owner");
        payable(msg.sender).transfer(_amount);
    }

    function getBalance() external view returns (uint) {
        return address(this).balance;
    }
}

The smart contract defines a public variable called owner and two functions: withdraw and getBalance. The withdraw function allows only the user to remove/transfer from the wallet, and if the sender is not the same as the owner, it returns an error Caller is not owner , but if the sender is the owner of the wallet, the user can successfully transfer the amount to the address, while the getBalance function allows the owner to retrieve the current amount in the wallet.

Deploy Smart Contract

Before Deploying any Smart Contract, it is essential that you first deploy in a false blockchain environment. This helps us test our contract code, ensure it is functioning as expected, and its security. Also, deploying to a real blockchain requires us to pay gas fees; we can avoid the cost of these transactions while testing. Let's deploy our contract.

On the Remix IDE, go to Deploy and Run transactions. Click on the Remix VM (London) environment; this is used to connect to a sandbox blockchain.

Next, we choose the Ether account we want to use to deploy the contract (Note: Deploying a smart contract is also seen as a transaction. Therefore, we need an Ether address for that).

Finally, we choose the contract we want to deploy. The deployed contract will be indicated at the bottom; if you deploy more than once, the contract's address will change.

Interact with Deployed Smart Contract

We can interact with contracts from the Remix IDE and play around with what we have built.

The withdraw button allows the user to input how much he wants to get out of the wallet, the getBalance button informs us how much is currently in the account, and lastly, the owner button displays the wallet address.

Conclusion

In conclusion, decentralized applications (DApps) have the potential to revolutionize the way we interact with the internet and with each other. DApps offer increased security, transparency, and autonomy using decentralized networks and blockchain technology compared to traditional centralized applications.

However, it's important to note that DApps are still in their early stages of development and adoption. As with any new technology, challenges and limitations must be addressed, including scalability, user experience, and regulatory compliance.

Despite these challenges, the potential benefits of DApps make them worth exploring for individuals and organizations looking to leverage the power of decentralization. Whether in finance, governance, or any other industry, DApps can transform how we do business and interact with the world around us.

I would love to connect with you on LinkedIn | Twitter | Github | Portfolio