New Ethereum talks, every Monday. The week's conference uploads by event, in your inbox.

Loading player…

AAVE Live Coding Session - Emilio Frangella

Ethereum DenverSat, Oct 9, 2021, 12:00 AM

This talk is from ETHDenver & ColoradoJam , the world’s largest Web3 BUIDLathon and Community Gathering. ETHDenver’s content is crypto and blockchain focused. Learn more: ethdenver.com Subscribe to our channel to learn more + connect on Twitter: https://twitter.com/EthereumDenver. Speaker: Emilio Frangella This talk was recorded Sunday, February 7, 2021. ETHDenver 2021’s content themes included DeFi & Money Legos, NFTs & the Creator Economy, ColoradoJam & the Next Frontier of Statehood, Protocols & Interoperability, Open Source & Public Goods, The Ownership Economy & Digital Orgs, Usability Design & Adoption and Legal, Privacy & Identity. Industry experts also weighed in on topics like decentralization, privacy, identity, non-fungible tokens, scaling, governance, diversity, and arts and culture in blockchain technology. The 2021 event was February 5-12 and hosted virtually in 2021 with 5000+ blockchain enthusiasts, developers and community members from 80 countries and 50 U.S. states. 🔴 SUBSCRIBE ETHDenver’s YouTube https://www.youtube.com/channel/UCgz9NU06t_FkT0Py1Vadczw 💡 LEARN ETHDenver’s Official Website https://www.ethdenver.com/ 👋 CONNECT ETHDenver Twitter https://twitter.com/EthereumDenver 📖 READ ETHDenver News on Medium https://medium.com/ethdenver 🛍️ SHOP ETHDenver Official Merchandise bufficorn.com 💬 CHAT Telegram https://t.me/Ethdenver Discord https://discord.gg/AxFmNdCVnU 📱 FOLLOW LinkedIn https://www.linkedin.com/company/ethdenver Instagram https://www.instagram.com/ethdenver/ Facebook https://www.facebook.com/ETHDenver/

Transcript

okay so we're gonna move on to um our next speaker guest i think we're ready we've got ave emilio hello everybody am i live yes apparently yes amelia hey john i'm giving you a couple like an extra minute how about that oh that's amazing so we're gonna do amazing session i see what sorry we're going to do a live coding session uh well to be honest i wanted to like touch an argument that is maybe a little bit more complex so there might be not much real coding live like more showing how it works and like a little bit of code description how it actually i prepared an example for the guys with a small bounty there is also a small bounty at the end of 0.2 eth for people that wants to play with it and the first one that will actually get the exercise right they will get a small bounty 30 minutes is a bit um small of a timeline to actually talk what i want to talk about but i think we are going to touch um really interesting argument that is the flashlights everybody i think knows the flashlights is one very hot topic and is something that people want to know about is maybe a little bit more advanced than what i said uh i i saw before about like before was a bit more like entry level there's be there is going to be an explanation of how flashlights work how they work on chain i will demonstrate how an actual flashlight transaction works like by showing the the actual execution of the transaction and then i will show a little bit of code and give the guys reference if they want to take the challenge and win 0.2 eth so cool i think we can start go for it it's all yours uh can you guys see my screen yes yes yeah it's a little bit uh okay let me show the i have a short presentation first i prepared like just to introduce the argument the topic and let's see do you guys see the presentation we haven't we are ready to go right yes yes oh perfect okay so flash loans everybody knows i will go quite quick here because there is no not much time i'll just explain everybody probably heard about flash loans and what flash ones actually are and it was introduced um like ave was not actually the first one to be honest introducing flashlight the concept was lying around for quite a while but nobody actually picked it up on them what we did is actually integrate the concept of flash alone in a protocol that actually has a lot of liquidity available which actually brought the the feature to light for the whole default space uh what that what flash loans are essentially uh we all know we all know that transactions on ethereum they are atomic right so one transaction is either executed successfully in all its uh sub-transactions or it's reverted completely so nothing is actually stored on the on the blockchain right so what flash loans actually do is uh they leverage this automaticity to a allow anybody to take liquidity out of a smart contract with the condition that at the end of the transaction their return this liquidity otherwise the transaction fails okay this is the the the basic concept like people calls them the the schrodinger loans and that's because like the loan exists at the end of the transaction only if the transaction actually um gets executed successfully otherwise if the transaction will fail there is no loan essentially so uh i we have been uh with flash loans for one year and and one a bit a little bit more than one year live the protocol has issued like two billions of actually four billions of flash loans including also the new version of uh ave version 2. uh so i introduced it shortly and quickly the the concept we actually with other version 2 that was released like more or less two months ago we extended the concept of flash loans with uh some uh very cool uh additional features that essentially allow a more a broad use case for a flashlight within ave and also with other protocols so how a flashlight actually work uh we define essentially we have a smart contract on ethereum that is the ave protocol main other protocol smart contract that is called landing pool right everybody that interacts with the other protocol they interact with this contract underneath so this contract allows anybody to deposit funds uh withdraw funds borrow repay basic lending protocol functionalities right and they in this contract also exposes a function called flash loan which is exactly what we are gonna use to interact with the uh with the to actually actually execute our first flashlight okay uh so how flashlight what is the architecture of a flashroom so we have a um as i was saying lending pool contract which has also a flashlight function people that interacts with flashlight usually they call this function here and this function which receives as a parameter the address of another smart contract that is actually the smart contract that will execute the logic of the flash loan that we call a flashlight receiver okay receiver because it's the actual smart contract that receives the funds when the transaction begins right so uh what happens is that we call this function here this function automatically transfers the funds to this contract that is the contract that we deploy it is the contract that does the interaction with the with the protocol and uses the funds in whatever way we programmed it to use right and at the end of the execute operation what happens is that the funds needs to be returned to the leaning pool how the funds are returned it's actually the landing pool that will pull the funds from here so the lending pool if you guys are familiar with the concept of a erc20 approvals what happens is that this contract approves the landing pool to essentially withdraw the funds from the the the uh from the landing pool receiver from sorry from the flashlight receiver that is actually uh the funds that we use for the operation to be to be complete and to be successful okay so this the architecture is quite simple we have this interaction between two two contracts of course the execute operation at some point can become quite complex depending on the operation that the exe the on the actual strategy that execute operation is gonna execute so it might get complex or relatively complex can for example be a series of uni swap swaps and that would be for example trying to catch uh a slippage that raised between different pools in unit swap and execute an arbitrage right can be for example invoking a liquidation on compound so we get the funds from here and we liquidate on compound without i mean actually any fund or can be any other usage of funds that can be executed profit profitably in one transaction this is more or less what flash loans are used for okay what can we build with using flash loan we can build a lot of stuff right now like maybe at the beginning was not so clear people like us a lot of people associated flash loans with uh hacks right and abuses and arbitrages and stuff well yeah i mean they are used there were there was um there was a hack actually not long ago on why iron for example was not really a hack was more like um an abuse of certain uh configuration in a certain condition that uh essentially with which essentially the abuser came out with some profit right so they abused a certain functionality over and over using flashlight they actually used a couple of billions so i think of flash loans in the in within like 30 minutes more or less and they were able at the end of the day to end up with a profit of 2.5 million die so uh to be honest like some people say it's a hack some people say it's um vulnerability some people say it's an arbitrage it really depends on how people [Music] see this kind of actions on the blockchain but they are essentially enabled by flash loans why flashlights because these are actually um actions that usually require a lot of capital and essentially flash loans give capital for almost free right it's a small fee that you need to pay on ave some other protocols there for free like you will need to need to pay one way for example returning one way more um on ave the fee is i think if i remember correctly 9bps so in uniswap is 30bps like a swap so yeah what what you will need to do is use this capital as as long as the the transaction is profitable the transaction will succeed so i have prepared a live example but before going to the lab honestly is a live example where i will show some code but it's also an exercise that i will leave for you guys and there is a price at the end like i prepared as a small contract that you can interact with using flash alone in mainnet i put like 0.

2 eth in it that is there is [Music] a small attacks not so small actually attack surface on this contract that if you discover this attack surface you can empty the the the uh the strategy the the contract that i deployed using flash loans so if you guys want to play with it and you want to try to earn a 0.2 eth then you can experiment a little bit and uh like extend this the example that i will show you later and like maybe you will win like 0.2 eth try to do it while the gas cost is low because otherwise you will end up like probably paying more in transactional costs than than the 0.28 but it's still an interesting um exercise for you guys so but before doing that i want to to show what actually a flashlight looks like in real life okay so i can show you can still see my screen right yes anybody okay perfect okay i will show for example a an actual flashlight transaction that is executed uh from our front end okay from our client interface uh let me see if you go on ave essentially if you go on v2 we have added some functionalities for example you can borrow using a certain collateral and then you can actually repay your debt you don't need actually the the the principle that you are borrowing but you can repay for example with the collateral this is usually useful for people that leverages usually so what happens is that people for example they deposit it they borrow die they buy more if they deposit again enough they borrow more diet they buy more heat and that's a loop that is repeated until essentially you have no more borrowing power right this is uh leverage okay essentially people leverages gets more exposure to it using this strategy and usually they end up without any principle like they don't have any dye to for example if they use dye to leverage it they end up without any dye to actually repay the debt and what they do usually if they want to profit that take profit or they want to to exit the position because for example the price of it is going down it's not not profitable anymore to keep the um leveraged position they usually use the same the collateral itself to repay how this works uh how does this work this works using flashing okay so essentially the the flow of the operation is the protocol the the client the front end executes executes a flash loan that this flash loan uses a flash borrows the amount that the user wants to repay in stable coin it repays the debt which frees up the collateral then it takes the collateral it's it it withdraws from abe it takes the collateral goes on unis what sells for the principal and repays the flashlight this is the execution i can show you guys an actual transaction of this happening these guys this guy for example is uh using usdc to leverage either okay or maybe this is a i know this is the opposite actually the guy was short eater and since today the price of either was going off it was going down the guy essentially closed the position using the repay with collateral which used flash alone under the hood right so let's try to see what is the second sequence of area of operations here and let's try to reconstruct the flow so you can see that the last action is a flash alone because the event is emitted at the end okay that is actually the first action so the the the front end says tells to the protocol okay flash borrow 25 000 almost 26 000 usdc and send to this contract here this contract no sorry this contract here this contract is the contract that we brought that does they actually repay with collateral action okay so it takes 26 000 usdc sends to this contract this contract takes the usdc goes to uniswap and sells for ether then it supplies and then it supplies to the protocol ether and then essentially it uses the heater to repay the the the flashlight this is the actual execution of the flow okay so uh i want to go a little bit more in depth in the in the in this transaction and actually showing all the steps that are executed during this particular operation okay so i use this tool guy guys if you are just starting with solidity or you are more experienced developers you probably if you are more experienced developers you probably know tools that allow you to to debug the transaction and to see the actual execution flow of the transaction this is personally a tool that i love and i use a lot to to analyze transactions and debug transaction and actually see the flow of the transaction it allows you also to see each individual sub-transaction of the of the interaction with the blockchain it's really really powerful it's called tenderly and if you want to experiment for personal use is free so i really guys recommend if you want to explore a complex transaction or just experiment a little bit with it it also allows you to simulate uh transactions in mainnet so it's really really powerful it's really recommended and more importantly is free for personal use so let's see this is the this is the address that this caller that called the transaction uh let's see if it shows the transaction this is the transactional hash i will send maybe in the chat later or i will add to the document at the end so when i will share the document you guys have this transaction if you if you also want to check uh you can see that essentially like i was saying the first action that is invoked on the protocol on the on the smart contract that is the lending pull smart contract is a flashlight right so like i was saying and if we check the parameters of this flash loan we will see that here the receiver address is the address that of the contract that i specified before that is actually the contract that will execute the operation and is that 0x63a3f and so on the asset is the address of the usdc the amount is the amount of usdc in with the decimals with the series dc are six decimals so it's 25.76 k the mode of the flashlight uh it's a part in particular configuration of uh other flashlight version two it has different modes but there is no time to explain right now if i will share more documentation uh at the end if you guys want to i want to explore a little bit more in that the other flashlight version two and there are also other patterns that these palms are essentially essentially used by the they are compiled by the front end and pass it to the contract that we wrote and essentially in here there is mostly the configuration of the swap that we expect to execute including the path uh to execute the swap on on uh uniswap you know that uni swap has multiple pools and usually to get the best rate is not enough to just go and try to swap on one single pool but you will need to actually tell the uniswap contracts specifically the uniswap router contract which pulls which path across all the pools the the trade should take to get the least slippage possible and this is calculated of chain right so our client essentially calculates of chain the path that the unit swap needs to follow to sell usdc for it right and so we compile all this information and we pass this as params all the details are in here here essentially it's a series of arrays we're not gonna be not gonna put too much detail on it but just to give you a heads up on how it works so back to overview um as you can see so flash alone is the first action that is actually calling the flashlight function on landing pool and here the transfer underline two is essentially transfer transferring the funds from the linum from the lending pool to be more precise in version two is from the a token contract of usdc that essentially holds the usdc funds to the caller if we go here and check the transfer underline two let's check the parameters you see that target is the address that we saw before that is the target that actually executes the operation of the flash loan and the amount is the amount of usdc that we the we actually defined the client actually front-end defined when called the flashlight okay so this is the actual operation that takes the fund from out of the protocol and transfers to to the uh a contract that will actually execute the logic of the flashlight let's go a little bit further and we will see that at some point uh here it is there is the execute operation on landing pool which actually calls which is called actually on the um let's see if it shows here where it's actually called this is the execute operation that is called on the contract that was specified before that is the contract that they received the funds let's see if it shows the uh no it doesn't show the target unfortunately maybe it shows here let's see now it doesn't show the target the target is the address of the contract that we saw before it's weird because i usually should show okay but it doesn't show no problem you will need to trust me that is this contract is actually the address of the contract that we saw before and is the address of the contract that receive the funds okay this is called by the landing pool so once this is called where is it once this is called the flow of the execution passes on the destination contract and actually the actual execution of the of the operation starts you see that [Music] it executes this swap liquidity operation that is actually the operation of is swapping the usdc for if that we saw before in the list of the transaction okay let's see if at the end i can show here there is all sort of operation and calculations done to execute the swap uh the withdrawal from other version two and they're ready redeposit the new currency uh in is still in other version two let's see if i can find the actual operation that essentially enables the uh the protocol yeah here it is at the end of all this huge execution will all the operations and the sub operations uh related to swap with uniswap withdrawal from a redepositing of it there is this safer proof that is executed by the contract and this is safer proof it should be i hope is the safer food that i hope it is uh yeah this is the address if you go to main minute i will link the contracts afterwards this is the address of the lending pool contract so what what's what is happening here is that we are at the end of the execution and the the contract that executed the operation and executed as what at the end reapproves the lending pool to say okay i've done i have done everything i needed to do with my funds you can take your funds back and your funds that will be will be pulled from by the landing pool are the funds sent at the beginning there are 27.

uh six not 25.7 k more or less plus a small fee that is the nine bps fifth that i described before okay uh okay this is the end essentially of the operation that i was describing and at the end there is uh a sequence of actions this is the essentially the final part of the flashlight functions with where all the accounting of the operations is being made so uh the state of the learning pool is updated to say hey i received a little bit more liquidity that is the 9bps that i given so i have given out before so i need to account for that particular liquidity right so this is all the operations that the lending pool does to actually account for this kind of information and for the fact that a flashlight was executed and more funds that was that wasn't taken at the beginning uh were returned at the end and that's it this is the execution it might seem very very complex under certain aspects it is but at the end of the day the code itself is not so and the logic itself is really simple it's just taking the liquidity out using that liquidity to repay the debt or to swap uh to a certain asset and then taking that asset already depositing other protocol that's it it's super super simple okay uh do you it doesn't anyone have questions like [Music] does anyone like i've curious any curiosity about this execution flow and if you guys want to see something in particular apparently not okay uh okay then i will go forward i am seven minutes left right that's correct that's correct okay perfect i will show quickly the uh code that i prepared i will explain what this code is for the for the uh what is the goal of the code that they wrote like what is the um experiment and the uh what i want you guys to achieve and then i will be available of course on discord if you want to contact me on twitter i put the handle on on the presentation i also have the same handle in telegram so if you guys want to contact me i will be always available so let's show let's uh show quickly the code can you guys see my screen with the remix yes yes okay perfect so um i recommend you guys especially if you are beginners like to use remix is a quick and easy tool to write solidity code to experiment with using um using the built-in javascript machine you can also deploy a deploy to testnet to mainnet and you can call mainnet function is really easy to use of course this might be not so suitable for big projects but it's perfect for code samples and uh to try out stuff okay so uh what is what is the idea and what i built for you guys to experiment with i built essentially a um contract this the demo flashlight receiver that is essentially an implementation of the receiver that i was talking before and then um essentially is the contract that is called using flashlight and execute and they execute the actual actual flash run code receives defense and executes the actual uh flash run code okay so what this flash loan receiver do the idea is that you guys will write the code that goes here and essentially what should this flashing receiver do is calling another contract that i already deployed in mainnet right and i there is in the in the github that i prepared there is the address and all the instructions to call this particular contract this contract essentially this this one is the fake arbitrage arbitrage strategy essentially is a contract that i brought to simulate uh an arbitrage where you use certain a certain amount of fund and you earn a certain profit right it's actually i called fake arbitrary strategy because it doesn't do actual any actually any arbitrage what i did is simply transfer to this contract a small amount of funds this 0.2 eth and uh essentially when you call this function passing some if uh passing some funds to it it will return the the the same funds that you did you sent to the contract plus 9 bps that is exactly the amount that you will need to pay the fee this will essentially allow you guys to execute a an actual flash loan successfully and see what it feels like inter interacting with uh flashlights so uh the core of this contract is the arbitrage fund is the arbitrage function that you guys willing to call this arbitrage function is a payable function so it requires that you um to to work properly it requires that you guys send some value you see here message value the value needs to be bigger than zero but at the same time the value needs to be smaller than 0.01 ether this is this is a limitation i put to allow everybody to explain otherwise the the contract would be easily drained so i wanted to put the limit on the amount so you will receive you can use maximum 0.01 eth and you will receive when you call this function the calling contract will receive the amount trans transferred so maximum 0.

01 eth plus 9 bps you see a guy say multiply here by one zero zero zero nine and divide by one zero zero zero zero so the returning amount will be uh nine bps more than the actual amount that it was passed okay this is all that this function do but it's everything that we need to actually execute a successful flash loan on ave so what we you guys will need to do is on the contract demo receiver that i already prepared you will need to finish the execute operation here invoke the arbitrage function on foreign strategy this is everything that you will need to do uh the flow of actions i already put in the github but i will repeat here is essentially [Music] getting the reference the fake arbitrage uh strategy contract may be extracting an interface from the fake arbitrary strategies to call the arbitrage function one cool thing is that uh ave uses weth that is the wrap dth you will need to unwrap which is an additional exercise that you will need to do to actually you will need to use the rapid ddh contract to call the fake arbitrary strategy pass the east that are being returned receive the heat from the fake arbitrary strategy and river up and within the contract the if received to drop this and then approve using rapidity approve the lending pool address that is also specified here to be able to withdraw the amount that you received from the fake arbitrary strategy it's not complex might seem complex to some of you guys with it's not complex at all it just requires a little bit of experiments and we are at the end but as i was saying before there are 0.2 eth in the contract um i put some uh safety guards in the contract but i left on purpose uh kind of a back door in the contract that you if you guys discover you can uh empty the the contract and take it 0.2 eth okay so this is left for you guys as an exercise and that's a bounty so that you are more motivated to try and experiment and actually try to win a small prize in at the end just one recommendation if you find the back door very quickly please wait a little bit i know that like you will get probably front front but at least like let other guys also experiment with it and don't drain the contracts immediately just for the for the um for the benefit of everybody i think we are out of time right you are out of time amelia you did good though you did really good in 13 minutes i tried i i tried i wanted really to do to challenge something that is a bit more complex to be honest it requires way more than half an hour but i think that maybe even like if you guys experiment with it with it even for more and more like less experienced the solidity developers it can it can be a good challenge and it also will allow you to understand one of the most uh famous and infamous defy primitives for sure well thanks again it's good seeing you thank you thank you thank you john and i i loved this time with in denver hope we can see in person soon yeah we'll see you soon bye now bye bye

Automatic transcript — names and jargon may be misspelled.