0
0
Blockchain / Solidityprogramming~10 mins

Flash loans in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Flash loans
Start Transaction
Borrow funds instantly
Use funds for arbitrage or swap
Repay loan + fee within same transaction
If repayment successful
Transaction succeeds
If repayment fails
Transaction reverts, no loss
Flash loans borrow funds instantly within one transaction, use them, and repay immediately; if repayment fails, the whole transaction is canceled.
Execution Sample
Blockchain / Solidity
function executeFlashLoan() {
  borrow(amount);
  useFunds();
  repay(amount + fee);
}
This code borrows funds, uses them, and repays the loan plus fee all in one transaction.
Execution Table
StepActionState ChangeConditionResult
1Start transactionTransaction beginsN/AReady to borrow
2Borrow fundsBalance += amountLoan availableFunds received
3Use fundsPerform arbitrage or swapFunds borrowedPotential profit
4Repay loan + feeBalance -= amount + feeSufficient fundsLoan repaid
5Check repaymentVerify repaymentRepayment successfulTransaction succeeds
6End transactionCommit changesAll steps successfulState updated
ExitIf repayment failsRevert all changesRepayment failedTransaction reverted, no loss
💡 Transaction reverts if loan plus fee is not repaid within the same transaction
Variable Tracker
VariableStartAfter BorrowAfter UseAfter RepayFinal
balanceinitialinitial + amountvaries (profit/loss)initial + profit - feeupdated or reverted
Key Moments - 3 Insights
Why does the transaction revert if the loan is not repaid?
Because flash loans require repayment within the same transaction; if repayment fails, the blockchain cancels all changes to prevent loss, as shown in the exit row of the execution_table.
Can you keep the borrowed funds after the transaction?
No, the borrowed funds plus fee must be repaid before the transaction ends, or else the entire transaction is reverted, ensuring no funds are lost or kept.
What happens if the useFunds step does not generate enough profit?
If the profit is insufficient to repay the loan plus fee, the repayment step fails, causing the transaction to revert as shown in the exit row of the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what happens at step 4?
ATransaction ends successfully
BBorrow funds, increasing balance
CRepay loan plus fee, reducing balance
DTransaction reverts
💡 Hint
Check the 'Action' and 'Result' columns at step 4 in the execution_table
At which step does the transaction revert if repayment fails?
AStep 3
BExit row
CStep 5
DStep 6
💡 Hint
Look for the row labeled 'Exit' in the execution_table
If the useFunds step generates no profit, what is the likely outcome?
ATransaction reverts due to failed repayment
BTransaction succeeds with profit
CLoan is repaid with fee easily
DFunds are kept without repayment
💡 Hint
Refer to the 'Use funds' and 'Repay loan + fee' steps in the execution_table and variable_tracker
Concept Snapshot
Flash loans let you borrow funds instantly within one transaction.
You must use and repay the loan plus fee before the transaction ends.
If repayment fails, the whole transaction reverts, so no loss occurs.
Common uses include arbitrage and swaps.
This ensures risk-free borrowing within a single blockchain transaction.
Full Transcript
Flash loans are a special kind of loan on the blockchain that lets you borrow money instantly but only within one transaction. The process starts by beginning a transaction, then borrowing the funds. You use these funds for something like arbitrage or swapping tokens. Before the transaction ends, you must repay the loan plus a small fee. If you do this successfully, the transaction completes and all changes are saved. But if you fail to repay, the blockchain cancels the entire transaction, so no one loses money. This makes flash loans a powerful tool for quick, risk-free borrowing and trading within a single transaction.