Morphoblue flash loans let contracts execute custom logic before repayment in the same transaction
Last updated:Morphoblue requires flash loan repayment before its flashLoan call returns within the same transaction. A calling contract receives tokens, executes custom logic through onMorphoFlashLoan, and allows the core to collect the borrowed amount. Native flash loans charge no protocol fee and require no collateral. Successful execution needs enough of the original token and an allowance authorizing the repayment pull. Operation-specific callbacks also support composite actions, sometimes removing the need for a separate flash loan.
The receiver must authorize the core’s repayment pull; a direct token transfer does not replace that collection.
The Singleton, Receiver, and Callback
The core contract holds the tokens, while the receiver supplies the logic executed during its callback. Morphoblue places its lending markets in a singleton, a shared contract with separate market accounting. A receiver contract calls
flashLoan
with a token address, an amount, and arbitrary data. The core sends that amount to the contract making the call. The receiver implements
onMorphoFlashLoan
, which runs while the flash loan call remains active. Any external contracts the receiver invokes retain their own execution requirements.
Transfers Around the Callback
A native flash loan brackets the receiver’s custom logic with an outgoing transfer and a repayment pull. The transfer order determines when the receiver can use the tokens.
Outgoing Token Transfer
The core transfers the selected token before invoking the receiver’s callback. The receiver therefore has access to the borrowed balance during its custom logic. A failed outgoing transfer stops the call before that logic begins. Capacity follows the chosen token: holdings of a different token do not fund this request.
Callback and Final Collection
The core invokes
onMorphoFlashLoan(assets, data)
on the caller, then resumes when that function returns. It collects the original token amount with
transferFrom
. The receiver must finish any operations needed to make repayment possible before returning. The core does not wait for a subsequent wallet transaction to provide the missing tokens.
Why Must Repayment Finish in the Same Transaction?
Repayment must finish during the same transaction because the core collects it before
flashLoan
can return.
A callback error propagating back to the core, or a failed repayment transfer, makes the flash loan call revert. Reversion rolls back the transfers and state changes inside that call, including its nested operations. A surrounding contract can catch the failure and continue its outer transaction. Catching it does not preserve the reverted loan or create a debt payable later. The relevant deadline is the return through the call stack, not a number of seconds or additional blocks.
Repayment Allowance and Token Amounts
Repayment succeeds only when the receiver has both the required token balance and sufficient allowance. The receiver approves the core contract as spender through the token contract. An allowance from the transaction sender’s wallet does not authorize transfers from the receiver. Allowance belongs to a particular token, holder, and spender. Sending tokens directly to the core does not replace its final
transferFrom
; the core still attempts collection from the caller.
The
assets
argument specifies the token quantity in base units. Native flash loans do not calculate repayment using market supply shares or borrow shares. Any amount calculation inside custom logic must preserve this distinction, especially when the same integration also calls lending functions accepting share-based inputs. Token display decimals do not change the integer amount the contract collects.
The Token Balance Available for Flash Loans
Flash loan capacity follows the selected token’s balance in the chosen core deployment at execution. Native flash loans can draw on the core contract’s full balance of the selected token. That balance includes its available market liquidity, deposited collateral, and direct donations. These holdings share a contract address even though markets keep separate positions. A market’s liquidation loan-to-value threshold does not set this balance-based flash loan ceiling.
Available balances change as transactions execute. A prior balance reading does not reserve liquidity for a later call. Separate deployments hold separate balances, so the integration must use the contract on its selected network. The shared balance also does not establish how much a particular lender can withdraw from an individual market.
Protocol Fees and Execution Costs
Gas depends on the executed operations and the network’s price for computation.
Morphoblue charges no protocol fee for its native flash loans. Additional protocols invoked during the callback can impose their own charges, including trading fees when custom logic uses an exchange. Slippage can also affect how many repayment tokens the receiver obtains.
A new market loan repaid in the same transaction adds no elapsed-time interest; debt left outstanding can accrue interest separately. A market’s protocol fee takes a share of interest; it does not become a flash loan surcharge. On Ethereum, reverted execution still consumes gas for work the network performed. Successful repayment also says nothing about whether trading proceeds covered every execution cost.
Authenticated Callbacks and Position Authority
The receiver needs separate controls for callback authenticity and authority to initiate its strategy. A genuine core-originated callback identifies the immediate caller, while the integration controls which strategy instructions it accepts.
Trusted Core Caller
The callback should reject any
msg.sender
other than the intended core deployment before using its arguments. Otherwise, a direct caller could feed forged amounts or data into logic expecting a genuine loan. The receiver also needs appropriate access rules for its entry point initiating
flashLoan
. An unrestricted entry point can let another caller select dangerous instructions even though the subsequent callback comes from the real core. The core does not validate the receiver’s strategy instructions.
Position Management Permissions
Callbacks do not bypass authorization checks on
borrow
,
withdraw
, or
withdrawCollateral
. When the receiver acts for another position owner, that owner must authorize it. Token allowance and protocol position authorization grant different permissions. A receiver can hold repayment tokens and still lack permission to move someone else’s collateral. Any collateral withdrawal must also leave the position healthy when the core checks it.
Flash Liquidity and Deferred Settlement
An operation callback can remove the need for separate flash liquidity when its settlement order matches the strategy. These hooks run after particular accounting changes and before the corresponding token collection.
Supply and Collateral Supply
The
supply
and
supplyCollateral
functions update the relevant position before collecting incoming tokens. With nonempty data, they invoke
onMorphoSupply
or
onMorphoSupplyCollateral
before collection. The supplying contract must deliver the loan asset or collateral asset, respectively. This ordering creates a temporary gap between position accounting and token delivery. Further market calls during the callback retain their own liquidity, authorization, and collateral requirements.
Debt Repayment
The
repay
function reduces debt shares before
onMorphoRepay
runs with nonempty data. Its callback receives the amount of loan tokens the core will collect afterward. Reduced debt can allow an authorized collateral withdrawal during the callback, subject to the remaining position’s health. The core collects repayment from the contract calling
repay
, including when the debt belongs to another address.
Liquidation Settlement
An eligible liquidation transfers seized collateral to the liquidating caller before
onMorphoLiquidate
runs. With nonempty data, the hook can use that collateral to source loan tokens for settlement. The core then collects the required loan tokens from the liquidator. The borrower’s loan-to-value ratio must exceed the market’s liquidation threshold before liquidation can proceed. Collateral conversion costs and the liquidation incentive affect whether the available collateral covers repayment.
Does the Native Interface Follow ERC-3156?
The native Morphoblue interface does not implement ERC-3156. A receiver implementing only that standard needs adaptation before it can accept the native callback. The core’s
flashLoan
accepts a token address, an asset amount, and data. It sends tokens to the caller and invokes
onMorphoFlashLoan
, with no separate receiver argument. ERC-3156 defines a receiver parameter and a different callback signature. The interfaces have different function selectors and callback requirements.
A compatibility adapter must preserve native repayment while translating the interface it exposes. The native zero protocol fee and token-balance ceiling describe underlying lending capacity; they do not establish an adapter’s pricing or behavior. An integration must account for the wrapper’s actual token handling and permissions.
Integration Choices for Token Funding
The required transfer order determines whether a dedicated flash loan or an operation callback fits the integration. Match the resource custom logic needs with the point each function makes that resource available. Market callbacks retain their underlying operation’s checks.
-
Choose native
flashLoanwhen logic needs tokens before a market action. The core must hold enough of the selected token. - Choose a supply callback when logic needs a credited supply or collateral position before incoming-token collection. Nonempty data activates the corresponding hook.
- Keep token approval separate from position authority. Further collateral withdrawals need the relevant authorization and a healthy remaining position.
-
Use
onMorphoRepaywhen reduced debt enables subsequent permitted actions before loan-token collection. The repayment caller must still provide those tokens. -
Use seized collateral to fund liquidation settlement when conversion proceeds and any other available loan tokens together cover the required amount. The corresponding callback is
onMorphoLiquidate.
Everyday questions about Morphoblue
Will Empty Data Skip the Flash Loan Callback?
Empty data does not skip the native flash loan callback. The core invokes onMorphoFlashLoan after sending the tokens regardless of the payload’s length. The receiver must handle that payload and prepare repayment; the optional-data behavior of market-operation callbacks does not apply here.
What Happens If a Flash Loan Requests Zero Assets?
A native flash loan requesting zero assets reverts before transferring tokens or invoking its callback. Zero therefore cannot serve as a callback-only request, even when the receiver has existing funds or the core holds a large token balance.
How Does onMorphoFlashLoan Identify the Token to Approve?
The native callback receives an asset amount and arbitrary data, without a separate token argument. The receiver must obtain the token identity from its own trusted operation context, such as stored configuration or validated callback data. That identity must match the token selected for the loan; the amount alone cannot identify it.
When Does a FlashLoan Event Establish Successful Repayment?
A FlashLoan event retained in a successful transaction receipt reflects a call whose repayment completed. The core emits this event before transferring tokens and invoking the callback. A reverted call loses its logs, even if an outer caller catches the failure. An event seen only in a simulation or execution trace therefore does not establish completed repayment.
Can One Native flashLoan Call Request Several Tokens?
One native flashLoan call accepts one token address and one asset amount. Its data argument supplies callback instructions, not an additional list of tokens for the core to transfer. An integration handling several assets must account for its additional calls and obligations; it cannot substitute token arrays into this native function signature.
Does flashLoan Itself Create Supply Shares or Borrow Shares?
The native flashLoan function itself neither mints supply shares nor records borrow shares. It temporarily transfers tokens and collects the same amount after the callback. Additional lending operations inside custom logic can create or modify market positions, with their own accounting and interest consequences.