In the second module, we're going to be talking about decentralized exchange or DEX. Most of the discussion will be focused on the leading DEX, which today is Uniswap. What is Uniswap? We've actually been introduced to Uniswap in the previous course in terms of defy preeminence. I gave you an example of a constant product automated market maker. Uniswap is basically the prime example of the automated market maker on the Ethereum Blockchain. We will talk about version 2 of Uniswap. There is a version 3 that I will also talk about and the differences between version 3 and version 2. But I think it's important to understand version two before actually going to version 3. Again, the example that I used in the previous course used a constant product rule. In the previous course, we had a situation where we had 10 of one coin, 10 Ether, and 1,000 of another coin, let's say USDC. The key was to multiply those two together, and we got 10,000. That is something that will be fixed. The algorithm, so in this formula, the k is going to be fixed. I showed you examples of how this constant product, an automated market maker actually worked and we've got plenty more examples in this module. Again, the product is denoted as k. It's the lingo and DeepAI. This is called the invariant. It remains fixed. The AMM is risk neutral, and what I mean by that is that it is not aware or doesn't really care if you're buying or selling. This is a completely different than traditional finance, where there would be a market maker and that would be a person, and they would definitely care whether you're buying or selling and they'll be different prices. This is a very interesting idea, and it's a very simple idea, and literally just the tip of the iceberg in terms of what can happen in this space. Because we've got this constant product, k equals x times y, we also have the exchange rates that are set as basically just the ratio of the x and the y. Let's go through a number of examples and these are very simple examples, to give you the idea of what's happening. Let's think of a Uniswap pool where we've got two stable coins, DAI and USDC. As with any Uniswap pool, you put in the same value in terms of the pair. These are both linked to the US dollar. Four DAI equals four USDC. In this situation, the invariant or the product of the supply of DAI and USDC is 16. The exchange rate is just one to one. The invariant, you can see how that's calculated. This is where we start. Now this is an example of a pool of liquidity that is really sparse. This is just for an example, we'd never have a pool with so few DAI and USDC. It doesn't make any sense, but for the illustration, let's actually go through and look at the mechanics. Suppose that you wanted to sell four DAI for USDC. That's, we're going to use our DAIs. We've got somebody that wants to sell four DAI. We're going to use the automated market maker. We're going to deposit the four DAI to the contract, and it turns out that you can only withdraw two USDC. Again, look at the mechanism here. An additional four DAI had gone into the contract, so we've got a total of eight. We need to maintain the invariant. The invariant is 16. The only way to maintain the invariant is to have two USDC. Only two USDC are able to come out of the contract when you deposit the four DAI. This is essentially how this works in a very simple way, and you can see that that's a very considerable decrease in value. Notice that the effect of exchange rate here was two DAI for one USDC. Again, this is, we sometimes call a slippage, but this is really the result of insufficient liquidity in the pool. This is an example of something that really doesn't work. The mechanics work, but it doesn't make any sense that it would take two DAI to buy one USDC, certainly not on the open market. Now let's change the example a bit. Let's add some additional liquidity. Even this amount of liquidity is too small, but I want you to see how it transforms the situation. Now let's say we've got 100 DAI and 100 USDC. The invariant now is much larger. Previously we had 16, now we've got 10,000. Let's do the same transaction. Somebody wants to sell four DAI for USDC. Again, they deposit four DAI into the contract, and what they're able to pull out according to the algorithm is 3.85 USDC. You can see the mechanics in the box below, that if you look at the invariant, which is 10,000, we now have 104 DAI, and the only way that we can maintain the invariant is to have 96.15 of the USDC. That's where the 3.85 actually comes from. You can see that that's way different than the previous, that the slippage here is effectively going from four DAI to 40 USDC, the slippage goes from four to 3.85 USDC. It's a smaller amount, but still it's large. Why I don't have the example here, you can probably see where we're going here, that if it was the case that instead of just 100 DAI and like 100 USDC, that we have millions in this pool. The slippage is going to be really, really small. This is really important here, that liquidity is crucial. If there's deep liquidity, it's going to minimize the slippage. The slippage is the amount that the exchange rate will actually be changed by the actual trade that you're executing. It's important to have these for the success of a decentralized exchange like Uniswap, to have sufficient liquidity. In the big picture here, decentralized exchange is a competitor to the centralized exchanges like Coinbase and Binance and Kraken. For the decentralized exchange to give the user a good experience, it's important to minimize that slippage or to maximize liquidity in the pool. Indeed what Uniswap does is, incentivizes depositors to supply the capital. You provide capital, you're incented, there's lots of capital. There's minimum slippage and the decks becomes much more competitive or even superior to the centralized exchange. Then notice that the liquidity provider is adding to both sides of the market, when you set this up, you put an equal amount of liquidity at both sides. There's a number of different layers in terms of the importance of liquidity, basically when additional supply comes in, this will increase the liquidity and decrease the slippage. The higher the invariant is, then the lower the slippage. You can see, you can indeed graph it out to show the amount of slippage as a function of the invariant. The invariant, you can think of it differently, is a direct measure of liquidity and this particular protocol. Within Uniswap there are some fees, there's a 0.3 percent fee and that fee is paid back into the pool. This is a fee that those that are providing the liquidity, providing the supply, they're going to earn the fees based upon their contribution to the liquidity pool. You can imagine what's happening here, there can be lots of trading on both sides, buying and selling, and the more activity the more fees that are being generated, and the more that goes to the suppliers of the liquidity. Ideally, if you're supplying liquidity, you would like to actually go to a pool that's got a lot of volume, a lot of turnover. This mechanism is very similar in terms of what Uniswap does to the cToken that we discussed with compound, you're providing liquidity that needs to be a share of the pool and to see, we've got Uniswap own version of this. Think of a DAI, Ethereum pool as having a Uni token that is DAI/ETH. It's a little different, but the same basic idea, in terms of the Uni token.