UniSwap protocol from 0 to 100

arnau ramio
13 min readSep 4, 2020

It is undeniable: the enthusiasm around Blockchain is increasing every day and more and more people are interested in the technology that will lead to more social and commercial disruption in the coming years.

It should also be said that Blockchain is a “base” technology: like the Internet, its function is very basic: to verify and store transactions in a decentralized way in a ledger called Blockchain. The idea behind is simple, like the Internet (a data transmission network from point A to point B). This allows your applications to not be limited to a specific industry or a specific function. Its uses are practically unlimited, in fact, I am convinced that most of the use cases have not yet been released; very striking news for entrepreneurs looking for highly scalable and disruptive business opportunities.

With this in mind, one of the areas in which Blockchain is having the most impact is decentralized finance, also known as DeFi. We have already talked about it in other posts such as the one referring to Maker or FlashLoans, but to understand it I would define it as a way of generating financial services / products typical of the traditional financial sector (loans, guarantees, insurance …) adding some differences;

The first and most important is that they are decentralized, they do not depend on any central entity (mainly banking entities), since their logic of capital movement works by Smart Contracts. This allows them to be transparent, faster and more agile products, since -for example- there is no minimum amount of investment, and above all, they are accessible to everyone as they do not require KYC.

Second is the fact that thanks to Blockchain technology and its Smart Contracts it is possible to create completely new financial products / services, impossible with the traditional system. We are in front of a technology with the potential to change the entire financial system and de-monopolize the most concentrated and oligarchic sector in the world. And this without counting other benefits such as giving access to normal people or even the unbanked to investment opportunities and financial products that were previously only accessible to a minority.

Now that we have a little clearer on what DeFi is and why it is so revolutionary, I would like to dedicate today’s post to commenting on the UniSwap protocol: a DeFi service on the Ethereum network that very commonly leaves all those who listen to it fot the first time astonished. Let’s go for it!

UniSwap is a DeFi project born in November 2018 and developed thanks to a funding of $ 100,000 granted to developers by the Ethereum Foundation. In a short time, it has established itself as one of the most powerful protocols in the DeFi world (5th position in value according to DefiPulse on the time of writing the article), in addition to being the first DEX (decentralized exchange) to accumulate considerable value. A curious fact is that this super project has been driven by only 2 developers, who also have not generated any business model around it. Tthere is no foundation or internal token that accumulates the value generated in the protocol to be able to finance future improvements in development or similar things. It is something that Vitalik Buterin defines as “public pools”, decentralized systems that benefit all those who use it. This is where the discussion enters whether these projects are profitable in the long term, since in case of problems they could only be financed through donations. Although on second thought, Bitcoin is in the end something very similar to “public pools” and for now it is still the most consolidated and important Blockchain.

The goal of UniSwap is to allow users to execute “swaps” between tokens generated on the Ethereum network (mainly ERC-20 tokens). What does this mean? It’s like doing a trading card swap. Now, when you want to change a token, you are forced to go through a centralized exchange, pay commissions and compromise some privacy. Not counting the fact that normally to exchange one ERC-20 token for another (let’s say Chailink for BAT) you have to buy ETH with your Link token, and with that ETH, buy BAT. A very high gas expense and commissions.

The swap offers another solution, to be able to make direct changes at the atomic level, without going through any centralized entity, all through protocol. And despite how wonderful this sounds, the most revolutionary thing about UniSwap is not the “swap” itself, but the logic that they have integrated into the protocol to make this possible. Although to be able to appreciate the change that UniSwap offers, we must first understand how it currently works in traditional markets.

TRADITIONAL MARKETS

Traditional markets all work in the same way, they use an “order book” to determine the price of an asset. Markets are algorithmic software that connects buyers with sellers. With this software you have two possibilities to enter the market: buying / selling at market price, or buying / selling at a price that you determine. This generates a book of purchase and sales orders that allows the software to identify the price of the asset, since this will always be the value at which there are people willing to buy and people willing to sell.

BitTrex BTC / USD order book

This method is the one used in any classic exchange, and allows to resort to investment strategies such as the use of algorithmic bots capable of predicting future price behavior by analyzing the orders registered in the market. You can even simulate levels, or try to position yourself first to be the first to settle the trade. This system also has certain problems, since in markets with little volume the price can be very manipulable, in addition to the fact that in moments of panic where nobody wants to buy the price it can fall to 0 despite the fact that the asset that is represented (the shares represent ownership of real companies) is much higher. Without going any further, the COVID-19 crisis has caused falls of more than 80% in some airlines, although these remain the same as two months ago. This is why the stock markets close when there are sharp falls, to avoid this panic that spreads throughout the world and causes nobody to want to buy, leading to a free fall in price that will continue to fall until it finds a level where there are people willing to buy.

United Airlines Stock Chart (~ 80% drop)

UNISWAP LOGIC

UniSwap, for its part, works completely different from traditional market models, and this is the most fascinating part of its protocol. First, the idea that there must be two people willing to do the swap and that a software brings them together no longer exists, instead a pool is created that will provide liquidity so that swaps can be made at any time. This in turn means that there are people encouraged to lock their tokens in a pool to give liquidity to swaps, although we will see this a little later. Second, the price is not determined by the buy / sell orders, but by a formula that moves the price each time there is an order to make a swap. Something complex that we better see with examples.

The idea is this: we have a pool with two types of tokens that can be swapped. Each pool is made up of two tokens (ETH-DAI) for example, and there will be a different pool for each pair that can be swapped. When a person wants to swap 1 ETH, for its value in DAI, the price at which the DAI will be acquired will depend on the situation of the pool. The more ETH you want to swap, the more the price will get. This is determined by this formula:

Let’s put in the case that a person wants to swap for 1ETH and receive BAT in return. The swap price is directly related to the liquidity of the pool, and the total value of the swap to be carried out, since the more pressure you put on the pool, the more cost the swap will have. This makes it possible to discourage swaps when there is little liquidity in the pool, and to encourage swaps when the opposite is the case.

Before the swap request, the pool has the following situation: it consists of 10ETH and 500BAT. From there the “invariant” is calculated, a number that cannot vary and is calculated by multiplying the amount of ETH * by the amount of BAT. When requesting the swap, the pool starts to have more ETH than there was before (11), so the Smart Contract must execute a calculation to know how much BAT is left in the pool in order to keep the invariant at 500.

That is: 500- (5000/11) → the result will be the amount of BAT that you will receive for 1 ETH.

If in the example the swap were of less value (say 0.1 ETH) the amount received would change and would become 4.9 BAT. As you can see, the acquired value is proportionally higher than before, since by asking for less from the pool, it does not see its liquidity as compromised, and offers a better price for the swap. Amazing!

Last example to make it clearer. A person wants to swap the pool of (ETH-MKR) to swap 10 ETH for the corresponding amount of MKR. Remember that the amount of MKR will depend on UniSwap, depending on the liquidity of the pool and the pressure we put on it (the more value we want to swap, the more pressure we put on the pool). This pool consists of 1000ETH and 470MKR. Let’s do the math.

Invariant = 1000 * 470 → 470,000

The pool situation has now changed, we have 1010ETH, and then we must calculate the amount of MKR that the swap applicant will receive, since we must keep the invariant stable.

470,000 / 1010 → 465.34
470–465.34 → 4.675 MKR

The applicant will receive 4,675 MKR. If we repeat the example by requesting a swap of 100ETH, the amount of Maker received would be 42.72MKR, a price percentage more expensive than the previous one, since we have stressed (requested more liquidity) the pool, and this has moved the price of the swap accordingly.

LIQUIDITY POOLS

The question we should ask ourselves is where does this liquidity come from in the pools? What incentive is there to freeze your assets in a UniSwap pool to allow people to realize them?

This is where the swap commissions come in, which are 0.3% in swaps where one of the tokens is ETH, and 0.6% when none of the swapped tokens is ETH. These commissions are distributed among all the people who are giving liquidity to the pool as a reward. This topic will be interesting to investors, since we can see returns of up to + 90% per year. The profitability will be given by the combination of 2 variables: the pool liquidity and the daily volume.

  • Low liquidity — high volume: This is the perfect combination, since not only will there be a lot of volume and therefore many commissions, but also the price of swaps will be high, since the pool will be stressed each time.
  • Low liquidity — low volume: As there is little liquidity, prices will surely be high, but there will not be much return because the volume will be low.
  • A lot of liquidity — a lot of volume: This is also a wise combination, since surely the value moved in the pool will not be as high as in cases of little liquidity (the pool will not be very stressed and will allow swaps at good prices) but there will be a lot of volume to generate profitability with commissions.
  • A lot of liquidity — little volume: This is the worst combination, since not only will the prices be low (as will the commissions) but there will also be little volume to collect them.

This is the reason why UniSwap has surely grown so much, being the first decentralized exchange that has really been able to accumulate a lot of locked value in its liquidity pools. In order to know the liquidity and profitability of the pools in UniSwap we can visit: www.pool.fyi and if we want to add liquidity and get a return on our crypto capital we can do it from uniswap.com.

www.pool.fyi

It is also to comment on the risks, which although there are none (since you will not lose money, only generate income through swap commissions) it may happen that you obtain negative interest due to the fall in the price of the deposited tokens, and not because of the protocol. The risk — or rather the characteristic of the pools — is that you must always deposit two tokens (ETH-DAI / ETH-MKR / ETH-BAT …) and therefore depending on the demand for swaps you will accumulate more than one token than the other. This imbalance is more exaggerated when one of the tokens loses a lot of value, since due to the protocol it is usually the one that we accumulate the most.

Some clarifications for when we want to add liquidity: we must keep 2 tokens in that pool, depending on the pair in which we want to participate, and the value of both must be the same. For example, if I want to put liquidity in the pool of (ETH-DAI) by putting 1 ETH (170 $) I will be obliged to put 170 DAI (1 $). Once in the pool, the proportion between these two will vary depending on the demand for swaps, and generally, due to the protocol, more of the token will accumulate that has decreased more in value. Your returns will be acquired through the pool token called UNI-V1 (etherscan), which will represent the proportion of the pool of which you are the owner and which, in turn, will accumulate the returns generated by the pool. It will be through this token that you can recover your investment and collect the returns that you have been generating.

UniSwap app

CONCLUSION

Once we have seen how UniSwap works and all the possibilities it offers us, let’s take a moment to reflect on what this innovation represents in the field of finance and why it is revolutionary.

The first point is clear: it is a never seen way to calculate the price and to generate markets. For the first time, the price is not regulated by the purchase orders, but through an algorithm that moves the price for each swap that is made in the pool. Now it is no longer necessary to have purchase orders to be able to maintain the price of an asset. Goodbye to the fortunes spent on market makers to maintain the price of an asset.

Another wonderful thing is the possibility of creating markets from scratch and giving liquidity to a token (or a tokenized asset) without the need to list it in an exchange, where they do not ensure liquidity either, since this will depend on the purchase and sale orders that there. With UniSwap you can give a value and liquidity to your token by creating a pool with ETH, which gives it a value. If we give it a couple of turns, creating a pool with 100 ETH and 100 of our tokens implies that our token will have a value of 1 ETH. As you can see, we have just given liquidity, value and market to a new token, and we have not needed hundreds of thousands of dollars to list it on an exchange, where we will probably need Market Makers to generate buy orders so that its value does not fall to 0.

Another surprising fact is that in UniSwap “security-tokens” can be swapped. Currently there are some available, although we will not be able to see it from our Metamask, since to transact with “securities” we must accredit ourselves with the token issuing company that we are accredited investors. Still, the idea behind it is fascinating. I can sell my 0.1% of a block of flats in Madrid, anywhere and at any time. This is to give liquidity to illiquid markets.

RealT — Real Estate tokenization on Uniswap

This innovation also allows new business models such as Unisokcs, limited socks that can only be purchased with the Unisock token, only available on UniSwap. This means that every time someone buys a token to buy the socks, the value of the token will increase, giving more and more value to the socks based on their scarcity. [The pool will have less liquidity and therefore the swap price will be higher].

Unisocks!

Without a doubt, we are in a time of change and innovation. The doors that the DeFi world opens to us are limitless and more and more door will open. In the coming years we will see how Blockchain technology will connect the world economically, in the same way that the internet connected us socially. And it is actually this feature that fascinates me the most about the DeFi protocols; there are no restrictions, anyone in the world can participate and access financial services that were previously only accessible to a minority.

Welcome to the world revolution in the financial sector, and there is no doubt, “blockchain is to money what the internet was to information.” From all this, as we always say, we dedicate a lot of love in our Blockchain face-to-face programs. You can visit our Master in Blockchain or our DeFi Bootcamp. :-)

REFERENCES

--

--

arnau ramio

Blockchain-Business Developer & DeFi Manager. Co-Founder & COO at Themar Solutions and Co-Founder of Learningheroes.com