# blog.md · GitHub

April 2, 2020DApps

# DeFi (Decentralized Finance) Cheatsheet

If you are like me, you have a passing familiarity with DeFi but the names of all the projects (especially the smaller ones) start to bleed together and it is easy to mix up what they all do. In this cheatsheet, we describe several underlying primitives of DeFi. While real world DeFi projects offer complex, full-fledged services, for the cheatsheet we describe a simple version of (a.s.v.o) a specific DeFi service (some projects provide multiple services).

We illustrate the examples using WETH (a wrapped version of ETH, the native currency of Ethereum, where wrapping it just allows interoperability with a wide variety of services) and WBTC (a wrapped version of BTC, the native currency of Bitcoin swapped onto Ethereum's blockchain). We assume all DApps can hold WETH and WBTC and can issue tokens.

#### Autonomous market maker (a.s.v.o. Uniswap)

Working Example: Alice creates a DApp and deposits 1000 WETH and 10 WBTC (assume 100 WETH = 1 WBTC, so the value of each deposit is the same). The DApp allows anyone to add WETH by removing WBTC; or to remove WETH by adding WBTC. A public mathematical relationship sets the amount of one token that can be removed for a deposit of the other (if one pile of tokens becomes smaller and smaller, a larger (and eventually infinite) amount of the other token must be added to further reduce the pile). Alice earns a transaction fee on each transaction. Carol can add WETH and WBTC (in the same proportion as the current pile of each token) as an investment to earn a cut of the transaction fees Alice is making. Later Carol can remove an equivalent value of WETH and WBTC tokens but in proportion to the WETH and WBTC in the DApp.

Mathematical Relationship: Call the current amount of WETH in the DApp $m$ and the current amount of WBTC $n$. Bob deposits $x$ WETH and pays a fee in order to withdraw $y$ WBTC. After the transaction, the DApp will hold more WETH ($m+x$) and less WBTC ( $n-y$ ). Given ${m,n,x}$, the value of $y$ is set such that $m\cdot n=(m+x)\cdot(n-y)$. Bob will compute $y$ ahead of time and decide if $y$ WBTC is valuable enough to trade $x$ WETH for.

Again, call the current amount of WETH in the DApp $m$ and the current amount of WBTC $n$. To invest in the market maker, Carol can add $a$ WETH and $b$ WBTC such that $m/n=a/b$ . Later, the DApp holds $m'$ WETH and $n'$ WBTC. If Carol withdraws her investment, she receives $ab$ tokens: $a'$ WETH and and $b'$ WBTC such that $a'b'=ab$ and $m'/n'=a'/b'$.

#### Routing protocol (a.s.v.o. Kyber)

Working Example: Alice sets up a DApp that can route trades to other DApp-based markets, such as autonomous market makers and decentralized exchanges. If Bob's DApp wants to trade 1000 WETH for WBTC, Alice's DApp will stage the exchange with all the markets, determine which can handle the volume of 1000 WETH and which will offer the best overall price. Additionally it might split the order between multiple markets, possibly swapping WETH for a third token on one exchange and the third token for WBTC on a different exchange. Additionally, it allows Bob to convert WETH into a basket of other tokens (in a specified proportions).

## (2) Lending and Margin

#### Facilitated lending (a.s.v.o. 0x, Dharma)

Working Example: Alice sets up a DApp to manage loans. Bob wants a loan of 100 WETH. He posts to the DApp the conditions of the loan: the amount he wants to borrow, interest he will pay, the repayment schedule, how much collateral he will post in WBTC (for example, 1.5 WBTC assuming 1 WBTC = 100 WETH), and the consequences of a missed payment or drop in the value of the collateral. Carol sees Bob's post, agrees to fund the loan, and deposits 100 WETH in the DApp. The DApp sends the WETH to Bob, holds the WBTC in collateral, and issues to Carol a transferrable token. The holder of the token, initially Carol, is the recipient of Bob's repayments, but later Carol might sell this token to David who then begins receiving Bob's repayments. If Bob fails to repay or the value of WBTC (the collateral) falls relative to the lent WETH, the collateral may be partially or fully sold by the DApp (e.g., through the routing protocol above) according to the conditions of the loan.

#### Money market account (a.s.v.o. Compound, Fulcrum, Aave)

Working Example: Alice sets up a DApp to collect a pool of tokens for lending. Bob deposits 100 WETH into the DApp and receives 100 dWETH tokens that represent his deposit. Bob transfers these to Carol, and Carol redeems the 100 dWETH tokens for 100 WETH from the DApp plus a small interest payment in WETH.

Next, David wants to borrow 100 WETH from the DApp. Instead of the DApp attempting to evaluate how risky of a borrower David is, it only offers the loan if David can provide 150 WETH worth of another token it supports (called a non-recourse loan). In other words, David's collateral (in this case, WBTC) must be worth 1.5x the amount being borrowed. David deposits 1.5 WBTC (assume 1 WBTC = 100 WETH, so this is worth 150 WETH) and receives 1.5 dWBTC. Assuming the DApp has unused deposits from others adding up to at least 100 WETH, the DApp exchanges 100 dWETH with David for 1.5 dWBTC. Later, David repays 100 dWETH and a large interest payment in dWETH and he receives 1.5 dWBTC.

The interest rates are set by a public mathematical relationship in DApp between the supply of assets for lending and the demand for loans.

Failure/Failsafe Mechanism: If the WBTC/WETH exchange rate changes enough that the collateral (1.5 WBTC) is approaching the same value as the loan amount (100 WETH), David is given a timeframe to repay the loan or to add additional collateral. If David does not respond, the DApp will auction the collateral for its value in WETH (acting quick enough to ensure it receives at least 100 WETH for it), and David's loan is canceled (he keeps the 100 WETH he borrowed, loses the 1.5 WBTC he deposited, and the DApp keeps the 100+ WETH from the auction). If the DApp receives less than 100 WETH for the 1.5 WBTC, then the loss is passed onto the holders of dWETH, such that each dWETH receives: 1 WETH + small interest payment - (total loss in WETH)/(amount of dWETH).

Failure/Failsafe Mechanism: If a user attempts to redeem dWETH tokens for WETH but there is not enough WETH in the DApp, because it has been lent out, the user will be unable to redeem tokens until either loans are repaid or more users deposit WETH. The interest rates will be adjusted to incentive both. When deposits are unavailable for redemption, the account is said to be illiquid. Note it is not insolvent --- the DApp holds the value of the deposited WETH in WBTC which is serving as collateral to the loans

#### Flash loan (a.s.v.o. Aave, DyDx)

Working Example: Alice sets up a DApp to collect a pool of tokens for "flash" lending. Alice deposits a large amount of WETH in the DApp. Bob wants to borrow the WETH from Alice's DApp for a single transaction. Bob sets up his own DApp (call it DApp2) with a special borrowing function that can only be called by Alice's DApp (call it DApp1), that specifies the useful transactions to conduct using the WETH that is to be borrowed, and that returns the borrowed WETH (plus a fee) to DApp1. To initiate the loan, DApp2 calls DApp1 and DApp1 conducts the following sub-transactions: (1) DApp1 transfers the WETH to DApp2, (2) DApp1 calls DApp2's function and allows it to run to completion, (3) DApp1 checks if DApp2 has transferred back the same amount of WETH (plus a fee), (4) if and only if no WETH was returned in step 3 or the amount is less than what is required, DApp1 reverts the entire transaction. Note that reversion means all sub-transactions (1-4) are discarded and the state of DApp 1 (and DApp2 and any other DApps touched by sub-transaction (3)) go back to the state they were in before the loan was initiated. The only change to the state of the world is that DApp2 loses the WETH (in gas) required to pay for all the computation used in trying to execute each sub-transaction.

#### Margin trading (a.s.v.o. bZx, DyDx)

Failure/Failsafe Mechanism: If the WBTC/WETH exchange rate changes enough that Bob's losses approach his 1000 WETH deposit, Bob is given a timeframe to reverse the trade (trade the 50 WBTC back into WETH) or to add additional WETH to his deposit. If Bob does not respond, Alice can request the DApp reverses the trade. The price drops faster than Alice can respond and Bob's losses exceed 1000 WETH, Alice receives less than the lent 4000 WETH back.

## (3) Stablecoins and Derivatives

#### Indirectly-backed stablecoins (a.s.v.o. Dai)

Working Example: Alice sets up a DApp that can hold WETH and issue tokens. The DApp determines how much WETH is equivalent to $1.50 USD using the current exchange rate, provided to the DApp by a trusted third party oracle, and Alice deposits this amount of WETH into the DApp. The DApp issues to Alice two places in a line --- each place is a transferrable token. At some future time, the holder of the first place in line can redeem up to$1.00 USD worth of the deposited WETH at the going exchange rate, and the holder of the second place in line gets any remaining WETH. Alice will transfer the first place in line (as a stable coin called AliceCoin) to Bob for $1.00 USD, and will hold or sell the second place in line. When Bob redeems the AliceCoin, it will be worth$1 USD in WETH when the entire deposit of WETH is worth more than $1 USD. If the exchange rate drops enough, the entire deposit will be worth less than$1 USD --- Bob will get all of the deposit and the holder of the second place in line will get nothing.

#### Inverse ETH (a.s.v.o. Synthetix)

Working Example: Alice sets up a DApp that can hold and send a stablecoin (USDC). The DApp determines how much USDC is equivalent to 1 WETH (assume 1 WETH = 100 USDC) using the current exchange rate, provided to the DApp by a trusted third party oracle. Alice deposits twice this amount (200 USDC) into the DApp. The DApp issues to Alice two places in a line --- each place is a transferrable token. At some future time, the holder of the first place in line can redeem 1 WETH worth of the deposited USDC at the going exchange rate, and the holder of the second place in line gets any remaining USDC. The second place in line is an "inverse ETH" that increases in value when the value of ETH decreases. For example, assume Bob buys the first place in line for 100 USDC and Carol buys the second place for 100 USD. If ETH drops to 75 USDC, Alice loses 25 USD (the first place receives 75 USDC) and Carol makes 25 USD (the second place receives 125 USDC).

If you are like me, you have a passing familiarity with DeFi but the names of all the projects (especially the smaller ones) start to […]