I'm trying to understand flash loans. It seems like because the to/from payment both happen within the same function, we cannot atomize each to a transaction on chain. Is this assumption incorrect?
1 Answer
You are correct. Basically, how flash loans work is:
- First you call a contract's borrow function.
- Then the contract sends the required funds.
- After you receive your funds you can do whatever you want with them.
- At the end the contract checks if the returned funds are sufficient.
- If they are not the transaction will fail.
This is possible due to the fact that if a require() fails at the end of the function, it will revert all the changes that happened before, so there is no way you could take the funds and not return them.
-
Sure, but if the flash loan succeeds, does the chain show
contract (100 tokens) -> user
anduser (100 tokens) -> contract
, or does the chain not show either because it happened in one transaction? Commented Jun 14, 2023 at 20:25 -
As the chain I suppose you mean Etherscan? Block explorers use events to figure out where the tokens went. So both transactions will trigger an event and will consequentially be displayed in transaction. Example: etherscan.io/tx/… Commented Jun 15, 2023 at 7:14