Deconstructing DeFi
Tags: blockchain
Published: 2022-07-27 20:33:28

# Introduction

If you clicked on this blog post I'm going to make two assumptions about you, my wonderful reader:

1. You know what a cryptocurrency is, at least at a basic level.
2. You may have heard the term "Defi" but don't really know what it means.

If these assumptions are incorrect, this may be a difficult read. Even if you consider yourself crypto-savvy, and maybe even have money in cryptocurrency, Defi is a complex topic. It is an increasingly important one, however, as investors and investment media have begun talking about the possibility of a ["crypto winter"](https://www.forbes.com/sites/jonathanponciano/2022/07/07/crypto-winter-watch-all-the-big-layoffs-record-withdrawals-and-bankruptcies-sparked-by-the-2-trillion-crash/?sh=2f02e7cd7b44) where trillions of dollars in value could be lost. I'm not a market forecaster or crypto investor, but I am a technically savvy observer and an experimenter with blockchain-based application development. And from a technical perspective cryptocurrency, blockchains and smart contracts are fascinating. Most of the applications built on top of a blockchain are in the Decentralized Finance space, which I will define below. But first I need to lay down some more fundamental concepts.

## Blockchain (The Relevant Bits)

When I say that an application or program is built "on a blockchain", what I mean is that the blockchain acts as the _data layer_ for the application. The simplest way to think of a blockchain is as a write-only database that uses crypto tricks to allow it to be decentralized. A decentralized network is like a cluster of servers where each server is fully redundant - so long as one is still operating the network is slow but technically up, and more servers can be added by anyone because the cryptography enforces the integrity of the database.

## Smart Contracts

A smart contract is a collection of code that runs on a blockchain-based protocol structured to enable code execution. The most popular example is the [Ethereum](https://ethereum.org/en/) blockchain, but other smart-contract enabled blockchains include [Polkadot](https://polkadot.network), [Solana](https://solana.com), and [TRON](https://tron.network). The programs that run on a smart contract can contain essentially arbitrary code, like any computer program. There are limitations, however. There are fees associated with any type of data storage and updates. The contract cannot independently communicate with any data that doesn't have an address on its blockchain. Developers get around this latter limitation by running servers that continually update or sync the contract with external data sources, known as [oracles](https://en.wikipedia.org/wiki/Blockchain_oracle).

## Tokens

A token is a cryptocurrency that doesn't use its own blockchain. Instead, it is launched on a smart contract-capable blockchain as its own smart contract. That contract tracks the total supply of the token, the balances of each user or wallet that holds that token, and manages transfers of the token between users. So long as the token's contract correctly implements a standard specification (Ethereum has the [best documented examples](https://ethereum.org/en/developers/docs/standards/tokens/)), other smart contracts can execute code that interacts with those tokens.

NFTs, which you may have [heard about](https://news.google.com/search?q=nft&hl=en-US&gl=US&ceid=US%3Aen), are an example of a token. What distinguishes a non-fungible token is simply that there is only one of them, so only one person can own it. This can be used to represent something like a deed for artwork, or it can be used to grant special permissions to the holder when accessing another smart contract.

Other tokens represent stake in a DAO, or [Decentralized Autonomous Organization](https://en.wikipedia.org/wiki/Decentralized_autonomous_organization). Such "organizations" are another smart contract, this one controlling a pool of funds or some other on-chain resource like another smart contract. Holders of DAO tokens are able to vote through the DAO's contract on proposals for what to do with the managed resource. As an example, a DAO was created in a recent attempt to [purchase an orginal copy of the U.S. Constitution](https://www.theverge.com/22820563/constitution-meme-47-million-crypto-crowdfunding-blockchain-ethereum-constitution).

Another type of token is a "wrapped" token. The idea is that since one blockchain cannot communicate directly with another, a developer can create a token that represents the value of one blockchain's currency on a second blockchain. An example of this is [wBTC](https://wbtc.network), or Wrapped Bitcoin. The company that owns the wBTC token's smart contract will accept your Bitcoin and give you a wrapped version of it in the form of an Ethereum token. This token can then be exchanged, again through the smart contract owner, for actual Bitcoin, therefore theoretically keeping the price of wBTC and BTC in sync.

A final example of a token is a "stablecoin". A stablecoin is a token which is kept by the issuer at a fixed price in terms of a government currency, generally the US dollar. [Tether](https://tether.to/en/) maintains a popular stablecoin known as USDT by keeping reserves matching the supply of the token. Other approaches include that used by [MakerDao](https://makerdao.com/en/) for its stablecoin Dai, where reserves are decentralized and take the form of tokens or Ethereum. These decentralized stablecoins are referred to as "algorithmic stablecoins". A more unfortunate example is [Terra](https://www.terra.money), who's algorithmic stablecoin collapsed to the tune of [60 billion dollars](https://www.bloomberg.com/news/articles/2022-05-19/luna-terra-collapse-reveal-crypto-price-volatility).

## Defi

Once an ecosystem of tokens developed on the smart contract blockchains, the next step was "[Decentralized Finance](https://en.wikipedia.org/wiki/Decentralized_finance)", or "Defi". Token holders want to do stuff with their tokens, mostly buy, sell and trade them. To a large extent, Defi consists of markets such as [Uniswap](https://uniswap.org/) where Ethereum tokens can be quickly exchanged or converted to Ether/ETH. However other financial (or pseudo-financial?) instruments of varying degrees of sophistication and marketing buzz exist. Various kinds of contracts are referred to as "staking" contracts, "liquidity pooling" is used to back decentralized token markets, and "crypto lending" is its own strange world of token economics. Here I'll endeavor to introduce the various uses of these terms and what actually happens to the crypto invested in them.

## Staking

Staking is a term used to refer to many forms of smart contracts, ranging from core to the crypto ecosystem to mathematically suspect gambling mechanisms. The former type of staking involves running a node (a server on a decentralized network) that processes and validates transactions to a blockchain. The "stake" is a smart contract that holds the node operator's cryptocurrency. If the node successfully and accurately validates transactions, the owner will receive rewards from the protocol based on the amount they have staked. Submitting invalid transactions or any other behavior counter to the network's health results in the stake being "slashed", i.e. the owner of the node loses their crypto. This mechanism of validating transactions, called proof-of-stake, replaces the power-intensive and inefficient mechanism of proof-of-work, where a special hash value must be calculated to successfully process a block of transactions. The Ethereum core developers are [working towards a move to proof-of-stake](https://ethmerge.com) and [Algorand](https://www.algorand.com) has been proof-of-stake from its inception.

On the other end of the spectrum we have token "staking" contracts that work entirely on top of the native blockchain protocol, with no "built in" source of returns. These contracts can have wildly varying implementations, there being no fixed definition for "staking" in crypto marketing, but the simplest example comes from "Meme Token" [ShibaInu](https://www.shibatoken.com). The anonymous developers recently rolled out [ShibaSwap](https://shibaswap.com/#/), where users can place their SHIB tokens in [this](https://etherscan.io/address/0xb4a81261b16b92af0b9f7c4a83f1e885132d81e4#code) smart contract. This contract performs a simple operation - every time tokens are deposited, the user is given "xSHIB", which can be returned to the contract in exchange for a percentage of the total SHIB tokens that have been deposited. If you put in 10 tokens when the pool contains 100, you'll receive enough xSHIB to retreive your 10 tokens back. If someone else comes along and deposits 10 tokens, they'll receive enough xSHIB to get their tokens back as well. But since the pool now contains 120 tokens, if you "burn" your xSHIB and withdraw your stake, you'll receive about 10.9 tokens. The sustainability of this staking mechanism is left as an exercise to the reader.

## Liquidity Pooling

Here we get to the core of Defi. Liquidity pools are smart contracts that allow automatic exchanges between two tokens or between a token and its native cryptocurrency. A liquidity pool has two types of users, with the first and most important being [liquidity providers](https://help.uniswap.org/en/collections/3033939-provide-liquidity). These users commit a balance of two tokens or a token and the native crypto and specify a range of prices at which they would swap between the pairs, in exchange for a percentage fee. This is yet another transaction that may be referred to as "staking". Other users can then access the smart contract through an app to [swap](https://help.uniswap.org/en/collections/3033938-swap) between the tokens.

If the token's price as determined by the smart contract exceeds the bounds a liquidity provider has set, instead of having a balance of two tokens the user's balance will be concentrated entirely in one token. This will _always_ be the less valuable token, regardless of whether the price is above or below the provided range. In order to cash out their position after the price leaves their range, a user must sell a token that by the logic of the contract is already declining in value. Users are thus incentivized, in theory, to provide accurate price ranges. These contracts are then advertised as [oracles](https://docs.uniswap.org/protocol/V2/concepts/core-concepts/oracles) which provide theoretically accurate price information to other smart contracts.

[Uniswap](https://uniswap.org), whose help articles I've linked above, is the most prominent and best-documented market for tokens based on liquidity pools. Other markets of varying degrees of credibility exist as well. Though the nature of smart contracts makes them theoretically open to analysis by anyone, in practice a smart contract audit is a highly technical skill beyond most users. [Rug pulls](https://coinscreed.com/defi-top-10-list-of-crypto-rug-pulls.html) are unfortunately common enough to be a real concern.

## Crypto Lending

Our final, and perhaps strangest, Defi contract is the crypto lending contract. In a crypto lending contract one user, the lender, deposits a token into the contract. Other users can deposit collateral in the form of native cryptocurrency or another accepted token, and in exchange withdraw the token deposited by the first user. A crypto loan differs from a swap through a liquidity pool in that the second user can then _get their collateral back_ by depositing the borrowed token plus interest paid in that same token. Crypto lenders, therefore, earn a fee when the loans are paid back, yet another form of a transaction referred to as "staking".

The interesting thing is that the interest rates and the amount of collateral required at first don't make sense. The [loan to value ratio](https://docs.aave.com/risk/asset-risk/risk-parameters#loan-to-value) on [AAVE](https://aave.com/), a crypto lending platform/algorithmic stablecoin, is below 100%. This means that if you deposit $100 worth of ETH as collateral, you are then loaned _less_ than $100 in the borrowed token. Why bother? Because it allows crypto investors and speculators access to leverage. The way AAVE works is this: first, the user borrows a token, usually a stablecoin, in exchange for depositing ETH as collateral. The user then sells that stablecoin for more ETH, and (if the restrictions of the contract allow) repeat the cycle until they have a bunch of ETH in the contract _and_ ETH in their wallet, but none of the stablecoin token they borrowed. If the value of the borrowed token falls relative to ETH, that user will be able to sell their ETH for enough of the token to cover the loan and interest, making more money than they would just by holding ETH.

But since the borrowed tokens are stablecoins, this can happen in one of two ways. One is that the price of ETH in a government currency goes up. The other is that the stablecoin suffers a collapse, or "depeg," as [happened](https://www.gfinityesports.com/cryptocurrency/luna-crypto-crash/) to the company Terra and their cryptocurrency Luna. Crypto lending functions like a leveraged short, with the long side of the option held by users who believe themselves to be earning a simple percentage return on their tokens.

# The Unknown Future

As I said in the introduction, I'm not an economist or a finance expert. I don't have money invested in cryptocurrency. I haven't even been able to find the amount of money locked in these Defi smart contracts, so I can't speculate too much as to its impact on crypto prices more broadly. But I feel the need to at least say that nothing I've seen in Decentralized Finance implies that it is any more trustworthy than the "traditional" kind of financial instrument, and that's said as someone who was job hunting in 2008. Someday, I think, there will be more done with blockchain development than financial instruments, and there are a few projects that I actually quite like. What we have now makes me worried for the future, but I encourage everyone to make an informed judgment.

_Credit where credit is due: There is an open issue related to author re-assignment on this platform. This post was written by the one and only @jackvcurtis, and published by the Content Manager at The Gnar Company._

_Learn more about how The Gnar [builds software](https://www.thegnar.com/software-development)._