Lets hack Flash Loans | Flash loan assignment/discussion

This is a little late answer, but thanks for this post. I shall wait for updated version of DeFi 201 course, because I want to do flash loans but with the updated methods (such as those you mentioned in the post) DeFi 101 course just got updated, I believe @amadeobrands is working on updated DeFi 201. Will be waiting for it.

Also I wish there would be some kind of lecture on how to counter front running, which is a big issue on this area.

I haven’t test in a mainnet any flashloan, just testnets, cant tell you for sure what could be the best practices when taking this into a mainnet, but i think the calculations for the fee must be done manually and taking in consideration the network congestion (when fees goes high like today at 220wei).

Maybe the solidity wizard @mcgrane5 could give us a little suggestion for this :mage:

Carlos Z

1 Like

hey @BitcoinAlchemist. yeah i can try to give some points on what I know. This actually seems like an intresting course on the academy that i have not yet take. might get around to it at some point.

Ok so in real world scenarios i guess there are a few cases that we can use flashloans such as arbitrage, colletoral swapping and self liquidation. The main or most popular use case is probably arbitrage. For example making an arbitrage bot to maximise profit on token pair price mistmatches using flashloans. Arbitrage is sually executed using automated scripts hence the term “arbitrage bots” and can fall into a few different categories. We have regular arbitrage bots, liquidation bots and sniper bots and more. you can ook them up if your curious

I guess in terms of assessing risk here for considering to use a flashloan for a regualr arb bot there really isnt much. This is the beauty of flashloans. Since the ethereum protocol is atomic in nature if we have a transaction that someone executes and at some opint the code faisl then the entire transaction will revert as if nothing ever happened. This whole premise is used in flash loans

So a flahsloan it its simplest for is a loan that can be borrowed from blockhain AMM poopl or other or liquidiuy pools no questions asked. The only clause is that the loan must be paid back in the same transaction that it was borrowd in. And heres the beautiful part. For non programmers here, on the ethereum BC you can write smart contracts that execute multiple transactions in one. Another way of saying this is you can bundle up many transactions into one. So in terms of flashloans what this means is you could write a function to borrow the loan, then in the same transaction you could define logic to send thr loan to some dex smart contract then use the money to swap some tokens then send the money back to the contrct where you took out the loan, then pay back then loan and boom. Thats like 5 different transcations bundles up into one. so here we have successfully taken out a flahsloan and paid it back in the same transaction

So why is this risk free? Well as I mentioned since ethereum transactions get revered if any piece of logic code fails then if we take the case above say we take out a loan for 100 daibon uniswap, then we trade 100 dai for 110 dai worth of ether on uniswap, then we take this 110 dai worth of eth and send it to suishi swap to trade it back to dai. But say there is a worse rate on sushi swap so that whn we trade the eth back to dai we only now have 90 dai. Therefore we dont have enough dai to pay back the flashloan so our transaction will get revereted and uniswap wont loose money and neither will we.

So this is the beautiful thing about flashloans is that they are pretty much risk free. now i say nearly because we will still have to account for trader fees which i think is 0.03% on uniswap and also gas fees we could potentially find a real oportunutiy which makes us profit from arbitraging a token pait on two exchages but if the gas for the trade is more than the profit then well loose money. This is the only way we can loose money using flash loans really.

Arbitrage is one thing that I have studied quite well so if you have any other questions let me know. As for things like self liquiddation of colleroral swapping im not as insightful on so if thats what your intrested in i suggest you do your own research incase i say smothing from from what i do know

EDIT
In terms of the uniswapV2 protocol. many of the functions that you would use to help for various calculation sin arbitrage such as max profit etc take into account the 0.03% fee so specifically for uniswap many time syou can leave this calculation out as uniswap handle it for you. The only thing you would need to estimate is gas and you can do this simply using web3.js on any contract function by calling “estimatedGas = contractInstance.methods.NameOfFunction().estimateGas()”

I have not played around with flashloans from Aave which i know are the popular choice amoungst a lot of people so im not sure what way their SC’s work i will surely look into this for you and let you knoe. But yes i some cases you will need to calculate things like fee estimatio yourslf as Carlos mentions and the best way to go about this is to go to the github of the flashloan provider you rusing and to read their smart contract and documentation directly where you will be able to figure all of this stuff out

1 Like

Hi, I follow the instructions on the Programs a Basic Flash Loan part 2 lesson but get an error message when I transact:
“Gas estimation failed. Gas estimation errored with the following message (see below). The transaction execution will likely fail…”
I used the same code in the Github repository and modified the PROVIDER ADDRESS to
0x88757f2f99175387aB4C6a4b3067c77A695b0349,
according to https://docs.aave.com/developers/deployed-contracts/deployed-contracts.
I sent DAI to the contract from my Metamask on the Kovan network, as explained, and increased the gas limit.
What went wrong?
Thanks

1 Like

Probably nothing went wrong from your side, just that the market conditions for the flash loans are not profitable, therefore it will revert all the process (there is a require at the end of the process that check if its profitable or not), if you just trigger the transaction, the revert msg will be sent to you.

Carlos Z

Hi, thank you for replying
Can you be more specific about the market conditions (that ate not profitable)? What exactly is the profit here?
Also, is there any error code/message so I can understand the problem?
Thanks

1 Like

Most revert errors are a require condition that got triggered, please share your contract in the following way to review it properly

Carlos Z

Sorry, I do not understand the instructions how to encapsulate the code.
I tried to use it in the code file but got an error; can you be more specific please?

An additional flash loan use case I could think of would be flash minting. The idea of this is to obtain instantaneous minting of an arbitrary amount of an asset. These newly minted assets then exist only during one transaction and are burned at the end of the transaction cycle.

1 Like

Hi everyone. Is it just me that thinks flash loans are not profitable anymore considering the extreme high gas fees nowadays for uniswap transactions?

I learnt a lot during this course, a lot of useful information and even if I am not a programmer I am now committed to discover more within this space, just that the defi space moves so fast that I think the content of this course is a bit outdated, a lot of things have changed since the creation of the course. I am looking forward for a new updated version of the course, big thank you to the instructors for sharing with us all this alpha.

2 Likes

I also found it outdated, there are front runner bots aswell which will prevent anyone with human speed to profit from arbitrage opportunities. Or least it was so last time I researched. I hope an update is made with possibly new way how to profit with flash loans if there is one

4 Likes
  1. my first flash loan I made a profit of 90 usd:
    First Flash loan

  2. Arbitrage loan with DAI and BAT:
    with 100 DAI i made 2.8. profit - gas fees = 2.6%
    Arbitrage DAI - BAT success
    Arbitrage DAI - BAT - did not profit

  3. Arbitrage Opportunity DAI- MKR
    for some reason I was able to profit in both Uniswap factories I tried many times:
    Attempt 1 - 0.91 cents profit or 90%
    [Attempt 2] 0(https://kovan.etherscan.io/tx/0x090ef6990756641be64973436a3a81d0a1ef834775a3dcc850719f252f9b5e73) - 0.90 cents profit or 90%
    i also noticed that kETH faucet is backward where kETH is way bigger than wBTC so some numbers were not really in sync with price reality also 1 kETH = 75K MKR so my ratios were not the same as when @amadeobrands made his video.

I really like the solidity code and flash loans and want to understand more about the mathematical concepts and potential hacking models as well as the consequences of multiple risk creation and how to avoid a catastrophe like what is going on in DeFI with lot of off-chain issues that impact any crypto asset

1 Like

Also after completing the Arbitrage opportunity ; what should be let the Liquidity pools created in UNI v1 and v2 swaps? shouldn’t we remove the liquidity? that’s where I believe @amadeobrands missed to mention in the last video

It can really be interesting! Thank you for sharing such information and giving people the opportunity to earn using the knowledge and skills of other people.

1 Like

Hi guys,
I’m on the program a Basics Flash Loan part 2 of the video, and when I go to the AAVE docs webpage, the page is no longer available. and it seems like it is due to AAVE hade move from v2 to v3. So I’m wondering what provider address should I use now?
in addition, the video uses Koven testnet, which is depicted. So I’m wondeing would Gorelis testnet works the same?

Also I think this course needs an update.

1 Like

Hey @Philip_Wong , hope you are great.

I have created a guide for that minor issue related to the kovan network, please give it a look :eyes:

Hope it helps you, let me know any question you might have for it.

Carlos Z

1 Like

hey @thecil,
what about the provider address part? i couldn’t find the documentation or the up to date address for that anymore.
also where can I find the address to use if I want to try the code on the mainnet?

1 Like

Try to use the V2 of aave market, they also have the contracts deployed on goerli.

https://docs.aave.com/developers/v/2.0/deployed-contracts/deployed-contracts

Carlos Z

1 Like

Hey, thanks for sharing this interesting information about Flash Loans and the potential gains they can generate. It’s great to see how the DeFi space is evolving and exploring different use cases for these loans. I’ll definitely check out the Github page and perform the assessments to learn more.

1 Like

I’ve been diving into the world of flash loans, and it’s been quite a thrilling ride! It’s amazing how technology is reshaping the finance landscape. If anyone’s up for a discussion or sharing insights, count me in! But while we’re diving into these high-tech finance hacks, remember that financial stability is crucial too. Recently, I heard about this Mortgage Broker in Rotherham. They’re experts in managing finances and might come in handy in the real world.