Flash Actions

Step-by-step guide to building atomic strategies with Lotus flash loans and callbacks, including leveraged positions, refinancing, and collateral swaps.

Flash actions let you execute complex, atomic strategies in a single transaction using flash loans and optional callbacks.

circle-exclamation

Common use cases:

  • Lever up or down a position atomically.

  • Refinance between tranches or markets.

  • Collateral swaps and liquidation integrations.

How it works:

  • Flash loan transfers assets to your contract, calls your callback, then requires repayment by the end of the transaction.

  • Supply/repay/liquidate/collateral operations accept optional callback data. When configured, Lotus calls back into your contract so you can complete multi-step flows without intermediate state risk.

spinner

Minimal Flash Loan Callback

Implement ILotusFlashLoanCallback to receive flash-loaned tokens and execute your strategy. Lotus calls onLotusFlashLoan after transferring the tokens, then verifies repayment.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import {ILotusFlashLoanCallback} from "lotus/src/interfaces/ILotusCallbacks.sol";
import {ILotus} from "lotus/src/interfaces/ILotus.sol";
import {IERC20} from "openzeppelin/token/ERC20/IERC20.sol";

contract FlashArbitrage is ILotusFlashLoanCallback {
    ILotus public immutable lotus;

    constructor(address _lotus) {
        lotus = ILotus(_lotus);
    }

    function execute(address token, uint256 amount) external {
        bytes memory data = abi.encode(token, msg.sender);
        lotus.flashLoan(token, amount, data);
    }

    function onLotusFlashLoan(uint256 assets, bytes calldata data) external {
        require(msg.sender == address(lotus), "unauthorized");
        (address token, address initiator) = abi.decode(data, (address, address));

        // --- Custom logic here ---
        // Swap, arbitrage, or compose other operations.
        // The full `assets` amount must be repayable by the end.

        // Approve Lotus to reclaim the tokens (no fee).
        IERC20(token).approve(address(lotus), assets);
    }
}
Element
Description

ILotusFlashLoanCallback

Interface your contract must implement

onLotusFlashLoan

Called by Lotus after transferring assets to your contract

assets

Amount of tokens flash-loaned

data

Arbitrary bytes you passed to flashLoan

Integration tips:

  • Ensure callback contracts are minimal and non-reentrant with clear allowlists.

  • Validate amounts and state before returning from callbacks.

  • Prefer known-good routers and auditors' patterns for token transfers.

See Also

Last updated