Decentralized Exchange (DEX)
Introduction
A decentralized exchange (DEX) is a peer-to-peer marketplace where transactions occur directly between cryptocurrency traders. Unlike centralized exchanges like Binance, DEXs exclusively trade cryptocurrency tokens for other cryptocurrency tokens, without allowing exchanges between fiat and cryptocurrencies.
Decentralized exchanges are essentially a set of smart contracts. They establish the prices of various cryptocurrencies algorithmically and use "liquidity pools," in which investors lock funds in exchange for interest-like rewards, to facilitate trades.
While transactions on a centralized exchange are recorded in that exchange's internal database, DEX transactions are settled directly on the blockchain.
DEXs are usually built on open-source code, allowing anyone interested to see exactly how they work. This also means that developers can adapt existing code to create new competing projects, as seen with Uniswap's code being adapted by various DEXs like Sushiswap and Pancakeswap.
The exchange uses Vara fungible tokens (VFT) for the tokens and the Gear-lib FT wrapper for the pair to track liquidity.
Math
All prices are algorithmically calculated. Investors provide funds to the liquidity pools, and the price is calculated according to the amount of tokens in the reserves using the following formula: where and are the reserves of token0 and token1, respectively, provided by the investors, and is the constant. All prices/amounts are calculated so that MUST remain constant. This means that the more token0 in the pool, the lower the price of token1 will be when performing a swap.
Factory Program Description
Given the potential large number of trading pairs, there should be a way to monitor and deploy new pairs. This is where a factory comes into play. The factory helps to create new pairs and monitor all existing pairs.