Welcome to the second course in the learning experience, DeFi and the future of finance. The second course we're going to talk about DeFi primitives. These are the fundamental building blocks of DeFi, and it's really important to understand them to fully appreciate the potential of decentralized finance. We've already gone through in the first course, DeFi Infrastructure, and that introduced some of the key concepts that are important for, not just DeFi, but blockchain technology in general. In this course, we will talk about the mechanics in the first module , of decentralized finance. The idea of supply and how supply is varied in terms of an increase in supply and decrease in supply. Ownership. We'll talk about loans, in particular, collateralized loans, and swaps, which is basically exchange. Then at the end, I've got a module that talks about joining the world of DeFi, and we'll set up a wallet using MetaMask. Then near the end, what we'll do is review the blockchain tech big picture, to go through the key general concepts, so we're all on the same page. In the third course, which comes next, we'll do a deep dive into the existing protocols that are very popular in terms of DeFi today. The final course, course number 4, focuses on the risks and the opportunities of decentralized finance. Let's start with the mechanics. We'll talk about transaction mechanics, we'll talk about fungible tokens. We've already introduced fungible tokens in terms of the ERC20 in the first course. But remember, I said in the first course that there'll be a number of opportunities to revisit some of the concepts that I introduced in the first course, and this is one of the opportunities. Of course, we will also talk about NFTs or non-fungible tokens. Let's get started with the mechanics. Transactions in Bitcoin and Ethereum are different because Ethereum offers something extra. I mentioned this in the first course but really want to emphasize that here. There are two types of accounts or addresses in Ethereum. One is an Externally Owned Account or an EOA, and that's basically what you're going to set up at the end of this course using a MetaMask wallet. You will have an address. I've got an address also, I will share my public address with you. Indeed, we will talk about how that address is actually derived today in considerable detail. With this externally owned account, it is very straightforward for me to send some Ethereum or any token that's associated with the Ethereum blockchain from one address to another address. It might be that I'm sending to you, but it might be I'm sending to myself because I can have an arbitrary number of addresses. Indeed, think of it as a physical wallet, where you've got two physical wallets and you can take some paper currency out of one wallet and put it into the other wallet, and that's same thing happens in the world of decentralized finance, but obviously it's all virtual. However, Ethereum, and this is where we depart from the Bitcoin world, there's more, so it's not just externally owned accounts. There is a possibility of a contract account. This is a smart contract that lives on every node in the Ethereum blockchain. In that account you can actually send funds to, and that account can disperse funds. It is an algorithm that's running that is very important. This is a key distinction between Bitcoin and Ethereum. There's nothing like this in Bitcoin. All we can do is to use Bitcoin as a transaction mechanism to transfer fund. That's basically it for the implementation of Bitcoin that exists today. Ethereum is much richer. This is a different level, and this level is going to be really important. Basically, it's also possible that when you send a transaction to a smart contract, it potentially can go from that smart contract and interact with many other smart contracts. Is going to be important for us? It's not just you send something to the contract and maybe the contract sends something back to you. No. It's possible with this idea that I introduced in the previous course of DeFi Legos, that this contract is there, and when you send a transaction to it, it might have many different steps. You start by interacting with one contract, so that's the genesis. But then it's possible from that point to interact with many other contracts. This is going to be important, in particular for trading and arbitrage and refinancing of loans and things like that. Disability to interact with multiple contracts even though you start with one. Another property that is going to be crucial and very important to understand is the idea of atomicity. What do I mean by that? These contracts are atomic. This is what actually happens. Let's say I've got a transaction that has multiple steps in it. There's multiple different components, so I'm going to be interacting with many different smart contracts, so it's got many different layers to it. Let's say I've got 10 steps, and I go through the first eight steps, no problem, but there is a problem with the ninth step, so the contract fails, and the transaction fails. That means that we actually revert to the original state. Think about the difference here between traditional finance and decentralized finance. Maybe you've got a wire that's going to the UK and then it's going to, let's say France, and then it's going to hop to Hong Kong. Suppose is no problem that the wire goes to the UK, and there's no problem of going to France, but then something happens. There's a problem with the last step going from France to Hong Kong. Well, your funds are stuck in France again, and you need to get them back somehow, and that's going to be a time costly effort, and who knows exactly what happens. In decentralized finance. No. If you're transactions got 10 steps and something fails on the ninth step, then you revert to the initial state. This is going to be particularly important and it will preview a concept that I introduced in the first course, this idea of a flash loan. It's going to be very important. Think about taking a loan out in the first step that you're going to pay back in the last step. I will argue that loan is pretty well risk-free. It's risk-free because if there is any problem with any of the intermediate steps, you revert to the initial state before you took the loan. Having this atomic property is really crucial for the functioning of decentralized finance, so think of it as funds moving between many different contracts, exchanging hands. You know when this is happening, that if anything goes wrong, then you've revert back to where you began. This is again, a very powerful idea and crucial to defy. In the first course on defy infrastructure, we did introduce this idea of gas. Gas is an important concept. The cheapest in terms of the amount of gas that you need, is just a simple transaction between two externally owned accounts, just a simple peer-to-peer transfer, let's say of Ethereum. Again, it's really simple. It doesn't take much computing power to do that. That computing power is directly quantified into the amount of gas that you need. All computing power that's used on Ethereum blockchain has got a calculation in terms of the amount of gas that's necessary to actually do it. This is why Ethereum is a distributed computational platform. You're actually running some code. That code might be really simple, and it wouldn't take much gas, or it might be more complex. You can imagine a transaction going to a smart contract that has got multiple components to it, where you interact with many different smart contracts. That's a much more complicated transaction. It will take more gas. Of course, the cost of running this is the amount of gas, which equals the amount of computing power multiplied by the price of gas. The price is set competitively in an auction. As I said in the first course, that if the network is very congested, a lot of people are active, then the price of gas will be bid up, making it more expensive to do any transaction, and especially a very complicated transaction. Just let me review a few important things that if you calculate the amount of gas and send more gas than is needed to do the transaction, then the residual is transferred back to you, the user. You get a refund effectively if not all the gas that you budgeted was used. However, if you don't send enough gas and let's say you've got this transaction with 10 different steps, it gets through the first eight steps, but then runs out of gas. Well, that's what I was talking about in terms of something failed. Something fails in that transaction, and what happens given that this is atomic, we revert back to the initial state before number 1 through eight were actually executed. In this case, there's no refund, and there's no refund for a simple reason. You actually use the computing power of the network to get through steps 1 through eight. It doesn't make any sense to refund something that you've actually already used. That is an important subtlety of how gas actually works. Again, the gas fee is set competitively in an auction. Gas is an issue, and we have already talked about this in some detail that if the gas price is too high, then it discourages people from doing smaller transactions. Ideally, we want a world of defy where the cost of transacting is really low. Ethereum developers totally understand this and think of this as a short-term problem that they are actively working to solve. Because for Ethereum to really take off as the premier decentralized finance platform, the cost of transacting must be reasonable. There are other competitor platforms currently today that are not using proof of work, or the cost of transacting is far less than Ethereum. There's direct competition and many defy platforms are available on these alternative block chains too. This is something that obviously the force of competition will effectively push Ethereum to the point where these transactions' fees are greatly reduced. Interestingly, for Bitcoin there's no gas fee but say there is a transaction fee, but there is no initiative like the second version of Ethereum going to proof of stake. Bitcoin is stuck in the world of proof of work. Let me talk about something else, which is the memory pool. When a transaction is proposed, it goes into a holding area. This makes sense because we need to verify. Think of a simple transaction where you're sending Ethereum from one party to another party. Well, what does a validation of that transaction look like? Well, the first thing is that a miner needs to gather it into their list of transactions. The first thing they're going to do is to look at the fee that they could actually make in terms of like the gas fee. That's the first thing they'll look at, so gather the transactions. The second thing, they're going to make sure that the transaction is valid before they go ahead and mine a block. If you put an invalid transactions into a block, even if you win the lottery of the hashing, the block won't be recognized because it's got an invalid transaction in it. You need to make sure everything is valid. You pull this stuff out of the memory pool. The memory pool is completely public, everybody can actually see these. This is basically the way that the miners gather the transactions and validate. But there's something else here that's interesting and it is a result of proof of work. The miners actually see all of the pending transactions. Think about that in centralized finance. Suppose that you had insight information that there was going to be a really big buy order for Ethereum. Wow. That's something that you might want to get ahead of it but in decentralized finance, all of these pending transactions are public and what we're really talking about is front running. Again, in traditional finance, you can't do this. This is the illegal. You've got inside information that for example, somebody has got a huge order for IBM stock and then you go buy the stock before that order goes through. That's called front-running. Then when the order goes through, it boosts the price because of the price impact and you make some money. That is the illegal, you're using inside information to front run. What we're talking about here in the world of decentralized finance is legal, front-running. It's legal because it is completely open, anybody can see it. A miner might actually choose to exclude a transaction or put their own transaction ahead of a transaction and front run. Now, interestingly, this only works if you win the blocks, there's some risk to it. But if enough miners are actually doing this, then there's another source of revenue for the miners. It's called minor extractable value. The miner extractable value is the amount of money that they can make by legally front running some transactions. Again, this is something that is sharply different from the world of centralized finance and this is an issue with proof of work. This is one of the drawbacks of proof of work. When we transition to proof of stake for Ethereum, this is much less of an issue. Think of this as another reason to move to proof-of-stake, to mitigate essentially the miners front running the users and when that happens, obviously it makes the user experience a less favorable experience. You can see, and this is a graph that shows that the amount of miner extractable value, or MEV has been increasing through time. These are the basics of transaction mechanics, both the positives and some other negatives with the current technology.