SWAP/DeX and AMM
The ShibaSwap DeCentralized Exchange
Last updated
The ShibaSwap DeCentralized Exchange
Last updated
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
It uses smart contracts to hold liquidity reserves of various tokens, and trades are executed directly against these reserves
Utilises factory/registry contracts (explained below) that can create and register token pairs
Each token pair holds its own reserve liquidity
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
This allows trades between the two based on relative supply
It uses constant product formula
The ratio of tokens in the pool, in combination with the constant product formula, ultimately determine the price that a swap executes at.
It is thus dependant on individual actors called Liquidity Providers to maintain and replenish its reserves who are rewarded for this.
Adding liquidity requires depositing an equivalent value of ERC20 tokens into the ERC20 token pairs' associated pool.
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.
A subsequent trade reduces or increases the ratio of the tokens in the pool and thus affecting the price of the tokens involved.
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.
Since such a system is isolated and does not depend on an order-book for prices an arbitrage trade essentially helps correct the price.
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