But then how are the funds used? Why is somebody able to transact with funds that they will receive in a block that hasn’t yet been added to the chain?
3. give LOANER_ADDRESS (amount_borrowed + interest)
When 2. is executed, the loanee has the money. When 3. is complete, the loaner has the money back. If the loanee doesn't have the money to give, 3rd step fails. And since its atomic, the whole transaction fails.
flash_loan(int amount_borrowed, func arbitrary_trades)
1. give LOANEE_ADDRESS amount_borrowed
2. call arbitrary_trades()
3. give LOANER_ADDRESS (amount_borrowed + interest)
When 2. is executed, the loanee has the money. When 3. is complete, the loaner has the money back. If the loanee doesn't have the money to give, 3rd step fails. And since its atomic, the whole transaction fails.