A Bird's-eye View of Decentralized Finance - (DeFi)

An introduction to Decentralized Finance - (DeFi) and its main components.

A Bird's-eye View of Decentralized Finance - (DeFi)

Introduction

This article is part of DeFi Talents mentorship program as an assignment required to graduate from the program. In the article, I'll discuss a general overview of the DeFi ecosystem.

What is Decentralized Finance (DeFi)?

Before we dive deeper into the building blocks of the DeFi ecosystem, let us first get an overview of some essential concepts and general ideas here

A short history of money and value transfer

Why does finance even matter? And why we should care about "money" and "value"? to answer those questions we need to first know what they mean. Money, - whether it is cash or digital - in its current form is the biggest invention of humans in finance. In the beginning, people used to trade without money but used other goods in exchange for goods they need. And after some time, someone invented gold as a medium of exchange for those same goods and services. But gold has some problems when used to exchange value, it can't be divided into so many smaller pieces to represent much smaller amounts, and it isn't easy to carry, among many others, but those are the ones we care about here. After that, someone said, why can't we just hold that gold in a place and give people a "bond" that represents their "ownership" of the specified amount of gold, and let people exchange that bond among themselves instead of carrying gold all around. And that was the first representation of cash money in history. After that came the invention of banks as a way to protect people's money and let them earn some interest on top of it by using this money to fund (lend) other projects. And that was the true beginning of massively allowing people to transfer value among each other without the need to carry tons of gold and thinking of the security of it and the whole process.

Definition of DeFi

So, how DeFi is related to all the above? Well, as we will see later in the article, DeFi as a concept won't be possible without the invention of Blockchain technology. We can define Decentralized Finance in simple English words as a way to perform traditional finance but in a decentralized way with no intermediaries between a protocol/project and the end user. Also, automation and compossibility are two of its main features as we will find out later.

How it works

This image below (credits to Wikipedia) shows in a very short and visualized manner how DeFi works. The_DeFi_Stack_Figure.jpg Decentralized finance is just a fancy word for how many financial components/products are composed and built on top of each other in a permissionless environment. This environment is built on top of the blockchain. See? everything is built on top of something else or by using some other project or protocol - compossibility at its finest.

Problems solved by DeFi

Ok, that's nice, but why didn't traditional financial institutions just be built the same way and everyone will be just happy? Because financial institutions are way multi-layered and there are way too many intermediaries in the middle of everything. For example, a simple traditional bank transfer can take between 3 working days to a week in normal cases for the transfer to arrive at its destination safely. But, imagine that, if somehow we can represent traditional monetary values into a digital form that can be exchanged between parties with no middle man - except for a small tiny program - and the transfer can take between just one second up to 10 minutes trusting nobody and with certain finality of the transaction, Isn't that great? Yea, that's the beauty of blockchain and DeFi.

Decentralized Finance Infrastructure and Building Blocks

To better understand how DeFi works, we need to first understand how the underlying technologies that power the DeFi ecosystem work first. Mainly, it comprises the following technologies - Blockchain, Cryptocurrencies & Wallets, Smart Contracts, Tokens, Oracles, and dApps (UI/IX)

Blockchain

DLTdiagramV2.png Blockchain is a distributed ledger and database that is used to record transactions and data in a trustless and permissionless manner. The main difference between a normal database and a blockchain network is how each of them stores its data, in normal databases, we store data in tables with unique keys, to every record, but in the blockchain, world data is stored in blocks of transactions. These transactions can be anything from transferring value between two parties in a trustless way to storing a digital representation of ownership on the blockchain (which is known for short as tokenization).

CryptoCurrencies / Wallets

Cryptocurrencies are just the native money of a blockchain network, this "currency" is used to incentive certain people on the network called miners in a Proof-Of-Work blockchain (also called validators in a Proof-Of-Stake blockchain network) to record the transactions we talked about earlier in the blockchain so it'll be available for anybody to verify and validate its authenticity. They are also used as a way for end users to pay those miners/validators to include their transactions in the public network. These currencies can also represent monetary units when used as a medium of exchange of value between two or multiple parties (for example, someone may want to receive his/her transfer in ETH - the native currency of the Ethereum network - rather than USD). Ok, but where can we find and store these cryptic currencies, here comes the job of a digital wallet built for this specific reason, to hold the currency of a public blockchain network in a safe and easy-to-use way after we can easily buy them from an exchange (you can also win them by mining the public network and be part of the network as a miner yourself, but that's for another discussion).

Smart Contracts

Another building block of the DeFi ecosystem is Smart Contracts, which are the small tiny programs we touched upon earlier in our discussion, those tiny apps can make money programmable. When we represent monetary value on the blockchain using cryptocurrencies (or tokens, as we will discuss in the next section), we then can use smart contracts to create sci-fi-level scenarios of how this money can be used. For example, imagine if you want to create your own piggy bank and want it to be unlocked only at a certain point in time and won't allow you to withdraw whatever is in it, this can be done with a simple smart contract code on the blockchain, and this promise can't be broken (most of the time, unless there is a bug in the program's logic). This is just a very, very simple use case of the power a smart contract can have.

Tokens (Fungible and Non-Fungible)

Tokens in the Ethereum blockchain are just a set of rules that can be implemented using a smart contract, using a set of rules among all tokens will allow compatibility among different protocols and projects to use tokens as a monetary unit or a transfer of value. The main token types are fungible and non-fungible tokens. Fungible tokens are similar tokens that have the same features among them, and each token transferred from one user to another holds the same value. On the contrary, non-fungible tokens are one of a kind that represents ownership of something - a very popular use case is the NFT mania of digital art.

Oracles

One big drawback of using major blockchain technologies is the lack of communication to the internet or any other data source for example. The Ethereum network, for example, can't by default connect to the internet to grab the latest price of a stock and use that in a smart contract. Here comes the power of the Oracles, which are small programs that are built in a special way that allows other smart contracts to communicate with their smart contracts to grab specific data points from off-chain sources. A popular example of an oracle network is Chainlink

dApps

Communicating directly with smart contracts can only be for the tech-savvy, so there should be a familiar way for users to use the blockchain and communicate easily with smart contracts without the need to invest so much time learning new technologies. And here comes the concept of dApps (which is an abbreviation for Decentralized Applications) which are normal web apps that use special web components to facilitate the communication process with the end user and the blockchain network. dApps aren't a set of rules that are shared between every dApp, no, they're just a concept with a set of tools and technologies that can be used. dApps play an important role here, because, without an easy way to access applications built on top of the blockchain by end users, there'll be no mass adoption of the technology.

Decentralized Stablecoins

Let us here talk about one of the main building blocks (right now) of a decentralized and trustless environment like the blockchain

What are Stablecoins and why do we use them?

Cryptocurrencies can be very volatile all the time, and can't be easily used as a store of value without an excessive knowledge of how they work and the economics behind them, and a lot of other areas, still one can't be sure of how that can be used to store the value of a crypto asset. Here comes the introduction of stable-coins, and as the name implies, it's used to stabilize something, this "something" here is the volatility of cryptocurrencies. Stablecoins are just small smart contract applications that are being built on top of a public blockchain as tokens.

How Stablecoins works

In simple terms, a stablecoins is just a representation of a real-world currency like USD for example, to be used on-chain as a medium of exchange. There are four main types of stablecoins that are used today:

  1. Fiat-backed stablecoin: This is the most used type, and how it works is simple, you just buy a certain amount of dollars, for example, by depositing the dollars into the issuing company bank account and in return, they issue (mint) that same amount on-chain for you to be used as digital dollars (minus the fees), and this is how they maintain the peg of 1-to-1 USD to token ratio all the time. At any point in time, there should be one dollar in a bank account representing each token minted on-chain. One major disadvantage of this type is that they are very centralized. Major examples of this type are, USDC of Circle, USDT of Tether, and BUSD of Binance.

  2. Crypto-collateralized stablecoin: In this type, a peg of 1-to-1 USD to token ratio is maintained by over-collateralization of a cryptocurrency in the vault of a protocol. For example, to issue (mint) a representation of a 1 dollar token on-chain, a protocol asks for the deposit of a 150% equivalent amount in a specific cryptocurrency. Why is that? To make it safe for the protocol itself if the price of the cryptocurrency falls behind the price at the time of deposit, the protocol will sell the cryptocurrency at a small discount to encourage liquidity and to keep the peg to the dollar as a 1-to-1 maintained all the time. A major example of this type is DAI of MakerDAO.

  3. Algorithmic stablecoins: This is the most interesting type of stablecoins, in this type the dollar peg is maintained purely by the supply and demand mechanics of the market by utilizing mathematical equations that rule this peg. A major example of algo-stables is FRAX of FraxFinance

  4. Commodity-backed stablecoin: Similar to the fiat-backed stablecoin, this type is only backed by some kind of a commodity like gold, silver, oil, etc., Major examples of this type are, PAXG Paxos and XAUt of Tether.

Decentralized Exchanges (DEXs) in DeFi

Decentralized_exchange.jpg Another major component of DeFi right now is Decentralized Exchanges (DEXs) which are being used to exchange tokens and currencies on-chain without the need for an off-chain order book to record transactions

What is a decentralized exchange (DEX)?

Well, with the introduction of the fungible tokens standard (The ERC20 standard on the Ethereum network) a flood of projects started to use the concept to build their own token on top of the Ethereum network instead of building their own network, which is a good thing. But, after some time, a torrent of new tokens is being released every single day. And for these token users' to buy them, the token project itself should apply for listing in a centralized exchange platform, which will get paid a huge amount of money to agree to list the token, and it hugely depends on how large and how much liquidity are there in that exchange. So, a brilliant idea came out, why don't we just let the token owners decide on their prices and provide their own liquidity in a trustless and decentralized way? And that's when the idea and concept of a decentralized exchange came out.

How a decentralized exchange (DEX) works

There are two main types of DEXs - Automated Market Makers (AMM) and Decentralized Order Books

  1. AMM: This is the most used type of DEXs and it comprises over one type, or we can say mechanism of execution. What an AMM is doing is basically adjusting the price of a token or an asset according to the supply and demand for it automatically in a trustless and decentralized way based purely on mathematical equations. The main models of those mathematical equations and hence models of AMMs are - Constant Product Market Maker (CPMM), Constant Sum Market Maker (CSMM), and Constant Mean Market Maker (CMMM). Examples of an AMM DEX are, Uniswap and SushiSwap among many others.

  2. Decentralized Order Books: Order books are most used in traditional finance daily, it's the most common way of providing liquidity in markets. An order book is basically a matching engine, that matches buy orders with their respective sell orders from different buyers and sellers. The major drawback here is its dependency on a central party for matching buying and selling orders, but with blockchain, we can do it in a more decentralized way. Major examples of decentralized order books are, 0x, dYdX, Loopring DEX, and Serum.

Lending and Borrowing in DeFi

Like a normal financial system, there should be a way to maximize the use of idle monetary value by depositing it into interest-bearing accounts. Lending and borrowing in DeFi is no different.

What is decentralized lending and borrowing?

A lot of platforms arise to use the idle cryptocurrencies and tokens held by blockchain and DeFi users, bearing them interest over time when locking their currencies and tokens with the platform (staking)

How lending and borrowing in DeFi works

Users who want to lend to other users make a deposit of the cryptocurrency they want to lend in a DeFi lending protocol managed by a smart contract in a special vault for this specific cryptocurrency, in exchange, the lender gets a virtual token representing their ownership of the lent amount plus the interest earned accrued over time, and it can be redeemed at any point in time (the principal + the interest, and the protocol will destroy that virtual token after that). Users who want to borrow go to the platform and deposit another token or cryptocurrency in exchange for a specific token or currency for a specific amount of time, the borrower usually over-deposits the initial cryptocurrency to be able to get the loan (which we discussed earlier and know as over-collateralization). Examples of these lending and borrowing platforms are, Aave and Compound Finance.

Conclusion

We just touched upon the major building blocks of DeFi, it has way more than that, but we should always start with the basics.