welcome to the second course in the learning experience defy and the future of finance the second course we're going to talk about d5 primitives so these are the fundamental building blocks of defy and it's really important to understand them to fully appreciate the potential of decentralized finance so we've already gone through in the first course defy infrastructure and that introduced some of the key concepts that are important for not just defy but blockchain technology in general in this course we will talk about uh the mechanics in the first module of decentralized finance the idea of supply and how supply is varied in terms of increasing supply and decreasing supply ownership we'll talk about um loans in particular collateralized loans and swaps which is basically exchange and then at the end i've got a module that talks about joining the world of defy and we'll set up a wallet using metamask and then near the end what we'll do is kind of 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 d5 today and the final course course number four focuses on the risks and the opportunities of decentralized finance so so let's start with the mechanics and uh 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 and of course we will also talk about nfts or non-fungible tokens so so let's uh get started with the mechanics so so transactions in bitcoin and ethereum are different because ethereum offers something extra and i mentioned this in the first course but i really want to emphasize it here so 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 so 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 so 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 and it might be that i'm sending to you but it might be i'm sending to myself because i can have uh 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 the same thing uh happens in in the world of decentralized finance but obviously it's all virtual however in ethereum and this is where we kind of depart from uh the bitcoin world there's more so it's not just externally owned accounts there is the possibility of a contract account so this is a smart contract that lives on every node in the ethereum blockchain and that account you can actually send funds to and that account can disperse funds okay so it is an algorithm that's running that is very important so 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 uh funds okay that's that's basically it for the implementation of bitcoin that exists uh today ethereum is much richer okay so so this is uh a different level and this level is going to be really uh important so um so basically uh it's also possible that when you send a transaction to a uh a smart contract it potentially can go from that smart contract and interact with many other smart contracts and it's going to be important for us okay so it's not just you send something to the contract and maybe the contract sends something back to you no no it's possible with this idea that i introduced uh in the previous course of d5 legos that this uh a contract um is there and when you send a transaction to it it might have many different steps okay so 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 okay so this is going to be important in particular for trading and arbitrage and refinancing of loans and and things like that this ability to interact with multiple contracts even even though you start with one okay so another property that is going to be crucial and very important to understand is the idea of atomicity so what do i mean by that so these contracts are atomic and this is what actually happens so let's say i've got a transaction that has multiple steps in it there's multiple different um different components so i'm going to be interacting with many different smart contracts so it's got many different layers to it so one key feature is that 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 so that means that we actually revert to the original state okay so think about the difference here between traditional finance and decentralized finance so 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 and suppose there's no problem that the wire goes to the uk and there's no problem going to france but then something happens and there's a problem with the last step going from france to uh hong kong well your funds are stuck in france okay and and you need to get them uh back somehow and that's going to be a time costly sort of effort and uh and who knows exactly what happens in decentralized finance no if your transaction's got 10 steps and something fails on the ninth step then you revert to the initial state okay so this is going to be particularly important and it will kind of preview a concept that i introduced in the first course this idea of a flash loan so it's going to be very important okay so think about taking a loan out in the first step that you're gonna pay back in the last step and i will argue that loan is pretty well risk-free and and 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 okay so having this atomic property is really crucial for for the functioning of decentralized finance so think of it as funds moving between many different contracts exchanging hands uh and and you know when this is happening that if anything goes wrong then you revert back to where you began okay so so this is uh again a very powerful uh idea and crucial uh to defy so in the uh the first course uh on deep infrastructure we did introduce this idea of gas so 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 so just a simple peer-to-peer transfer let's say of ethereum okay that's really simple it doesn't take much computing power to do that and that computing power is directly quantified into the amount of gas that you need so all computing power that's used on the ethereum blockchain has got a calculation in terms of the amount of gas that's necessary to actually do it so this is why ethereum is a distributed computational platform so you're actually running some code and that code might be really simple and it wouldn't take much gas or it might be more complex so 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 okay so that's a much more complicated transaction it will take more gas and of course the cost of running this is the the amount of gas which equals the amount of computing power multiplied by the price of gas okay so uh the the price is set competitively in an auction okay 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 so just let me review a few important things that if you calculate the amount of gas and and send more gas than is needed to do the transaction then the residual is transferred back to you the user okay so 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 kind of what i was talking about in terms of something failed so something fails in that transaction and what happens given that this is atomic we revert back to the initial state before number one through eight were actually uh 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 one through eight so it doesn't make any sense to uh to refund something that you've actually already used okay so so that is an important subtlety of how gas uh actually uh works so again the gas v is set uh competitively in an auction um gas is an issue and and we uh i've already talked about this in some detail that if the gas price is too high then it discourages people from doing smaller uh transactions and ideally we want a world of defy where the cost of transacting is really low and ethereum developers totally understand this and think of this as a short term problem that they are actively working uh to solve because for ethereum to really take off as the the premier uh uh decentralized finance platform the cost of transacting must be reasonable there are other competitor platforms currently today that are not using proof of work where the cost of transacting is far less than ethereum so there's direct competition and many defy uh platforms are available on these alternative blockchains too so this is something that obviously the force of competition will effectively push ethereum to the point where these transactions fees are greatly uh reduced interestingly uh for bitcoin there's no gas fee per se there is a transaction fee but there is no initiative like the second version of ethereum going to proof of stake so bitcoin is stuck in the world of proof of work so let me talk about something else which is the memory pool so when a transaction is proposed it goes into a holding area and this kind of makes sense because we need to verify so think of a simple transaction where you're sending ethereum from one party to another party well what does a a validation of that transaction look like well the first thing is that a minor needs to gather it and uh into their into their list of transactions and the first thing they're going to do is to look at the fee that they could actually make in terms of uh like the the gas fee that's the first thing to 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 so if you put an invalid uh transaction into a block even if you win the uh the the lottery of the uh the hashing the block won't be recognized because getting invalid invalid transaction in it so you need to make sure everything is valid so you pull this stuff out of the uh the memory pool the memory pool is completely public everybody can actually uh see these okay so um so this is basically the way that um the miners gather uh the transactions and validate but there's something else here that's kind of interesting and it is a result of proof of work the miners actually see all of the pending transactions so so think about that in centralized finance suppose that you had inside 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 okay but in decentralized finance all of these pending transactions are public and what we're really talking about is front running okay so again in traditional finance you can't do this this is illegal you've got inside information that for example somebody uh has got a huge order for uh ibm stock and then you go buy the stock before that order goes through that's called front running and then when the order goes through it boosts the price because of the price impact and you make some money okay that is illegal you're using inside information to front run okay so 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 so 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 block so there's some risk to it but if enough miners are actually doing this then there's a another source of revenue for the miners and it's called minor extractable value so the minor extractable value is the amount of money that they can make by legally front running uh some transactions okay so again this is something that is sharply different from the world of centralized finance and this is an issue with proof of work so this is one of the drawbacks of proof-of-work when we transition to of stake for ethereum this is much less of an issue okay so so think of this as another reason to move to proof-of-stake to mitigate uh essentially the miners front running the users and when that happens obviously it it makes the user experience a less favorable experience and you can see and this is a graph that shows that the amount of minor extractable value or mev has been increasing through time so these are kind of the basics of transaction mechanics both the positives and some of the negatives with the current technology