Ludovic Frank - Freelance developer

How do you create a decentralized application on blockchain? Let's talk about smart contracts!

ionicons-v5-k Ludovic Frank Mar 15, 2022
69 reads Level: Confirmed

Hello?

How are you?
Once again this week, we're going to talk about blockchain, but this time from a more dev point of view than the other times.
Did you know that it's possible to develop on blockchain?
Let's talk about it.

Is a decentralized application (dApp) completely decentralized?

As I write these lines, there are points of centralization in these applications, in fact it's relatively simple to understand "why? "These dApps run in a browser, but a browser only recognizes a few protocols, such as

  • HTTP(s)
  • WebSocke(s)
  • FTP
  • WebRTC
  • Etc ...

What these protocols have in common is that, by their very nature, they are "client/server" and therefore centralized. Today, a browser alone cannot communicate with the blockchain.

How can this become completely decentralized?

Edit: Here I don't mean that Ledger Live is a dApp, but that for some of its functions, this desktop application connects directly to the blockchain, without going through a third-party API (and therefore a third-party server, which can cut off access if it wishes).

Well, today there are already completely decentralized applications, and the Ledger Live (desktop) application comes immediately to mind. This application lets you manage your ledger key wallets from your computer (Windows / macOS / Linux).

It uses "Electron", the framework developed by Github to create desktop apps using Web technologies, and I think we'll be seeing more and more applications like this in the future.

In fact, when you carry out a transaction on Ledger Live, you sign it with your Hardware Wallet, and Ledger live then connects directly to a network node (Bitcoin, for example) to send this transaction.
For the moment, this technique cannot be used in a browser.

Another method of using electron could be : each network node exposes an API accessible via HTTP(s), and the client application (in the browser) has an embedded list of all the addresses and connects randomly to one or other on the network.

Then, in this scenario, there's still one centralization point, the domain name and the server hosting the app (which, for the server, is static content, since it's the JavaScript code that does everything).

What is a smart contract?

The smart contract is the foundation of dApps. In fact, it's nothing more or less than code, but instead of deploying it on your server, you deploy it on the target blockchain (Ethereum, Terra, BNB, Elrond ... etc). This little piece of code will be replicated on all the nodes in the network, since it's now part of the blockchain, linked to an address (yes, the same type of address as your personal wallet?).
Would you like to see what a smart contract looks like? Click here to find out.

This code is the USDT smart contract, so it's actually compiled in the blockchain, here the source code has been submitted for verification, this source code is written with the Solidity programming language.For the remainder of this article, we'll take a look at my favorite blockchain, Elrond.

What can smart contracts be used for?

Here's a small, non-exhaustive list of what a smart contract can do:

  • Creation of automated lending systems
  • Automated decentralized insurance systems
  • Automatic exchange of one asset for another (you give me $PONNEY, I give you $NES)
  • Tokenize real estate: buy a property with several people and automatically pay out rents according to each person's share.

What does it take to create a dApp?

Your dApp will consist of several things:

  • One or more smart contracts on the blockchain (written in Rust at Elrond)
  • An API for accessing the blockchain. As I said earlier, for the moment this is a centralized point, and for this example we'll be using the Elrond api.
  • The front end, what you users see, often written with frameworks like React or Angular (React seems to be the industry standard).

Here, you've probably figured out the centralization point: all it takes is for the api.elrond.com server to go offline, or for the DNS record to disappear. The good news is that it's only the connection between the interface and the back end that's interrupted, while your users' data is still well within the decentralized blockchain.

The smart contract

To write your smart contract on Elrond, you'll need to familiarize yourself with the Rust language. Don't worry, if you're a dev, you'll get the hang of it very quickly .

As with any platform, Elrond comes with an SDK. In fact, they've done it right, with a "Visual studio code" plugin that lets you create an Elrond smart contract IDE in just a few minutes .

However, I advise you to use Linux or macOS, as all the "erdpy" functions don't work on Windows (even with WSL), "edpy" is the command line for interacting with Elrond, written in Python (including py, in fact?).

Here's an example of smart contract code, taken from the Elrond doc, it just pings pongs,
This smart contract is very simple, the user deposits money on it, decides after how long he'll be able to get his money back and then once this period is over he can "claim" his money (get them back).

Yes, I know the smart contract code is scary at first, but look, there are lots of comments and once you get the hang of it, it gets easier.
This smart contract, once compiled, is sent to all the nodes on the network, so yes, your little piece of code will be copied on 3200 machines all over the world... isn't that nice?

Now that we have a smart contract, let's look at the other part, the user interface.

At this point in the article, you're struggling and saying "yes, but there's some information missing here, isn't there? "
Yes, in fact, I'll give you a link at the end if you really want to develop this dApp, but for now I'll explain the concepts of how it works

The front end (the user interface)

Frankly, if you're a front-end developer, there's nothing special here. You're doing call APIs, via the Elrond api... Oh, if there are two or three little details...

There's no "login / pass", as you'd see on a classic application. To authenticate, a user uses a wallet, or a "Maiar", or a ledger key ... and so on.

The idea is that during transactions, the user uses his private key to sign the transaction in order to make it valid; a transaction not signed using the account's private key is simply ignored and invalid...

There you go! Speaking of private keys, another opportunity to talk about my article on ransomware for those who haven't read it.

For that, don't worry, there's of course a JavaScript SDK provided by Elrond, to show you "what it looks like" you can look at the project that goes with the smart contract ping-pong, here

And what about the api?

For the time being, the API is provided by Elrond, and acts as a bridge between your JavaScript application and the blockchain.

I don't yet know how, but personally I'd say that "each network node exposes an API", but I'm not an Elrond dev.

So all you have to do is read the doc available here.

Conclusion

This article was an appetizer for those who want to start writing decentralized applications. Want to go further?

Follow the Elrond starter guide, here.

As for me, all that remains is to wish you a very good week?