Academy Icon

Academy

DeFi

How to Calculate The Price Impact of Trades on DeFi Swaps

Mar 14, 2023
10m
Jacob CamilleriFrancesco Maddalena

DeFi swaps are a popular platform for crypto traders to buy and sell tokens without depending on centralized exchanges. But if you’ve ever used a DeFi swap, you know how complicated they are and the risks involved due to volatility, unpredictable traders, the impact of whales, and much more. 

At Sonar, we’re always looking for ways to take the uncertainty out of crypto trading. Our new Price Impact Calculator is designed to predict the impact of your DeFi swap trades on any token. You can also run hypothetical scenarios, modeling the impact of buy/sell orders by other people holding tokens. This way, you can decide if a token is worth investing in or is too unpredictable. 

In this article, I will explain how DeFi swaps work and the risks involved.  Then I’ll briefly introduce our Price Impact Calculator, which you can use to make better predictions in your trading.  

What Are DeFi Swaps?

If this is your first time hearing about DeFi swaps, here’s a quick rundown of how they work. (If you’ve already used DeFi swaps a few times, you can skip this part). 

DeFi swaps are decentralized exchanges that allow you to trade cryptocurrencies directly with other holders without using a centralized exchange (i.e., Coinbase or Binance). In centralized exchanges, you’re actually trading with the exchange itself, which acts as a middle-person matching buyers and sellers for a small fee.

In contrast, DeFi swaps are built on the blockchain and use smart contracts to execute trades automatically. Trades are executed directly between users' wallets, with no need for an intermediary via automated market-making algorithms to determine a token’s price and execute trades. 

As a result, DeFi swaps can offer greater privacy, lower fees, and faster transaction times than centralized exchanges. You can also trade a wider range of cryptocurrencies than centralized exchanges, as you’re not limited by the exchange's inventory of coins. You can trade any cryptocurrency supported by the swap's underlying blockchain.

You can participate further by providing liquidity to the pool of funds used to facilitate trades. And in some cases, you can earn rewards for doing so.

Uniswap, SushiSwap, and Curve are all DeFi swaps you’ve probably used or heard of. 

Trades on Defi Swaps (Decentralized Exchanges) Vs. Trades on Centralized Exchanges

Defi swaps were developed to cater to cryptocurrency tokens on the Defi Market and are especially useful for tokens with low market cap and trading volume. 

In centralized exchanges – which are backed by millions of traders and much higher trading volumes – order books are the preferred system of trading. 

Order books are a record of bid prices by traders trying to buy or sell a cryptocurrency token. A trade is only executed when two parties – the seller and buyer –agree to the same price. If no agreement is made, no trades are executed. 

How Order Books Work on Centralized Exchanges

01_Diagram 1.png

Diagram 1: A simplified BTC/USDT order book showing the limit orders set. 

The diagram above shows a very simplified version of the following order book: 

Trading Bitcoin using USDT.

Sell Orders

Size Column: 3 sets of 1,000 USDT sell orders set at the following prices in Bitcoin: 

  • 32,000 

  • 31,000 

  • 30,000 

Sum Column: The total amount of 3,000 USDT sell orders. 

Buy Orders

2,000 USDT worth of buy orders set at: 

  • 29,000 

  • 28,000 

  • 27,000

Total amount of USDT = 6,000. 

This means that anyone trying to buy Bitcoin at a price less than 30,000 USDT can’t do so until someone decides to sell some Bitcoin at that price. 

Simultaneously, at this moment, one cannot sell their Bitcoin at a higher price than 29,000 USDT.

For lower market cap tokens, an order book system could face long periods without any trades occurring due to the significantly smaller number of traders executing the orders and, consequently, much lower trading volume. 

  • How do DeFi swaps get around this? 
  • What price impact can a buy or sell have? 
  • What is the relationship between supply, market cap, and price on DeFi swaps? 

These are the questions we’ll be answering in this article.

But first of all, we must familiarize ourselves with some critical Crypto and DeFi terms.

Essential Terms For DeFi Swaps

Before trading on DeFi swaps and centralized exchanges, it’s crucial to understand the following terms and concepts. 

Market Cap: Crypto market capitalization (“market cap” for short) is the total market value of a cryptocurrency. Crypto market cap is calculated by multiplying the cryptocurrency's price by the number of its tokens currently in circulation.

Liquidity Pool: A liquidity pool in DeFi refers to a pool of tokens locked in a smart contract. The pool usually (not always) consists of 2 different tokens: native tokens and paired tokens (explained below). These tokens can be traded in and out of the pool using Automated Market Makers (AMM), better known as DeFi swaps.

Price: The value of a token in relation to either the native token value or a fiat value such as USD (for example, 1 PING* token = 0.0000025BNB, or $0.00075).

Circulating Supply: The total number of coins or tokens actively available for trade and used in the market and by the general public. (i.e., PING has a circulating supply of 3.8 billion tokens).

Native token: The original chain token forming part of a particular liquidity pool within a DeFi swap (examples include BNB/BUSD for BSC chain, ETH or USDT for ETH chain, etc.).

Paired token: The unique token paired with the Native token within a DeFi swap (i.e., PING in PING/BNB, LINK in LINK/ETH, etc.).

Price Impact: The percentage increase or decrease a particular trade will induce on a paired token price (i.e., 1 BNB or $300 swap for Token B will have a price impact of 5% on Token B).

*PING is Sonar’s native token. 

How Do Transfers, Swaps, and Liquidity Pools Work?

Now that we know what these terms mean, how do they relate to the price of a token? And how does a transaction (buy or sell) impact the economics of a liquidity pool? 

The answer is simply a mathematical equation that I’ll explain below. 

The first part of the explanation will help you understand the flow and exchange of tokens, while not being entirely mathematically accurate to the nearest degree. 

In the second part, I discuss the mathematical equations that provide a more accurate depiction of the internal mechanics of the swap functionality.

Defi swaps enable traders to swap tokens without needing someone else to set a limit buy/sell order. In fact, the technical term for swaps is Automated Market Makers (AMM). PancakeSwap, Uniswap, and Sushiswap are all AMMs, trading on different blockchains. 

The Swap holds a liquidity pool consisting of a mix of Token A (paired token) and Token B (native token)- Trading occurs through a simple process of give and take by allowing traders to execute a transaction that involves a 2-way transfer. 

This means that a trader may “purchase” Token A from the liquidity pool and replace it by depositing Token B back into the liquidity pool. The diagram below explains this using PING as Token A (Paired token) and BNB as Token B (native token).

02_Diagram 2.png

Diagram 2: A liquidity pool of 2 BNB and 2 PING tokens and a purchase of 1 PING token for 1 BNB token through a Defi Trader wallet.

The diagram shows how initially, there are 2 PING tokens and 2 BNB tokens in the pool. 

Therefore the price of 2PING = 2BNB, or 1 PING = 1 BNB. 

As soon as a trader swaps and purchases 1 PING, 1 PING token is extracted from the liquidity pool and into the trader´s wallet. Within the same transaction, 1 BNB token goes from the trader’s wallet into the liquidity pool. 

This leads to a pool containing 1 PING token and 3 BNB tokens. This means that 1 PING is now worth 3BNB. The price of PING has tripled in value in relation to BNB – so the Price Impact was 300%.

Using the above diagram, we can also conclude how smaller liquidity pools are more volatile than bigger ones. The bigger the liquidity pool, the less of a price impact the same trade will have. A comparison can be seen in the diagram below.

03_Diagram 3.png

Diagram 3: The effect of liquidity pool sizes on price impact.

How Does a Token’s Price Relate to Supply and Market Cap in DeFi Swaps?

At any time, there are four specific “standard” token values that cannot change without a transaction taking place. 

  1. Circulating supply
  2. The value of token A (paired token such as PING) in the pool 
  3. The value of token B (native token such as BNB) in the pool
  4. The current $ value of token B (native token such as BNB)

Using these known “standard” values, we can calculate all the other important values related to any paired token in a liquidity pool (such as PING).

Price

Let’s keep using the PING/BNB pool as an example. Let's say that the liquidity pool contains 480,000,000 PING and 1,200 BNB at the time of writing. We also know BNB´s US dollar value is currently $300.

Therefore, we can deduce the price of 1 PING in BNB, using the equations below.

Paired token in pool/Native token in pool = Price of paired token

Ex/ 480,000,000 PING & 1200BNB in pool

Therefore, 1 PING = 1200/480,000,000 = 0.0000025BNB

We also know that BNB is currently worth $300. Using the equation below, we can deduce the price of PING in $ value.

Paired token in pool/Native token in pool x $Price of Native Token = $Price of Paired token

Therefore, 1 PING = 1200/480,000,000 x $300 = $0.00075

This also explains why any USD price changes in the native token (such as BNB) will directly correlate to the difference in the price of the paired token (PING).

Let's say BNB goes up 100% to $600,

1 PING = 1200/480,000,000 x $600 = $0.00150 (+100%)

Price of PING in USD has doubled, directly proportional to the increase in BNB USD price.

Market cap

How do we calculate the market cap? This is one of the easiest calculations. 

Market cap is calculated by multiplying the price of the paired token with the circulating supply.

Market cap (Mc) = Price (P) x Supply (S)

Using the PING example above,

PING Mc = $0.00075 x 3,800,000,000 = $2,850,000.

The calculations are explained in a simplified manner in the table below. The same table uses PING as an example.

Table 01_0k.png

Table 1: The calculations behind the derivation of token economics. Standard values represent values that cannot be changed at the moment without a trade. The bottom row shows the PING/BNB pool at the moment of writing this article. 

Price Impact

Now that we understand how to calculate all these values, how can we calculate the price impact of trades? 

How do swaps affect the movement of a token’s price? 

To understand what happens, we must look at the code on which every DeFi swap was built, originally developed by Uniswap (considered one of the first and largest DeFi swaps). 

Uniswap’s original price calculation (a version of which most DeFi swaps are based on) is based on a constant product formula that determines how much of one asset (Token A) should be swapped for another asset (Token B) in the liquidity pool. 

(It is important to note that other DeFi swaps may have slightly or significantly different formulas. However, the most common system is based on the one developed by Uniswap). 

The formula is shown below:

Constant Product =   Token A in pool x Token B  in pool

The table below shows all the standard values and formulas a DeFi swap uses to calculate the price impact of any given trade. The table includes an example of a PING/BNB pool at current values. The table should be used in conjunction with Table 1 in the previous section, and is using the values already calculated in the token economics.

Check the complete spreadsheet >

table_02_v.3.png

Table 2: The formulae and calculations carried out by the AMM, while also showing two examples of trades within the PING/BNB pool – A purchase of 300BNB worth of PING; and a sell of 10 million PING.

We won’t get bogged down by the intricate details of every bit of math within the table. However, we can review the results shown for the PING/BNB example to understand the economics betters.

We know through Table 1 that PING is currently priced at $0.00075 and has a market cap of $2.85 million.

Within the BUYS rows, the table shows how a 300BNB (Up) or $90,000 (Up) buy within the PING/BNB pool will result in the following:

  1. The PING in the pool reduces to 384,000,000 PING tokens (An)
  2. The BNB in the pool increases to 1500 BNB tokens(Bn)
  3. The Buyer receives 96 Million PING tokens (W) swapped at an average price of $0.0009375 per PING (Pp)
  4. The Price of PING goes up to 0.00117$ (Pn), and a resulting market cap (Mc) of $4.4 million
  5. A total price impact of + 25% (Pi)

Conversely, the table shows how a 10 Million PING (Un) sell would result in the following:

  1. The PING in the pool increases to 490,000,000 PING tokens (An)
  2. The BNB in the pool decreases to 1175 BNB tokens (Bn)
  3. The Buyer receives  24.48BNB (W) swapped at an average price of $0.0007347 per PING (Pp)
  4. The Price of PING down to $0.000719, and a resulting market cap (Mc) of $2.7 million 
  5. A total price impact of -4.04% (Pi)

The results above help us make important conclusions essential for smart trading, especially within the low market cap space. These include:

  1. The trader can never swap at the exact price shown on the chart. 
  2. A “buy” is averaged at a higher price than the chart price. Conversely, a “sell” is averaged out at a lower price than the chart price (this is shown in the “Price paid” column in table 2).
  3. The bigger the buy or sell (trade) value, the bigger the price impact on the chart, AND the bigger the difference between the chart price and the trade price paid.
  4. Bigger liquidity pools lead to less price impact (i.e., less token volatility) and a better swap price for your trade.
  5. The increase in the price of the native token (ex/BNB) is directly proportional to the increase in price of the paired token (ex/PING) if NO SWAPS OCCUR. This means if no one buys/sells PING, while BNB doubles its $ value, PING should double its $ value as well.

Price Impact Calculator

At Sonar, we understand that not everyone loves math as much as we do. That’s why we’ve built a calculator that does all these calculations for you. 

05_Impact_calculator.png

In the current version, you input the expected buy or sell within your chosen liquidity pair, and the calculator automatically returns the projected market cap, price, and price impact induced by that particular buy/sell. 

Future implementations will include several updates and new features that will help you:

  1. Analyse if your buy or sell will have a massive price impact on a token that could result in a loss of value due to people selling afterward. 
  2. Get an idea of what level of investment is needed to reach a target price iii) analyze the prices impact a sell from one of the top holders of a token would have, and much more.  

Disclaimer

It is important to note that other variables might affect the price impact and DeFi swap economics, such as transaction taxes, gas fees, etc. While the above calculations are correct, such variables may lead to (usually slightly) inaccurate results. However, this gives a very good idea of the economics and what to expect with every transaction executed.

Watchlist
Save tokens you want to track over time
Beta Version: 0.5.5