Borrow Millions In DEFI With NO COLLATERAL? FLASH LOANS Explained (Aave, dYdX) - YouTube

Channel: Finematics

[0]
so what are flash loans all about and how can聽 they be used to borrow millions of dollars worth聽聽
[5]
of crypto with no collateral you'll find answers聽 to these questions in this video before we begin聽聽
[13]
if you want to learn more about decentralized聽 finance make sure you subscribe to my channel聽聽
[19]
a flash loan is a feature that allows you聽 to borrow any available amount of assets聽聽
[24]
from a designated smart contract pool with聽 no collateral flash loans are useful building聽聽
[31]
blocks in d5 as they can be used for things like聽 arbitrage swapping collateral and self-liquidation聽聽
[39]
flash loans although initially introduced by the聽 marble protocol were popularized by ave and dydx聽聽
[48]
so what's the catch a flash loan has to be聽 borrowed and repaid within the same blockchain聽聽
[53]
transaction the concept of a transaction on聽 a blockchain such as ethereum is no different聽聽
[60]
to the traditional definition of聽 a transaction in computer science聽聽
[65]
a transaction represents a set of operations聽 that must be executed in an atomic way聽聽
[71]
either all the steps are executed聽 or the transaction is rolled back聽聽
[76]
and none of the steps take place let's take聽 a simple database transaction as an example聽聽
[83]
if you're already familiar with this concept feel聽 free to skip this part imagine a database table聽聽
[89]
that represents user's balances alice's balance is聽 1 000 and bob's balance is 500 ali sends bob 500聽聽
[99]
dollars in this case we of course have to subtract聽 500 from alice's balance and add 500 dollars聽聽
[109]
to bob's balance so we start writing our聽 database update statements the first update聽聽
[115]
statement subtracts 500 from alice's balance the聽 second statement adds 500 dollars to bob's balance聽聽
[125]
this is cool but what happens if the first update聽 executes but the second one fails for some reason聽聽
[132]
maybe id2 is not present in the system and聽 we made a mistake thinking it's bob's id聽聽
[140]
if this happens we would end up in an inconsistent聽 state with alice's balance equal to 500 dollars聽聽
[148]
and bob's balance also equal to 500 dollars聽 to avoid situations like this we have to use聽聽
[155]
transactions in the above situation in sql we just聽 need to wrap both statements with begin and commit聽聽
[164]
keywords once we do that we can safely assume聽 that either both statements are executed correctly聽聽
[172]
or none of them are leaving alice's and聽 bob's balances untouched we often say聽聽
[179]
that if different steps are a part of the same聽 transaction they are atomic hence indivisible聽聽
[187]
all or nothing when it comes to ethereum聽 every common operation such as sending it聽聽
[194]
sending erc20 tokens and interacting with smart聽 contracts are executed within a transaction scope聽聽
[202]
transactions are grouped together and included聽 in ethereum blocks we can easily see all the聽聽
[209]
transactions that were included in a particular聽 block on any popular block explorers such as聽聽
[216]
etherscan one ethereum transaction can聽 consist of multiple steps for example聽聽
[222]
you could supply heath and borrow dye on compound聽 swap half of you borrow die for usdc on curve and聽聽
[231]
provide liquidity to die usdc pool on uniswap聽 all in one single ethereum transaction of course聽聽
[240]
if any of these steps result in an error the whole聽 transaction will be rolled back and none of the聽聽
[246]
steps will take place remember you will still pay聽 gas fees even for failed contract executions the聽聽
[254]
number of steps in a single transaction is only聽 bounded by the gas cost so although in theory you聽聽
[262]
could create a valid transaction with thousands聽 of steps realistically it would be rejected聽聽
[268]
because of the max gas cost limit per block now聽 let's dive a little bit deeper into flash loans聽聽
[276]
first of all the most important part of聽 executing a flash loan is to find a flash聽聽
[281]
loan provider projects such as other or dydx聽 developed smart contracts that allow defy users聽聽
[290]
to borrow different coins from a designated pool聽 under the condition that they are repaid within聽聽
[297]
the same ethereum transaction there is usually聽 a fixed cost associated with using flash loans聽聽
[304]
other contracts for example require the borrower聽 to return the initial amount plus an extra聽聽
[311]
0.09 percent of the borrowed amount the fee is聽 split between depositors who provide the funds聽聽
[319]
that can be borrowed and integrators who聽 facilitate the use of aves flash loan api聽聽
[326]
a part of this fee is also swapped to other聽 tokens and burned once the amount is borrowed聽聽
[333]
from the landing pool it can be used for any聽 other arbitrary actions assuming that at the聽聽
[339]
end of the chain of different steps the initial聽 flash loan is repaid because the loan has to be聽聽
[346]
repaid within one transaction there is no risk聽 of borrowers not repaying their borrowed amount聽聽
[353]
the only risk is the always present smart contract聽 and platform risk flash loans are becoming聽聽
[361]
more and more popular with some of the users聽 borrowing as high as 14 million die on the other聽聽
[370]
there are three most common use cases for flash聽 loans arbitrage flash loans can magnify the profit聽聽
[377]
of executing a successful arbitrage opportunity聽 let's imagine that there is a price discrepancy聽聽
[385]
in the die usdc pulls between uni swap and curve聽 you can trade one dive for one usdc on curve but聽聽
[394]
you only need 0.99 die to buy one usdc on uni swap聽 now you can try to execute the following arbitrage聽聽
[404]
borrow 100 000 die from ava via flash loan swap聽 100 000 die for usdc on uni swap and receive 100聽聽
[414]
and 1010 usdc swap 100 and 1000 ten usdc for聽 one hundred one thousand and ten die on curve聽聽
[428]
repay initial one hundred thousand die plus zero聽 point zero nine percent fee equals *100 090 profit聽聽
[438]
*920 die this looks nice but we have to聽 take a few extra things into consideration聽聽
[446]
network fees arbitrage transactions with聽 multiple steps can be quite expensive聽聽
[452]
always take transaction fees into聽 account when calculating your profits聽聽
[458]
price slippage always calculate how much price聽 slippage you'll experience while executing your聽聽
[464]
order a hint it depends on the size of your order聽 and the liquidity present in the liquidity pool聽聽
[472]
front running there is a high chance that someone聽 else will spot the same opportunity and will聽聽
[478]
manage to get their transaction mind ahead of聽 you on top of that bots that monitor the mempool聽聽
[486]
can pick up your profitable arbitrage opportunity聽 and send the same transaction with a higher gas聽聽
[493]
fee profiting them instead of you聽 basically stealing your arp opportunity聽聽
[500]
the next use case for flash loans is a collateral聽 swap let's say you have borrowed dye from compound聽聽
[508]
with it as collateral you can swap your collateral聽 from eath too for example but in the following way聽聽
[517]
take a flashlight in dye to cover聽 the amount of dye that was borrowed聽聽
[522]
repay your compound loan with borrowed聽 dye withdraw your eat swap your eat聽聽
[529]
for bad on uni-swap supplied bath as collateral聽 on compound borrow die against your bad collateral聽聽
[539]
repay flash loan with borrowed die plus fees聽 congrats you just swapped your collateral from聽聽
[545]
heath to bat and paid 0.09 percent of the borrowed聽 amount for this the last but not least example is聽聽
[554]
self-liquidation imagine the following scenario聽 you have a loan in die on compound with east as聽聽
[562]
collateral the east price keeps going down and聽 you're approaching the liquidation level you also聽聽
[569]
don't have to or don't want to deposit more ease聽 to increase your liquidation level and you also聽聽
[576]
don't have the die required to repay the loan聽 now instead of allowing the maker dollar contract聽聽
[583]
to liquidate your collateral and charge you the聽 liquidation fee you can take the following steps聽聽
[590]
take a flash loan for the amount of diet that聽 you owe repay your die loan and withdraw your聽聽
[597]
eat swap enough heat to die in order to repay the聽 flash loan plus fees keep the rest of your eath聽聽
[606]
these were the three most common use cases聽 for flash loans of course the concept of聽聽
[612]
flash loans is quite new and there are still a聽 lot of use cases to be discovered in the future聽聽
[618]
flash loans similarly to crypto can be used for聽 both good and bad when it comes to the latter聽聽
[625]
flash loans were used in most of the recent聽 defy hacks and allowed hackers to magnify聽聽
[632]
their potential profits as they do not require any聽 upfront funds one of the most famous hacks was the聽聽
[640]
bcx hack where a flash loan was used to manipulate聽 the uni swap oracle price as usual the problem聽聽
[649]
was not in the use of flash loans but rather in聽 some incorrect assumptions when it comes to using聽聽
[655]
uniswop as a price oracle events like this are聽 sometimes costly for the people affected by them聽聽
[663]
but they are usually result in the strengthening聽 of the whole defy ecosystem making it more and聽聽
[670]
more antifragile in the future although flash聽 loans are predominantly used by developers聽聽
[677]
it is also possible to use them without doing聽 any coding projects such as collateral swap聽聽
[685]
defy saver fury combo make it possible in聽 the description box below i have included聽聽
[693]
links to our guides on how to code a flash loan聽 with ava and how to use fury combo so no matter聽聽
[700]
if you're familiar with coding or not聽 you can find something that works for you聽聽
[706]
so what do you think about flash loans have you聽 ever used them and do you think they are good聽聽
[711]
for the defy ecosystem comment down below and聽 as always if you enjoyed this video smash the聽聽
[718]
like button subscribe to my channel and check out聽 cinematics on patreon to join our defy community