Uniswap Smart Contract Breakdown. Explaining its functionality by… | by Nazar Ilamanov | Feb, 2022

Explaining its performance by grouping strains of code

  • How Uniswap code is organized
  • Uniswap functionalities
  • Core contracts: Pair (laborious)
  • Core contracts: Manufacturing facility (simple)
  • Periphery contract: Router (simple)
  • Totally annotated code

The entire objective of Uniswap is to permit you to swap one ERC20 token for one more. For instance, you want Dogecoin however you solely have Shiba coin. Uniswap lets you promote your Dogecoin and get Shiba in return. That is all finished in an automated and decentralized style. Uniswap is only a decentralized change.

  1. Automated market makers (AMM): There is no such thing as a centralized matchmaker. There are individuals who present each tokens (Dogecoin and Shiba). They’re referred to as liquidity suppliers. These liquidity suppliers create a pool of Dogecoin and Shiba tokens. Now merchants can come and deposit Dogecoin and get Shiba in return. That is finished robotically, with out a centralized entity. Merchants pay a small share charge for the commerce which matches to liquidity suppliers for his or her companies.
From “Uniswap — a Unique Exchange

Uniswap has 3 variations.

  • v1 is simply too easy and doesn’t have all the trendy options.
  • v3 is basically v2 however improved and optimized — its code is far more difficult than v2.

Uniswap has 4 sensible contracts in whole. They’re divided into core and periphery.

  1. Periphery is for interacting with the core.
  1. Manufacturing facility — creates and retains monitor of all Pair contracts
  2. ERC20 — for protecting monitor of possession of pool. Consider the pool as a property. When liquidity suppliers present funds to the pool, they get “pool possession tokens” in return. These possession tokens earn rewards (by merchants paying a small share for every commerce). When liquidity suppliers need their funds again, they only submit the possession tokens again and get their funds + the rewards that had been accrued. The ERC20 contract retains monitor of the possession tokens.

We talked concerning the 4 sensible contracts that Uniswap has and the way they’re organized. However what’s the principle performance that these contracts implement? The principle performance is the next:

  1. Capabilities for liquidity suppliers — deposit extra funds and withdraw the funds together with the rewards
  2. Capabilities for merchants — swapping
  3. Managing pool possession tokens
  4. Protocol charge — Uniswap v2 launched a switchable protocol charge. This protocol charge goes to the Uniswap group for his or her efforts in sustaining Uniswap. In the meanwhile, this protocol charge is turned off however it may be turned on sooner or later. When it’s on, the merchants will nonetheless pay the identical charge for buying and selling however 1/6 of this charge will now go to the Uniswap group and the remainder 5/6 will go to the liquidity suppliers because the reward for offering their funds.

Let’s now dig into the precise Solidity code of the Uniswap sensible contracts. We’ll begin with the Pair contract. That is essentially the most advanced of the 4 sensible contracts. The remainder will get simpler.

  • It implements the IUniswapV2Pair interface, which is simply an interface for this contract (may be discovered here). It additionally extends the UniswapV2ERC20 contract. Why? For managing the pool possession tokens. We’ll study extra about it later.
  • SafeMath is a library for coping with overflow/underflow. UQ112x112 is a library for supporting floating numbers. Solidity doesn’t assist floats by default. This library represents floats utilizing 224 bits. The First 112 bits are for the entire quantity, and the final 112 bits are for the fractional half.

Managing the funds

A Uniswap Pair is an change between a pair of tokens resembling Dogecoin and Shiba. These tokens are represented as token0 and token1 within the contract. They’re the addresses of the ERC20 sensible contracts that implement them.

That is how funds are managed throughout 3 sensible contracts
  • _reserve0 and _reserve1 are Uniswap’s beforehand recognized balances (final time balanceOf was checked).
  • All we do on this operate is test for overflow (line 74), replace worth oracle (this might be defined in a later part), replace reserves, and replace a Sync occasion.

Minting and Burning

Now onto the subsequent performance — minting and burning. Minting is when a liquidity supplier provides funds to the pool and consequently, new pool possession tokens are minted (created out of skinny air) for the liquidity supplier. Burning is the alternative — liquidity supplier withdraws funds (and the accrued rewards) and his pool possession tokens are burned (destroyed).

  • We learn the balances of our contract (the Pair contract) on strains 112 and 113 after which calculate the quantity of every token that was deposited.
  • The pink a part of the code is for the elective protocol charge. We’ll look at it later.
  • totalSupply signifies the full provide of the pool possession tokens and is a saved variable within the UniswapV2ERC20 contract (see my breakdown of it here). The Pair contract extends UniswapV2ERC20 which is why it has entry to the totalSupply variable.
  • If totalSupply is 0, it signifies that this pool is model new and we have to lock in MINIMIUM_LIQUIDITY quantity of pool possession tokens to keep away from division by zero within the liquidity calculations. The best way it’s locked in is by sending it to the handle zero. (nobody is aware of the non-public key that can result in the handle zero so by sending funds to the handle zero, you primarily lock the funds without end).
  • liquidity variable is the quantity of latest pool possession tokens that have to be minted to the liquidity supplier. The liquidity supplier will get a proportional quantity of pool possession tokens relying on how a lot new funds he gives (line 123)
  • We lastly mint new pool possession tokens to the to handle (line 126). to is the handle of the liquidity supplier (this might be offered by the Periphery contract referred to as the Router which calls the mint operate)
  • balance0 and balance1 are whole balances of the tokens on this pool. liquidity is the quantity of pool possession tokens that the liquidity supplier (who needs to money out) has. Why do entry the liquidity because the stability of handle(this)? As a result of the liquidity was transferred to the Pair contract by the Periphery contract earlier than calling the burn operate.
  • We calculate the quantities of tokens to withdraw to the liquidity supplier proportionally to how a lot liquidity (pool possession tokens) he has (strains 144 and 145)
  • We then burn his liquidity and switch the tokens to him.
  • Rewards to the liquidity supplier are robotically withdrawn alongside together with his funds. The maths makes positive that rewards are accrued correctly and that you just get greater than you deposited.

More Posts