Bitcoin Forum
April 13, 2021, 04:04:54 PM *
News: Latest Bitcoin Core release: 0.21.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: [Tutorial] Uniswap V2: A complete overview of all innovations.  (Read 189 times)
Ratimov
Hero Member
*****
Offline Offline

Activity: 686
Merit: 3334


Margin enthusiast


View Profile
September 17, 2020, 11:55:42 AM
Last edit: September 17, 2020, 12:40:49 PM by Ratimov
Merited by ImThour (2), livingfree (1), Shef198911 (1)
 #1


s0urce: theblockchaincircle.org



Table of contents:


1. Introduction
2. What's new in Uniswap V2
3. How Uniswap V2 Works
4. Swap Options Uniswap V2
5. Price Oracle
6. Flash Swapping
7. New 0.05% Protocol Fee and Governance
8. JavaScript SDK
9. Summary





Introduction


Uniswap - one of the most common decentralized exchanges for trading and exchanging tokens, based on the Ethereum Blockchain. In this exchange, all token swaps are performed without intermediaries, directly from smart contracts.

Uniswap V2 was launched in the main Ethereum network in May 2020, before that testing was carried out on the Rinkeby testnet. Also at this time was released their main contract v1.0.1.

Who is interested in Uniswap in the first place? Those who hold, trade cryptocurrency are interested in the development of the Ethereum ecosystem, financial technology and financial trends in general. In the 3rd quarter of 2020 Uniswap experienced significant growth, as well as the whole defi-industry. Uniswap exists as a separate service, but can be integrated into third-party wallets or Dapps. Also for many smart contracts Uniswap is the basis.

Now let's look at the innovations of the second iteration of Uniswap.



What's new in Uniswap V2

In the second iteration of the protocol there were a number of improvements and changes, which were based on the exchange and liquidity mechanisms presented in Uniswap V1.

ERC20 to ERC20 token swaps - Now there is no need for an intermediate token to exchange between ERC-20 tokens, before as an intermediate token was Ethereum, it was known as ETH bridging. This feature reduces the number of transactions by half and therefore reduces the gas fee. It also allows Dapps to find routes for that one token to another in case there is no pool created for direct token swap.

Price oracle - The price oracle allows setting a weighted average price value based on the prices of token pairs in each block.

To learn more about the price oracle, click here: [Tutorial] What Is an Oracle? Oracle Types.

Flash swapping - Flash swap is needed for arbitrage transactions, where you can borrow tokens from Uniswap pool, conduct arbitrage with external service and then return the funds back. And all this in one transaction. The transaction is atomic, that is, if something goes wrong at some stage, the transaction is cancelled and the funds are returned back. Most often such flash swap is used for arbitration, but there are other cases of use that allow you to save on gas fees when there is interaction with the defi, such as closing Maker Vault

Support of non-standard ERC20 tokens -
by treating a void return type of transfer() and transferFrom() as a successful transfer. This may seem trivial, but major tokens such as USDT (Tether) and BNB (Binance Coin) do exactly the above on their transfer methods. Including widely adopted tokens that did not completely adhere to the ERC20 standard has strengthened Uniswap’s proposition to be the leading on-chain exchange in terms of usage.

Quote
Non-standard ERC20 tokens must support the methods specified in the official EIP specification, but return types, and consequently the method’s signature, may differ from what was outlined. This issue makes a token non-standard. Renaming or removing required methods also makes tokens non-standard, but this is less practiced and would require major workarounds if protocols like Uniswap wanted to support such deviations from the finalised spec.

Also Uniswap V2 introduces a switchable commission of 0.05% for protocol usage. It is currently disabled and can only be enabled for decentralized control mechanisms.


Uniswap V2 rolled out after extensive testing and auditing

Since smart contracts very often contain critical vulnerabilities, a quality audit is required and to avoid bugs and logical errors the Uniswap team needs a set of measures:

The move from Uniswap V1 to V2 saw the contracts re-written in Solidity, from Vyper in the first version. This successfully overcame the limitations of Vyper and allowed the Uniswap developers to leverage the newer opcodes that the latest Solidity versions have rolled out, further optimising the contract execution in terms of CPU resources used (and therefore gas fees).

Uniswap V2 relies on multiple smart contracts ranging from the Factory, Router (V2), Pair and Pair ERC20 contracts, along with a Library contract for utilities.

The major contracts that get Uniswap working are as follows:


The structure of smart contracts that make Uniswap V2 work.

Router2 is a more optimised version of Router1, but they both perform the same tasks. Uniswap advise that all developers now switch to using the Router2 contract.

The audit of Uniswap was conducted by Consensys Diligence. It presented a full report on the source code of Uniswap v2. You can see the report here

Also Uniswap has a Bug Bounty Program, in which anyone can participate, who will reveal some bugs and vulnerabilities.

Security is an ongoing concern in the blockchain space, and understandably so given the amount of value being traded in the field. Attacks on underlying blockchain mechanics that pertain to consensus, block creation and transaction validation are the focus of a lot of research in academia, but smart contract integrity is arguably just as important in the case of Ethereum where locked-up tokens rely on the integrity of the smart contract logic to keep them secure and reachable.

Now it's time to talk about how Uniswap V2 works.



How Uniswap V2 Works

Each pair of tokens has its own pool initialised via the Factory contract, and initial deposits are made to the pool in order to provide liquidity.

The exchange rate of the token is calculated based on the supply and demand of two tokens in the pool, with what is termed the constant product formula. With the constant product formula, token values in a particular pair are calculated based on supply and demand, where the value moves along a curve of the formula:


The constant product formula that determines exchange rates on Uniswap V2.

Exchange rates are automated based on the simple formula: x * y = k. The corresponding curve represents all possible token values, and each token pair will have their own curve that will regulate the current state of their exchange rates.

If B Token is in huge demand and quantities dry up then the price will increase exponentially, and move up the left side of the curve as a result. If on the other hand B Token has ample supply and no demand relative to A Token, the price will level out on the right side of the curve. Note that this supply-demand balance is based relative to A Token, so the same B Token paired with different A Tokens will represent separate quantities and price ratios.

As a result of these mechanics, Uniswap (and other decentralised exchanges relying on the constant product formula) rely on arbitrage trades to keep the value of the token in line with the rest of the market. Essentially, these protocols still require an external trading ecosystem to keep token values in check. Exchange rates of each token pair will constantly be updated to match the market prices — and this provides a huge opportunity for traders.
Exchange rates are applied in real time as you switch between token pairs on Uniswap.exchange. The JavaScript SDK (that we’ll look at further down) provides APIs for other apps to provide their own interfaces for fetching token pairs and their exchange rates. Exchange rates are applied to native ETH in addition to ERC20 tokens:


The exchange rate being applied to the ETH | DAI trading pair within the Uniswap V2 UI.

Now let's look at how swaps work, where an intermediate token in the form of ETH, also known as ETH bridging, is no longer needed, as it was in the first iteration (V1).



Swap Options Uniswap V2

 In Uniswap VI it was always necessary to use ETH as an intermediate token. That is to say, to change one token to another, you had to first change token 1 to ETH and then ETH already changed to token 2. This method increased the number of transactions and accordingly increased the gas fee.

Uniswap V2 applies direct token swaps without the intermediate token ETH:


A direct swap between two ERC20 tokens.

This seems reasonable and intuitive, and was perhaps the way V1 should have worked. The swapExactTokensForTokens and swapTokensForExactTokens methods on the Router contract can be called to make such a transaction.

The Exact terminology used in these method names represent the token you wish to trade to. In a trade from DAI to ETH where you require a particular amount of ETH in return, you would use the swapTokensForExactTokens. On the other hand, if you wanted to trade an exact amount of DAI for the corresponding ETH value, you would use swapExactTokensForTokens. This convention is used throughout Uniswap V2’s smart contracts.

The user can still use ETH as an intermediate token, but if there is no pool combining 2 tokens, this option is useful and less expensive.

The picture below shows a DAI and LINK exchange via ETH:


An indirect swap where ETH is the intermediary token (akin to Uniswap V1).

The corresponding Solidity methods that allow such transactions are swapExactETHForTokens and swapETHForExactTokens, along with swapTokensForExactETH and swapExactTokensForETH.

The final means of swapping tokens is one that routes a swap to multiple ERC20 tokens, or “arbitrary pairs of ERC20 tokens” as the white-paper terms it, before arriving at your desired output token. Of course, the native ETH token could be present in an arbitrary pair too.

In the following picture, let's look at the variant as DAI moves along the token range to obtain LINK:


Routing funds through a range of tokens to exchange a token with no direct pair.

This method is useful if there is no direct pool with input and output token and no more ETH-bridging is needed.

Although this swapping method is possible, there is no smart contract method available to automatically facilitate a multitude of token swaps. Instead, front-end applications must code the functionality manually and make multiple transaction calls to Uniswap instead.


Price Oracle

The mechanism of the price oracle allows to calculate the weighted average price of a token based on how the price of a token moves through a number of blocks. The time period may vary. It can be 1 hour and a day.

Although Uniswap provides token prices, it does not store any historical values on-chain. Instead, it is the Dapp developer’s responsibility to accumulate prices over a period of time to work out the average price over that time.

These prices are called TWAPS. That is, Time Weighted Average Prices.

The idea is to calculate the average price over a period of blocks by dividing the cumulative price (the price of the token at each block of the duration) by the timestamp duration (the end-of-duration timestamp minus the start-of-duration timestamp).

This is how it looks:


A TWAP is calculated by accumulating prices over a duration of blocks over the timestamp duration.

This approach protects against price surges and wild volatility, which is not uncommon in the cryptovoltaic world. The time weighted average price more accurately reflects the token representation on the market when there is strong volatility.



Flash Swapping

Flash Swaps are transactions consisting of a multi-stage process of borrowing tokens from a Uniswap token pool, doing something with those tokens, and paying back those tokens — all in that single transaction. If any stage of the transaction fails, all state changes are reverted and tokens remain in their corresponding Uniswap pool.

Consider the following illustration showing the buyer can execute the flash-swap transaction with no held tokens. This is because that token value is guaranteed to return to the pool either by the transaction failing or the buyer paying back the tokens borrowed in the same transaction:


A good use of flash swap is arbitrage trading, where the buyer makes a profit and returns the source money to the pool. Another use is the Maker Vault, where you return the funds to take back the ETH-secured funds and deposit them into the pool. This action will cost less than performing the same task using your own funds.

In the use case of trading bots, flash swapping could also be leveraged to automatically perform arbitrage trading. The bot would not need funds in order to execute the trade, being only required to identify the arbitrage opportunities and execute the flash-swap transaction.



New 0.05% Protocol Fee and Governance

Uniswap introduces a 0.05% protocol usage fee. This feature is currently disabled, but can be further activated if decentralized management is an example of a solution to activate it.

The 0.05% commission will be deducted from the standard exchange fee of 0.3%, which means that liquidity providers will lose some of their revenue.

This function is introduced to support ecosystem growth and development, and the decision to include it will be made by the community, which will vote on whether or not to activate it.

If this feature is activated, Uniswap will receive financial support to develop its platform and maintain its competitiveness.

On the up side, such a fee would fund efforts to improve the protocol and overall ecosystem which will lead to further adoption, and therefore more transaction fees to benefit liquidity providers. This theoretically makes up the loss initially introduced by the charge. This idea is summarised by the following illustration:


The positive outcome of turning on the 0.05% protocol charge

To manage the protocol charge mechanism, the Uniswap Factory contract contains feeTo and feeToSetter methods to set the protocol charge wallet address and the account able to set the feeTo value respectively.



JavaScript SDK

The Uniswap JavaScript SDK is an open source library that provides JavaScript APIs for NodeJS, JavaScript frameworks and any browser applications for integration with Uniswap.

For example, The Agent Wallet, Switcheo Exchange and Streamr Marketplace have already integrated their Uniswap into their services and products.

The JavaScript SDK documentation covers APIs for fetching token pair data and pair addresses, in addition to APIs for initiating trades and fetching pricing, that utilises the price oracle function discussed above.

Note that trades cannot be automatically executed, instead these APIs will prepare a trade, taking into consideration the mid market price and the minimum amount of output token you’re willing to settle (also known as slippage).

Once the trade is configured the user must initiate and sign a transaction with wallet software such as MetaMask.



Summary

So we've covered the second iteration of Uniswap, V2. The platform offers a wide range of innovations. We got acquainted with how horses work and what are their advantages. Uniswap is still one of the most popular decentralized exchanges and flawless, because it has never been hacked, no one has succeeded, as well as attack the basing protocol - Ethereum. The ecosystem is developing and the number of miners and validators is constantly growing.

V2 Pros:

- exchange of tokens without an intermediate ETH token
- flash swap, which helps to save on gas
- a price oracle that offers a weighted average price, protecting against high price spikes
- JavaScript SDK to help integrate Uniswap into various services and products
- transition to Solidity smart contracts, using the latest opcodes for efficient execution.

Disadvantages:

- Uniswap v2 is still expensive to use due to high gas prices.
- There is no live order book, so traders have to rely on centralized exchanges.

In general, Uniswap V2 is moving in the right direction, evolving and improving, even despite the introduction of a commission for using the protocol. In general, the Ethereum ecosystem and the defi sector in particular will not slow down in terms of technical capabilities and implementation in the short and long term.

Useful Links:

- Uniswap.exchange
- Uniswap.org:
- Uniswap.Info
- Smart Contract Documentation:
- JavaScript SDK Documentation
- Uniswap on GitHub



The material is taken from the source: Uniswap V2: Everything New with the Decentralised Exchange


        ▄▄▀▀▀▄▄
     ▄▄▀▄▄███▄▄▀▄▄
  ▄▄▀▄▄█████████▄▄▀▄▄
▄▀▄▄█████ ██▄▄▄▄▀██▄▄▀▄
█ ██████ ▄ █████ ████ █
█ █████ ▄█▄ ██ ▄█████ █
█ ████ ▄███▄ ██ █████ █
█ ███ ▄█████▄ ██ ████ █
▀▄▀▀█▄███████▄██▄██▀▀▄▀
  ▀▀▄▀▀█████████▀▀▄▀▀
     ▀▀▄▀▀███▀▀▄▀▀
        ▀▀▄▄▄▀▀

       ▄▄▄▄▄▄▄
   ▄▄▀▀       ▀▀▄▄
  █               █ ▄
 █   █▀▄ ▀█▀ ▀█▀   █ ▀▄
 █   █▀▄  █   █    █  ▀▄
  █  ▀▀   ▀   ▀   █    █
▄▀ ▄▄           ▄▀    ▄▀
 ▀▀  ▀▀▄▄▄▄▄▄▄▀▀      ▀▄
        ▀▄▄      ▄▄▀▀▄▄▀
           ▀▀▀▀▀▀

                      ▄▄▄
  ▄█▄              ▄███████▄
  ▀████▄▄         ██████▀██████▀
    ▀▀▀████▄▄     ███████████▀
    ▀██▄███████▄▄███████████
     ▄▄▄▀██████████████████
      ▀████████████████████
▀█▄▄     ▀████████████████
  ▀████████████████▀█████
    ▀████████████▀▄▄███▀
       ▀▀██████████▀▀
           ▀▀▀▀▀

               ▄▄   ▄▄
              ▄▀ ▀▀█  █
             ▄▀     ▀▀
         ▄▄▄▄█▄
     ▄█▀▀▀▀▀▀▀▀▀▀█▄
 ▄▀▄▀              ▀▄▀▄
█  █   ▄█▄    ▄█▄   █  █
 ▀█    ▀█▀    ▀█▀    █▀
  █                  █
   █   ▀▄      ▄▀   █
    ▀▄   ▀▀▀▀▀▀   ▄▀
      ▀▀▄▄▄▄▄▄▄▄▀▀

   ▄▄████████████████████▄▄
 ▄██████████████████████████▄
▄████████████████████████████▄
██████████ ▀▀█████████████████
██████████     ▀▀█████████████
██████████         ▀▀█████████
██████████         ▄▄█████████
██████████     ▄▄█████████████
██████████ ▄▄█████████████████
▀████████████████████████████▀
 ▀██████████████████████████▀
   ▀▀████████████████████▀▀

   ▄▄███████████████▄▄
 ▄██▀▀             ▀▀██▄
▄██              ▄▄   ██▄
██        ▄▄▄▄▄  ▀▀    ██
██      ▄▀     ▀▄      ██
██     █         █     ██
██     █         █     ██
██      ▀▄     ▄▀      ██
██        ▀▀▀▀▀        ██
▀██                   ██▀
 ▀██▄▄             ▄▄██▀
   ▀▀███████████████▀▀

   ▄▄███████████████▄▄
 ▄█████████████████████▄
▄██████████████▀▀███████▄
████████████▀▀    ███████
█████████▀▀   ▄   ███████
██████▀▀     █    ███████
████▀       █     ███████
█████▄▄   ▄█      ███████
████████ ██▄      ███████
▀████████ ▀▄███▄▄███████▀
 ▀█████████████████████▀
   ▀▀███████████████▀▀

  ▄▄███████████████▄▄
 █████████████████████
███████████████████████
███████████▀    ███████
██████████  ▄██████████
██████████  ███████████
████████      ▄████████
██████████  ███████████
██████████  ███████████
██████████  ███████████
 █████████████████████
  ▀▀███████████████▀▀

   ▄▄████████████████▄▄
 ▄█████▀▀▀██████▀▀▀█████▄
▄████▀  ▀▀▀    ▀▀▀  ▀████▄
████▀                ▀████
███▀                  ▀███
███       ▄    ▄       ███
██▀      ███  ███      ▀██
██       ▀█▀  ▀█▀       ██
██▄     ▄        ▄     ▄██
▀██▄     ▀▀▄▄▄▄▀▀     ███▀
 ▀███▄▄▄▄▄▄████▄▄▄▄▄▄███▀
   ▀▀████████████████▀▀
1618329894
Hero Member
*
Offline Offline

Posts: 1618329894

View Profile Personal Message (Offline)

Ignore
1618329894
Reply with quote  #2

1618329894
Report to moderator
1618329894
Hero Member
*
Offline Offline

Posts: 1618329894

View Profile Personal Message (Offline)

Ignore
1618329894
Reply with quote  #2

1618329894
Report to moderator
1618329894
Hero Member
*
Offline Offline

Posts: 1618329894

View Profile Personal Message (Offline)

Ignore
1618329894
Reply with quote  #2

1618329894
Report to moderator
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction. Advertise here.
1618329894
Hero Member
*
Offline Offline

Posts: 1618329894

View Profile Personal Message (Offline)

Ignore
1618329894
Reply with quote  #2

1618329894
Report to moderator
Shef198911
Member
**
Offline Offline

Activity: 1022
Merit: 54


View Profile
September 17, 2020, 09:11:24 PM
 #2

uniswap is really gaining a lot of popularity, and every time it surprises, only today's airdrop has brought a lot of hype, and I think this will not be the last surprises
livingfree
Hero Member
*****
Offline Offline

Activity: 1666
Merit: 525


View Profile
September 17, 2020, 10:36:34 PM
 #3

I'm still looking for easier materials and sources to know more about this Uniswap. It's gaining the traction and becoming more popular these days and this is thread that what I'm looking for.

This is a very helpful thread for people like me who are still wanting to know more about Uniswap and why the community is talking about it. Thank you ratimov!
beatsnnuts
Newbie
*
Offline Offline

Activity: 3
Merit: 0


View Profile WWW
September 26, 2020, 07:46:05 AM
 #4

Please suggest which platform is better, Binance BSWAP or Uniswap. I have seen various posts about Binance being more secure. please suggest.
wyvern2
Member
**
Offline Offline

Activity: 85
Merit: 10


View Profile
September 26, 2020, 05:07:32 PM
 #5

Please suggest which platform is better, Binance BSWAP or Uniswap. I have seen various posts about Binance being more secure. please suggest.
Dont know about that but no system is safe lol

Does Binance bswap have a fewer fee than uniswap?
I've tested today swap for like $50 and the total fees were like $3
livingfree
Hero Member
*****
Offline Offline

Activity: 1666
Merit: 525


View Profile
September 27, 2020, 10:15:17 AM
 #6

Please suggest which platform is better, Binance BSWAP or Uniswap. I have seen various posts about Binance being more secure. please suggest.
Dont know about that but no system is safe lol

Does Binance bswap have a fewer fee than uniswap?
I've tested today swap for like $50 and the total fees were like $3
That's a lot of fee. They're still affected due to the high fees the Ethereum network has. Binance will always be binance so it's safe unless there will be another batch of hacking from them. But I don't think it will happen as they're continuing to strengthen their security.

As their website says; https://www.binance.com/en/swap

"low fees"
AthenaBanana
Copper Member
Jr. Member
*
Offline Offline

Activity: 392
Merit: 1


View Profile
September 27, 2020, 02:46:06 PM
 #7

Thank you for this post this really made me understand Uniswap V2 and I thought there is no difference with dex exchange.

masulum
Legendary
*
Offline Offline

Activity: 1106
Merit: 1224



View Profile
September 30, 2020, 11:26:49 AM
 #8

Please suggest which platform is better, Binance BSWAP or Uniswap. I have seen various posts about Binance being more secure. please suggest.

For more varied swap needs, of course Uniswap is an option. Because for Binance itself still has fewer options. In addition, we also don't need to register an account to swap just need to connect metamask. but you need to make sure the domain you visit. the official uniswap domain is vary.
1. Uniswap.org
2. Uniswap.exchange
3. Uniswap.info
4. Uniswap.io

And one forking by Kleros team, Uniswap.ninja

Just remember, i think better is nothing, everyone have own decision, you can do a research to find the right service for you.

.freebitcoin.       ▄▄▄█▀▀██▄▄▄
   ▄▄██████▄▄█  █▀▀█▄▄
  ███  █▀▀███████▄▄██▀
   ▀▀▀██▄▄█  ████▀▀  ▄██
▄███▄▄  ▀▀▀▀▀▀▀  ▄▄██████
██▀▀█████▄     ▄██▀█ ▀▀██
██▄▄███▀▀██   ███▀ ▄▄  ▀█
███████▄▄███ ███▄▄ ▀▀▄  █
██▀▀████████ █████  █▀▄██
 █▄▄████████ █████   ███
  ▀████  ███ ████▄▄███▀
     ▀▀████   ████▀▀
BITCOIN
DICE
EVENT
BETTING
WIN A LAMBO !

.
            ▄▄▄▄▄▄▄▄▄▄███████████▄▄▄▄▄
▄▄▄▄▄██████████████████████████████████▄▄▄▄
▀██████████████████████████████████████████████▄▄▄
▄▄████▄█████▄████████████████████████████▄█████▄████▄▄
▀████████▀▀▀████████████████████████████████▀▀▀██████████▄
  ▀▀▀████▄▄▄███████████████████████████████▄▄▄██████████
       ▀█████▀  ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀  ▀█████▀▀▀▀▀▀▀▀▀▀
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
.PLAY NOW.
beatsnnuts
Newbie
*
Offline Offline

Activity: 3
Merit: 0


View Profile WWW
October 03, 2020, 11:24:11 AM
 #9

Please suggest which platform is better, Binance BSWAP or Uniswap. I have seen various posts about Binance being more secure. please suggest.
Dont know about that but no system is safe lol

Does Binance bswap have a fewer fee than uniswap?
I've tested today swap for like $50 and the total fees were like $3
That's a lot of fee. They're still affected due to the high fees the Ethereum network has. Binance will always be binance so it's safe unless there will be another batch of hacking from them. But I don't think it will happen as they're continuing to strengthen their security.

As their website says; https://www.binance.com/en/swap

"low fees"
That is unlikely, I am providing liquidity for the BUSD/USDT pair and binance charged $.63 for 2100 USDT. Please check again.
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!