Okay, so now let's talk about Automated Market Makers. They are very important feature of the DeFi landscape. This is a smart contract, and it deals with both sides of a trading pair. It quotes prices automatically and executes within the contract according to a rule. The rule could be simple, and we'll talk about a very simple constant product rule to start with or it could be much more elaborate. This is something that you could use to buy or sell at any point in time. This is constantly operational. The Automated Market Maker doesn't really care if you're buying or selling, it's just an algorithm. Let's talk about Automatic Market Makers, and I'll go through various different levels of complexity. One would be a fixed price ratio between two assets. This is very, I call it naive, and the reason is that if you have a fixed price, and we talked about this a little earlier, where we had just a fixed price between token and ether that, if it was the case that there was a decrease in the value of token in other markets, then token basically would be bought in another market and then deployed to the Automated Market Maker and all of the Ethereum would be drained from the contract. People effectively buying token cheap and then using that cheap token to get Ethereum. That is pretty naive because it's going to lead to the drain of the actual contract. The contract would have token in it, it would have Ethereum in it, and in the end, it would just have token in it. There's usually a pricing function that's used, and we'll go through a number of examples of pricing functions. Again, this is, I think a beautiful idea and that it is a technology that time is irrelevant. It doesn't matter when you want to trade. This is really important for DeFi protocols in general. For DeFi to work, it can't work on a 9:00 to 5:00 basis. It needs to work 24/7. All these contracts. Think of all these contracts that are interacting, and the decentralized exchange is just another example of a contract that we can interact with. It is interesting also, you don't have to give up custody of your funds. I would be transferring money to Coinbase, wait three days and then the transaction actually takes place. In this situation, I hold the funds until the transaction actually occurs, and is very, very efficient. Again, there's another idea here that's important, and this idea that I've mentioned a few times of composable liquidity. We will be taking liquidity like I gave this example of token and ether and actually working with that. We're able to create these trading systems, whereby we take these different tokens that are available on the Ethereum blockchain and easily put them together. This is like a fundamental component of the idea of DeFi Legos. There's advantages, obviously. The trade against the algorithm is great 24/7 is great, but it's important, and especially in these courses, I emphasize the downsides also. What are the risks here? One risk is the idea of impermanent loss. Again, another word for the word cloud of the learning experience, the four courses, impermanent loss. This is the idea of opportunity cost, and I want to go through some examples because this is such an important concept. As prices deviate from the beginning when you seed a trading pool or a liquidity pool, there will be some impermanent loss or some opportunity lost. Let's go through an example here. This is an example, we've got two tokens, token A and token B. They're both worth the same, one Ether. We set up a pool that has got A and B in it. It turns out the automated market maker has an exchange rate of one-to-one. We've got 100 A in the contract and 100 B. Notice the value of A and B are identical in the contract, so the total value of all of the escrow in the contract is 200 Ether. That's the starting point. Now, the world changes and it turns out that A and B both go up in value. That's great news. A, doubles in value. Now, on the open market for trading, so this is outside of the automated market maker, it might be on Coinbase or Kraken or Binance that A doubles in value. Now to buy A, it costs two Ether. B also increases in value. It actually quadruples. It goes from one to four, and so all of this is very good news. The AMM has got an exchange rate of one-to-one. Let's think about that. For a trader, they see the one-to-one and they go outside and buy A on the open market, which let's say is Coinbase. Then exchange the A for B and drain all of the B under the contract. That's basically what would happen here. This is interesting that we've got a contract that we've got 200 A and zero B. The B is gone, remember the B went up by four times. But given that it was offered at parity, then people buy the A in the open market and then send the A to the contract and withdraw all of the B. This is your ended, you provided liquidity and think of the value. In the end, you've got 200 A and the value of that is 400. What is impermanent loss? If you actually made money, you started out when you put the money in the pool, the value was 200 and now it's 400. Well, that seems good. But the idea of impermanent loss is, what if you didn't put the money in the pool? What if you just held the A and the B? Well, if you just held the A and the B, then the value is going to be much greater. Remember, you've got 200 A and the value is 400. This is basically doubling your money. You started with 200, you go to 400. Given that you did the pool. Now, consider the situation if you didn't put the money in the pool, you just held your A and your B. Well, what happens there? We know that A has doubled in value, so it's worth 200. We know that B has quadrupled in value, so it goes from a value of 100-400. If you did nothing, it would have been 600. The idea of the impermanent loss is, I put my money in the pool, and the end value was 400, but if I didn't do anything, the value would have been 600. The impermanent loss here is 200. One thing that's important, the price did deviate from par. This impermanent loss actually become smaller if the price of A increases and gets closer to the price of B. In this example, the impermanent loss is 200. This is an important concept of opportunity cost. This is a very simple example. We're going to go through a Uniswap example in a few minutes that has a different mechanism, it's not a one-to-one exchange. In any situation where you've got a one-to-one, this is a naive AMM, and if there's any deviation from par, then the contract will be drained. That's exactly what happened in this example, where all of the B is stripped out of the contract. Now I want to talk about something a little more general, and talk about impermanent loss with the Uniswap version 2, automated market maker. Actually version 3 exists today, it was recently released. But version 2 is still operational. I think it's important to understand what's going on with version 2 to fully appreciate version 3, which I deal with in some detail in the third course, the DeFi deep dive. Let's start out. This is fairly simple. We've got market prices, one Ether equals 100 DAI. For the purpose of this course, I'm using simplified numbers. Obviously, the price of Ether is much more than 100 DAI, which is linked to a dollar, which is like $100. But I'm just going to keep it simple and assume the base point is one Ether is $100. Alicia is going to make a contribution to liquidity pool, the automated market maker, and she will put in one Ether and 100 DAI. Notice those are equal in value. She will earn a fee for providing liquidity. Again, an equal amount is deposited. Alicia is not the only one in the pool. Indeed, there are others. The total pool is got 10 Ether and 1,000 DAI. The total liquidity is calculated as the product of the total number of coins and the pairs, so 10 Ether times 1,000 equals 10,000. That's what we'll call the total liquidity. It seems a little strange to do the multiplication, but you'll see that it becomes very useful. Alicia has got 10 percent of the pool, so 10 Ether, she's contributed one, 1,000 DAI, she's contributed 100. Let's go with this. Now, suppose in the open market, the price of Ethereum goes up. It goes up from $100-400. Arbitrageurs see an opportunity. They see the automated market maker is offering one for one. There's an incentive to go into the open market to buy DAI, and then deploy those DAI, send those DAI to the AMM and withdraw Ether and make a profit off of that. This is not going to be like the previous example. The previous example, all the Ethereum were drained from the contract, and I call that a naive AMM. This one is going to be different. It turns out that the arbitrageurs will do what they're supposed to do; they will add DAI to the contract, they will withdraw Ethereum, but at a point they stop. The point that they stop is when there are five Ether left in the contract. The contract will end up with five ETH and 2,000 DAI because there's a formula. The formula in the smart contract says that the product of the total supply must be 10,000. In this example, when we go to five ETH with 2,000 DAI, the product is 10,000. Notice that the ratio is exactly the market price, the 400 DAI for one ETH. Essentially, this automated market maker has adjusted to actually pick up on the market price. Here's a little more detail, and this is what the constant product automated market maker looks like, where you multiply the supply of the DAI times the supply of the ETH, you get that 10,000, and that 10,000 is constant, and this is called a constant product automated market maker. Notice the first line here where we've got in the pool, what we start with, 10 ETH, I've got 1,000 DAI, product, 10,000. You can see the price of ETH in terms of DAI is 100. As DAI are contributed to the contract, the price changes. You can see that what happens is that the product remains 10,000 no matter what. You can see on the far right column that the price of ETH in terms of DAI increases until it gets to 400, which is the market price. This is where we stop, and again, the product of 5 times 2,000 is 10,000. This is how this particular automated market maker actually works. Let's think about what Alicia is doing. She owns 10 percent of the pool and she withdraws all her funds from the pool. When she withdraws everything, she's going to end up with a half of an ETH and 200 DAI. The US dollar value is 400, so that means the value of the ETH is 200, and then she's got 200 DAI, which is 200. So the total value is 400. Her original investment was 200, so she's doubled money, which is good, but let's again think of the impermanent loss. If she didn't put her funds, if she didn't put her ETH and the DAI into this automated market maker, what would it be worth? Well, she had one ETH and that is worth $400, she had 100 DAI, that's worth $100, so that's 500. The impermanent loss here is 100, so 500 minus the 400. We're simplifying here because we're not accounting for any fees in this particular example, but you get the idea that this is a more sophisticated, automated market maker. There's still impermanent loss that happens, and this is a disadvantage of this particular technology. For any shift in price, there will be some permanent loss. It is path-independent, so it doesn't matter if there's one trader or 100 traders. It's completely irrelevant. The impermanent loss is calculated the same if there's one or 100 different traders. It turns out that the most attractive pairs to have for AMM are pairs that have correlated prices, so you don't see a lot of deviation , so-called mean-reverting pairs. You don't get one token greatly deviating from the other token. For example, stablecoin pairs are very attractive for AMMs because the impermanent loss is really small.