Bitcoin Mining Labby Coinovo

Mining Lab · Coinovo Academy · DeFi: financial services made of code

DeFi: financial services made of code

Swap, borrow, lend without a bank

In depth 14 min 7 cards 18 questions

Objectives

What DeFi means

DeFi stands for decentralised finance: swapping, lending and borrowing through smart contracts instead of a bank. Anyone with a wallet can take part, around the clock, without opening an account. The flip side: there is no hotline, no deposit insurance and nobody who can bring back a mistaken transfer.

No middleman also means: nobody to help.

Swapping without an order book

On a classic exchange buyers and sellers meet in an order book. A decentralised exchange, DEX for short, usually works differently: as an automated market maker, AMM for short. A pool holds two tokens, and a contract calculates the price purely from their ratio. It is like a currency exchange booth that sets its rate only from its own cash on hand. The more you take out at once, the worse the price gets.

In the pool, the ratio of quantities sets the price.

Full explanation

The formula behind the pool

The simplest and most widespread rule is x · y = k. x is the amount of one token, y the amount of the other, and their product k is supposed to stay the same with every swap, fees aside. An example: a pool holds 10 ETH and 20,000 stablecoins. k is 200,000, and the price of one ETH is 20,000 ÷ 10 = 2,000 stablecoins.

If someone buys 1 ETH, 9 ETH remain in the pool. For the product to stay the same, the pool must then hold 200,000 ÷ 9 = about 22,222 stablecoins. So the buyer pays about 2,222 stablecoins for one ETH, not 2,000. This gap is called slippage, and it grows the larger the trade is relative to the pool.

It follows that large pools give better prices for large amounts. And the pool knows nothing about the outside world. If ETH rises on other exchanges, the pool price stays put at first, until traders exploit the difference and buy ETH from the pool until the price matches again.

Who fills the pool and why

The tokens in the pool belong to liquidity providers. They deposit both tokens in the current ratio and receive LP tokens, a receipt for their share of the pool. Every swap incurs a small fee, which stays in the pool and so flows to the liquidity providers in proportion.

That sounds like a simple business: provide money, collect fees. But the traders who bring the pool price in line with the outside world earn from exactly that adjustment. Their profit comes out of the pool, which means out of the liquidity providers.

Whether providing liquidity pays off therefore depends on a race: do the fees bring in more than the price movements cost? That cost has a name of its own, impermanent loss, and it is the subject of the next card.

Impermanent loss

When the price of one token changes, traders shift the pool: afterwards you hold more of the token that fell and less of the one that rose. Compared with simply holding, you are worse off. If the price doubles, the gap is about 5.7 percent; if it quadruples, about 20 percent. The loss is called impermanent because it disappears if the price returns. Whoever exits before that makes it permanent.

The benchmark is not zero but simply holding.

Full explanation

Worked through with an example

You deposit 1 ETH and 2,000 stablecoins while the price is 2,000. Your stake is worth 4,000, and for your share x · y = 1 × 2,000 = 2,000. Now the ETH price doubles to 4,000. Traders buy ETH from the pool until the ratio y ÷ x is also 4,000.

Both conditions together give: x = √(2,000 ÷ 4,000), about 0.707 ETH, and y = about 2,828 stablecoins. Your share is now worth 0.707 × 4,000 + 2,828 = about 5,657. Had you simply held, it would be 1 × 4,000 + 2,000 = 6,000.

The difference is about 343, roughly 5.7 percent of 6,000. You have not lost anything in the sense of having less than at the start, quite the opposite. But you have less than someone who did nothing. That gap is the impermanent loss.

When fees make up for it

For pools following x · y = k there is a short formula. If r is the ratio of the new price to the old one, the gap compared with holding is 2√r ÷ (1 + r) − 1. At r = 2 that is about −5.7 percent, at r = 4 exactly −20 percent, at r = 0.5 again about −5.7 percent. The direction does not matter, only the size of the move.

The fees have to at least make up this gap for providing liquidity to beat holding. In a pool with lots of trading and little price movement that is more likely. In a pool with little trading and a highly volatile token it rarely happens.

In pools made of two stablecoins the impermanent loss is small, as long as both hold their peg. If one loses it, the pool ends up almost entirely in the weaker token, because that is exactly what everyone sells into it. The low risk in normal times becomes the full risk in a crisis.

Borrowing against collateral

On lending protocols depositors lend out their tokens and borrowers take them. Because there is no credit check and no identity, every loan has to be overcollateralised: the deposited collateral is worth more than the debt. The interest rate moves with utilisation, that is, with how much of the deposited money is currently lent out. People who borrow this way usually want to use money without selling their collateral, or to build up leverage.

Without a credit check, collateral replaces trust.

Liquidation

Every type of collateral has a liquidation threshold, for example 80 percent. If the debt is larger than collateral times threshold, anyone may repay part of the debt and receives collateral at a discount in return. The borrower loses that discount. An example: 10,000 euros in ETH as collateral, threshold 80 percent, loan 6,000 euros. Liquidation starts once the collateral is only worth 6,000 ÷ 0.8 = 7,500 euros, after a price drop of 25 percent.

Work out the liquidation price beforehand, not once the price is falling.

Code risk and oracle risk

Every DeFi protocol is code, and code can have bugs. Audit reports reduce the risk but do not rule it out. Some contracts can be changed by their operator, and then you trust the operator too. Then there are oracles: a lending protocol needs prices. If the price source can be manipulated, for example through a small pool, an attacker can trigger false liquidations or take out loans against worthless collateral. Bridges between blockchains have repeatedly been the target of large attacks.

Every layer a protocol builds on is a risk of its own.

Questioning a return

The first question for any return is: where does it come from? Solid sources are trading fees, loan interest and staking rewards. Other sources are newly issued reward tokens, which are only worth what their price is, hidden leverage, or simply the money of new depositors. A return well above low-risk interest rates means someone is carrying a risk. If you cannot find out who, it is probably you.

A return without an explainable source is a warning sign, not an offer.

Full explanation

Four questions before every deposit

First: where does the money come from? Do traders pay fees, borrowers pay interest, or the protocol pay new tokens? Only the first two come from real demand. New tokens are a payout that dilutes the value of all existing tokens and is therefore often lost again in the price.

Second: in which currency is it paid? A return in a token that falls by half is only worth half. Third: what can go wrong, and what does it cost then? Code bugs, oracles, impermanent loss, depegs and liquidations are the usual candidates, and several often strike at once.

Fourth: how do I get out again? Is there a lock-up, a queue, an exit fee, a thin market? A return that cannot be collected when you need it is worth less than the number promises. Anyone who cannot answer these four questions should not deposit.

Calculate instead of believing

An example with made-up values: a pool advertises 30 percent a year. Of that, 5 percent comes from trading fees and 25 percent from reward tokens. If the reward token falls by 60 percent over the year, the 25 percent is worth only 10. Together about 15 percent remain, and any impermanent loss still has to be deducted.

Also watch how the figure is stated. APR is the simple annual rate without compounding, APY includes reinvesting the earnings. 12 percent APR reinvested monthly gives 1.01 to the power of 12, about 12.7 percent APY. Anyone comparing two offers needs both in the same form.

And finally: a return is a figure from the past. It shows what was paid over the last few days, not what will be paid next year. If more depositors arrive, the same income is spread over more money and the number falls. If instead it rises sharply, it is worth asking what others are currently fleeing from.

Glossary

DEX
Decentralised exchange where swaps run through smart contracts.
AMM
Automated market maker; calculates the price from the pool.
LP token
Receipt for your own share of a liquidity pool.
Impermanent loss
Gap between a pool share and simply holding.
Liquidation threshold
Share of the collateral value beyond which a loan is repaid by force.
APY
Annual return including reinvested earnings; APR without.

Further reading

This module comes with 18 explained questions. They live in the app, along with your progress and the credit for finishing.
The Coinovo appA reason to come back every dayTasks, shifts and the daily hunt, plus an account that shows what has added up.Daily tasksYour own accountCosts nothingStart now

More modules

What Bitcoin isMoney without a middlemanHow mining worksGuessing until it fitsHashrateHow much guessing goes onThe difficultyWhy the rhythm holdsThe halvingThe reward shrinksWallets and keysWhat you never hand overFeesYou pay for spaceMining poolsTogether instead of aloneSpotting fraudThe same tricks every timeTokenomics, taken apartBitcoin as the worked example, projects under testEthereum and smart contractsA shared computer with a fee meterProof of stake and stakingCollateral instead of electricityStablecoinsStable as long as the backing holdsThe Lightning NetworkBitcoin instantly, with conditionsSpotting and stopping scamsCheck first, then signAI and cryptoA tool, not an oracleInvesting with a planRules instead of gut feelingRunning mining as a businessWhat a machine really costsSelf-custody in practiceFrom the first key to inheritanceTax and bookkeepingWhat the tax office wants to seeHow the network really worksNodes, mempool, forksRisk and position sizeHow not to lose everything