In this example i'm going to use Uniswap & Sushiswap,
Lets say on Uniswap 1 ETH
is 2800 DAI
, but on Sushi 1 ETH
is 2500 DAI
You borrow 2500 DAI
from Uniswap using a flash loan. With this 2500 DAI
you can go to Sushi and buy 1 ETH
. Take that 1 ETH
and sell it on Uniswap for 2800 DAI
. Now you have 2800 DAI
, You payback what you borrow and now you have 300 DAI
in profit for you.
The useful thing about this is that you keep the prices on all the major Decentralized Exchanges almost similar by doing arbitrage.
That is what Flash-loans are supposed to be used for, But then their are people that use it to exploit projects or manipulate prices to earn more of a token through a loop hole.
Flash-loans are essential to a platform because it keeps prices stable all around the platform(ETH,BSC,..)
The flash-loan has to go through in one transaction or it will fail, If you don't pay the money back withing that same transaction it will not go through.
Because of this to do a flash-loan you need to use a smart contract where it will do what you programmed it to do and repay the loan in the same transaction