Uniswap protocol: how it works

Uniswap protocol: how it works

Uniswap protocol: how it works

logo uniswap picture

Uniswap is a sleek ERC20 token exchange solution that does not use the classic exchange toolkit.  The protocol is based on creating pools of liquidity for tokens by combining them into smart contracts.


Due to this, everyone can carry out literally any range of actions with them: from creating currency pairs to exchanging or adding tokens.  It is a convenient and reliable tool that allows you to solve liquidity problems in transactions with digital assets by fully automating them.


Fungible tokens

ERC20 token picture

Created on the basis of the Ethereum blockchain, ERC20 tokens are optimal for the work of a market maker, since they combine such features as:

     • support for smart contracts;

     • the ability to use securities as digital analogues;

     • interchangeability.


The latter property plays a key role in the selection of these particular tokens.  The bottom line is that any of them can, without changes within the system, take the place of any other.  This does not violate the terms of exchange between users of the service, and also universalizes the transactions themselves.


Also ERC20 is a divisible unit of account.  She can participate in operations both in whole and in part.  At the same time, individual parts of the token are also proportional and can be replaced with any other equivalent parts of another of the same token.


Types of smart contracts in Uniswap

It is worth clarifying that Uniswap is not just a protocol that automates operations with tokens within a software environment.  This is, first of all, a tool that creates standards based on smart contracts, according to which transactions with ERC20 take place between all participants of the service.


How it works?


Uniswap includes two types of contracts:

     • Exchange (exchange contract).  Such contracts are based on the already existing (added) ERC20 token in combination with ether coins.  These assets are available to every investor and can freely participate in the exchange between them;

     • Factory.  These contracts are designed to create and control individual contracts of the first type, as well as to include addresses of new tokens in them.

Thus, Uniswap allows you to add new tokens and create trading pairs with them completely free of charge.  After checking for uniqueness, the created contract, with tokens tied to it, enters the general service registry.


Liquidity Pools as an Alternative to Order Book

picture of magic on the chart

The main "feature" of Uniswap is the exclusion of the principle of working with the order book.  Whereas classic crypto exchanges consolidate currency rates based on the supply of sellers and demand from buyers, Exchange contracts in Uniswap form full-fledged liquidity pools, where the user can directly exchange Ether for any of the presented ERC20s.


With the help of this mechanism, the investor excludes the need to work with the counterparty's order, which in the conditions of traditional crypto-exchanges can take a decent amount of time.  Moreover, with such an exchange there are no liquidity problems, due to which the exchange rate stability is achieved.


But how is the ratio of the cost of the hosted ERC20 to the user Ether calculated?  It's all about the proportional relationship between the actual amount of one and the other asset in a particular pool.  The market maker works automatically according to the formula x * y = k, so the larger the request from the token buyer, the lower his rate.


On the other hand, the correction of the ETH rate in relation to ERC20 is carried out at the expense of arbitrage traders who trade both on centralized exchanges and in several pools at the same time.  Thus, price regulation with third-party cryptocurrency markets is supported.

It works as follows.  Let's assume that the ratio of the token value to the ETH unit is 15:10.  In this state, the price balance corresponds to the ratio of the values ​​of these assets in the external market.


When the cost of ETH is halved on a third-party exchange, its price in relation to the token in the pool does not change.  In this case, arbitrage traders exchange ETH for a token through the pool at the old cost, thereby receiving twice as many tokens as if the exchange took place on the exchange.


Then the trader uses the token to buy ETH on the external trading platform, adds it to the pool and makes a profit by re-exchanging it for the token.  As a result, repeating this scenario aligns the exchange rate of the token and ETH in accordance with their external market quotes.


Thus, in Uniswap pools, it is possible to avoid unnecessary volatility of assets, while not losing connection with their rates on centralized trading floors.


Direct exchange of ERC20 tokens among themselves

Uniswap also provides the ability to exchange different ERC20 corporate tokens from its customer pools within a single transaction.  This is done by calling the tokenToTokenSwap function.  Its peculiarity lies in the fact that it can simultaneously enter the token A, which the client has, into the corresponding pool, displacing ETH from it, which is instantly sent as payment for token B. As a result, the user actually buys token B for token A.


Methods of providing liquidity in a pool

When creating an exchange contract, its pools are completely empty until the moment the first client makes a deposit.  If investments are made in a proportion different from the ratio of the market values ​​of tokens in relation to ETH, the liquidity of the pool may be questioned.  To avoid this, arbitrage traders should deposit a proportional amount of ERC20 and ETH into the contract pool.


The key merit of the large Uniswap pools is that they allow you to achieve liquidity under the exchange contract rather quickly.  One of the motivating mechanisms of the market maker is the payment of a commission for adding liquidity to the pool, which is frozen together with the deposited assets for the initially specified period of time.  The fees for other users are approximately 0.3% for a token / ETH exchange, and 0.6% for a token / token exchange.


In addition, the investor receives incentive liquidity tokens, which are awarded in proportion to the liquidity added to the pool.  However, these tokens burn when the assets are returned after they are frozen.