SWAP/DeX and AMM

The ShibaSwap DeCentralized Exchange

As explained in this guide from Uniswap, the easiest way to safely swap tokens is to use the router, which provides a variety of methods to safely swap to and from different assets. There is a function for each permutation of swapping to/from an exact amount of ETH/tokens.

It is also recommended by Uniswap here that swaps need access to a price oracle, (Unification) to be sure that the best execution they can get from Uniswap is close enough to what the oracle considers the "true" price.

There are a few things unique about how this is set up

  1. It uses smart contracts to hold liquidity reserves of various tokens, and trades are executed directly against these reserves

  2. Utilises factory/registry contracts (explained below) that can create and register token pairs

    1. Each token pair holds its own reserve liquidity

      1. Each pair is underpinned by a liquidity pool. Liquidity pools are smart contracts that hold balances of two unique tokens and enforces rules around depositing and withdrawing them

    2. This allows trades between the two based on relative supply

  3. It uses constant product formula

    1. The ratio of tokens in the pool, in combination with the constant product formula, ultimately determine the price that a swap executes at.

  4. It is thus dependant on individual actors called Liquidity Providers to maintain and replenish its reserves who are rewarded for this.

Adding Liquidity

  1. Adding liquidity requires depositing an equivalent value of ERC20 tokens into the ERC20 token pairs' associated pool.

  2. The first liquidity provider to join a pool sets the initial exchange rate by depositing what they believe to be an equivalent value of ETH and ERC20 tokens.

  3. A subsequent trade reduces or increases the ratio of the tokens in the pool and thus affecting the price of the tokens involved.

  4. If another trade does not balance the ratio or affects the price, this creates and arbitrate opportunity for an arbitrage trader as the price of one of the tokens involved may have changed at another exchange.

  5. Since such a system is isolated and does not depend on an order-book for prices an arbitrage trade essentially helps correct the price.

  6. The easiest way to safely add liquidity to a pool is to use the router, which provides simple methods to safely add liquidity to a pool

Liquidity Tokens

Last updated