Lets hack Flash Loans | Flash loan assignment/discussion

Thank you for all your teachings, i now have the confidence to dive deep into the DeFi eco-system.

1 Like

Kovan testnet faucet is not working for me either. I went to https://app.mycrypto.com/faucet and requested 0.01 KOVANETH. However, the transaction did not go through and was not located on Etherscan. Would a course admin please help? @thecil

To help future students, would someone be willing to update the lesson notes with the correct faucet link?

Also, the Aave Market link on ā€œProgram a Basics Flash Loanā€ part 1 & 2 lessons does not work. Looks like it is here and here.

1 Like

There are multiple variables we would have to estimate and assess before deciding to execute flash loan code in the real world. For example,

  • How to estimate the protocol fees for the loan provider and for both factories?
  • How to determine how much collateral we need to execute the arbitrage?
  • How to estimate the ETH fee needed for the flash loan?
  • We would also need to monitor the exchange rates for a given trading pair to estimate the profit, then compare it with the above question variables to determine if we would profit.

@amadeobrands Thank you for your hard work to help us learn about flash loans. Is there a good resource that describes how to assess the above questions?

We have a new video on DeFi 101 that use another faucet for kovan, take a look on the links we provide in that lesson :nerd_face:

https://academy.moralis.io/lessons/metamask-tutorial-part-2

Carlos Z

1 Like

Thank you, that worked. Here is the Kovan faucet link for other peopleā€™s reference: https://faucets.chain.link/kovan

@thecil To help future students, would someone be willing to update the lesson notes with the correct faucet link?
The Aave Market link on ā€œProgram a Basics Flash Loanā€ part 1 & 2 lessons does not work. Looks like the correct links are here and here.

1 Like

Done, thank you very much for providing the links, its make easier for me to fix asap :nerd_face:

If you have a time, just verify that the new links are working for you (in both lessons for prog basic flashloan p1 & p2).

Carlos Z

1 Like

Hi Iā€™m working on a flashloan and I am trying to find the uniswap factory address or any swap factory address. Do you know where that might be?

1 Like

Hi Iā€™m working on a flashloan and I am trying to find the uniswap factory address or any swap factory address. Do you know where that might be?

1 Like

Youā€™re welcome :slight_smile:. Yes, the ETH Kovan faucet link and the AAVE Market link work on both Part 1 and Part 2 lessons. Thanks.

The Uniswap v2 general factory that creates swap contracts appears to be available here: https://docs.uniswap.org/protocol/V2/reference/smart-contracts/factory#address

I believe the factory addresses used in the course arbitrage code depend on the token pair (see course Github repos).

There are multiple variables we would have to estimate and assess before deciding to execute flash loan code in the real world. For example,

  • How to estimate the protocol fees for the loan provider and for both factories?
  • How to determine how much collateral we need to execute the arbitrage?
  • How to estimate the ETH fee needed for the flash loan?
  • We would also need to monitor the exchange rates for a given trading pair to estimate the profit, then compare it with the above question variables to determine if we would profit.

@thecil Iā€™m guessing Amadeo is unavailable to help with these questions to assess flash loans in real-world scenarios. Is someone else available to teach us how to assess these questions and put this learning into real-world practice?

1 Like

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